简体   繁体   中英

Resolving dynamic object creation (inside another dynamically created object) with autofac

i'm trying to create a module (that will be referenced in a simple ASP site, using the Autofac Web Form Integration) that has a particular requirement. It should return a string created using a specific set of rules. Those rules are defined by a strategy, different for each "Actor". The "Actor" will be choosed at runtime depending on a value coming from the request. To make things harder, some "Actor" can have a different and specific implementation of this strategy based on, for example, the country of the request.

I have already solved this problem using an abstract and a concrete factory (the first to understand which "Actor" concrete factory use, the second one to determine if i need a specific or a generic actor's strategy). The problem is that, having adopted a strongly modular design, i need to use reflection in order to get an indirect reference to the specific actor's strategy class(otherwise i should have a reference to all the dll's containing all the specific strategy implementations).

I would love to avoid this approach, mostly because i would like to use a unique way to dynamically load the required components.

I've looked at some of the Autofac possibilities and from what i've understood, i can register different implementations of the same interface and resolve them using a key as explained here ( https://code.google.com/p/autofac/wiki/TypedNamedAndKeyedServices ). The problem is that i don't know how to resolve them inside my abstract and concrete factories (i don't have a reference to the resolver there...and i think that i should avoid it).

So my questions are:

1) How can i use Autofac in that scenario? What is the best practice (surely i'm missing something HUGE)

2) Do i have to reconsider all the design of this module? I mean, do i still need to use an abstract and a concrete factory?

3) Can anyone point me to some example or tutorial maybe related to my problem?

Many thanks for your help, and sorry for the long and not-so-clear question.

One possible approach would be to divide your strategies into generic ones and actor specific ones. You already did that in your head, but I don't know if your code reflects that.

  • Have two interfaces IActorSpecificStrategy and IGenericStrategy .
  • Add a method bool CanHandleActor(IActor actor) to both interfaces (or to the interface both derive from).
  • Inject two enumerables into your factory: One that contains IActorSpecificStrategy and one that contains IGenericStrategy .
  • When a strategy for an actor is requested, first enumerate all IActorSpecificStrategy instances and return the first for which CanHandleActor returns true . Otherwise, return the first IGenericStrategy for which CanHandleActor returns true .

If this is not applicable to your problem, please explain more detailed how the strategy is selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM