mardi 20 septembre 2016

Wrapper class for generic methods - Optimizing Reflection

I'm looking for some guidance on the best way to optimize my usage of a class which has several generic methods. The general set-up is as follows:

    Interface                         
    ------------------                  
    Implementation (n) --Register--> Data Layer --> Business Layer

I have an interface which has multiple implementations. Additional implementations will be added or removed as times goes on. These are then registered with a data abstraction layer. This abstraction layer has a slew of generic methods which can operate on any classes that implement Interface (constraints.) This is all fine so far.

My business logic layer now needs to use the data layer. It knows what is registered with the data layer but can't directly call any of the generic methods as you need to call them with a concrete type. So. Reflection.

I need things to be fast so my plan is to create a wrapper that goes between the Data Layer and the Business Layer. This would create and then cache a Func<> delegate of the methods (using MakeGenericMethod and then putting that into a delegate.) The business layer would then use that wrapped up data layer, calling methods with the types registered.

I have a doubt, though, as this has a memory overhead and I'm guessing will be a bit hard to maintain. I've looked into the dynamic keyword rather than using reflection directly which seems promising as it reduces the burden of caching but would require changing quite a bit of my data layer. Reflection.Emit is also possible but a little terrifying and I'm honestly not experienced enough to use it without having a brain freeze.

Are there any better ways of doing this?





Aucun commentaire:

Enregistrer un commentaire