使用结构图解决Child类依赖关系

时间:2022-09-04 11:12:39

I am trying to resolve dependencies using structure map.How do I resolve all child class dependencies automatically by resgitering the base interface using structure map.

我正在尝试使用结构map解决依赖关系。如何通过使用结构图重新调整基接口来自动解决所有子类依赖关系。

Conside the following example. I dont want to register UserHandler, officeHandler etc to the container separately. Is there a way I can register just the IHandler and resolve the handler automatically by passing IHandler, IHandler to get the respective handler.

考虑以下示例。我不想分别将UserHandler,officeHandler等注册到容器中。有没有办法我可以只注册IHandler并通过传递IHandler,IHandler自动解析处理程序来获取相应的处理程序。

public interface IHandler<T>
{
}

public class UserHandler : IHanlder<User>
{
}

public class OfficeHandler : IHandler<Office>
{
}

1 个解决方案

#1


This should do the job for you:

这应该为你做的工作:

var container = new Container(cfg =>
{
    cfg.Scan(s =>
    {
        s.TheCallingAssembly();
        s.IncludeNamespaceContainingType<UserHandler>();
        s.ConnectImplementationsToTypesClosing(typeof(IHandler<>));
    });
});

See this article for further details.

有关详细信息,请参阅此文章。

#1


This should do the job for you:

这应该为你做的工作:

var container = new Container(cfg =>
{
    cfg.Scan(s =>
    {
        s.TheCallingAssembly();
        s.IncludeNamespaceContainingType<UserHandler>();
        s.ConnectImplementationsToTypesClosing(typeof(IHandler<>));
    });
});

See this article for further details.

有关详细信息,请参阅此文章。