UnityContainer:解析时的条件逻辑

发布于 2024-10-02 23:11:04 字数 180 浏览 6 评论 0原文

我有一个具有 3 种不同实现的接口。我使用 Unity Container 在 Web 应用程序的 Web.config 中将 3 个实现注册为命名别名。

有没有一种方法可以使用 Unity 根据某种逻辑来解析已注册的实例之一。该逻辑包括联系数据库来决定要解决的实现。

感谢您的帮助。

问候 比拉尔

I have an interface with 3 different implementations. I register the 3 implementations as named aliases in the Web.config of the Web application using Unity Container.

Is there a way using Unity, to resolve one of the registered instance, based on some logic. the logic includes contacting a DB to decide on which implementation to be resolved.

Appreciate your help.

Regards
Bilal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2024-10-09 23:11:05

您可以在抽象工厂中实现逻辑并注入它:

public interface IMyInterface { }

public interface IMyInterfaceFactory {
   IMyInterface GetMyInterface();
}

public class MyInterfaceFactory : IMyInterfaceFactory  {
       private readonly IUnityContainer _container;
       public MyInterfaceFactory(IUnityContainer container) { 
           _container = container; }

       IMyInterface GetMyInterface() {
            var impName = Get_implementation_name_from_db();
            return container.Resolve<IMyInterface>(impName);
        }
}

You can implement the logic in an abstract factory and inject it:

public interface IMyInterface { }

public interface IMyInterfaceFactory {
   IMyInterface GetMyInterface();
}

public class MyInterfaceFactory : IMyInterfaceFactory  {
       private readonly IUnityContainer _container;
       public MyInterfaceFactory(IUnityContainer container) { 
           _container = container; }

       IMyInterface GetMyInterface() {
            var impName = Get_implementation_name_from_db();
            return container.Resolve<IMyInterface>(impName);
        }
}
指尖微凉心微凉 2024-10-09 23:11:05

您可以创建一个“路由器”实现,它知道如何将请求路由到其他实现之一:

// Here is a possible implementation of the router. There are 
// of course many ways to do this.
public class MyRouterImpl : IMyInterface
{
    List<IMyInterface> implementations = new List<IMyInterface>();

    public MyRouterImpl(MyImpl1 i1, MyImpl2 i2, MyImpl3 i3)
    {
        this.implementations.Add(i1);
        this.implementations.Add(i2);
        this.implementations.Add(i3);
    }

    void IMyInterface.Method()
    {
        int indexOfImplementationToExecute = 
            GetIndexOfImplementationToExecute();

        IMyInterface impl =
            this.implementations[indexOfImplementationToExecute];

        impl.Method();
    }
}

You can create a 'router' implementation that knows how to route the requests to one of the other implementations:

// Here is a possible implementation of the router. There are 
// of course many ways to do this.
public class MyRouterImpl : IMyInterface
{
    List<IMyInterface> implementations = new List<IMyInterface>();

    public MyRouterImpl(MyImpl1 i1, MyImpl2 i2, MyImpl3 i3)
    {
        this.implementations.Add(i1);
        this.implementations.Add(i2);
        this.implementations.Add(i3);
    }

    void IMyInterface.Method()
    {
        int indexOfImplementationToExecute = 
            GetIndexOfImplementationToExecute();

        IMyInterface impl =
            this.implementations[indexOfImplementationToExecute];

        impl.Method();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文