开发者

UnityContainer and Constructor Parameters

开发者 https://www.devze.com 2023-01-26 10:09 出处:网络
I have a开发者_Go百科n \"Adapter\" class that wraps an object called \"X_Session\". This adpater expects an ILogger and ICacheManager.

I have a开发者_Go百科n "Adapter" class that wraps an object called "X_Session". This adpater expects an ILogger and ICacheManager.

The way I thought of it is by defining this class with Unity and let it resolve both interfaces that are put in as constructor inputs.

What about the X_Session object? It is not registered by Unity, as I need to create it myself because its constructor requires input parameters that I gather from QueryString.

Do I resolve the Adpater class then set the X_Session object? Other better way?

Thanks


Again, with an abstract factory is simple:

public interface IAdapterFactory {
   Adapter Create(ISession session);
}

public class AdapterFactoryImpl : IAdapterFactory  {
   public AdapterFactoryImpl(IDependency dep) {
       this._dep = dep;
   }

   public Adapter Create(ISession input) { 
     return new Adapter(_dep, input);
   }
}

You have to register in Unity only the factory.


Another idea would be registering the X_Session object inside the Unity container at runtime, then having Unity injecting all the dependencies.

0

精彩评论

暂无评论...
验证码 换一张
取 消