Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一到结构图_C#_Structuremap_Unity Container_Ioc Container - Fatal编程技术网

C# 统一到结构图

C# 统一到结构图,c#,structuremap,unity-container,ioc-container,C#,Structuremap,Unity Container,Ioc Container,我正在试用这篇文章中的代码(顺便说一句,非常有趣)。他的IOC容器是Unity,我想使用结构图来实现这一点 他的密码是: public class EventSubscriptions : ISubscriptionService { public static void Add<T>() { var consumerType = typeof(T); consumerType.GetInterfaces()

我正在试用这篇文章中的代码(顺便说一句,非常有趣)。他的IOC容器是Unity,我想使用结构图来实现这一点

他的密码是:

public class EventSubscriptions : ISubscriptionService
{
   public static void Add<T>()
   {
       var consumerType = typeof(T);

       consumerType.GetInterfaces()
                   .Where(x => x.IsGenericType)
                   .Where(x => x.GetGenericTypeDefinition() == typeof(IConsumer<>))
                   .ToList()
                   .ForEach(x => IoC.Container.RegisterType(x, 
                                                            consumerType, 
                                                            consumerType.FullName));
   }

   public IEnumerable<IConsumer<T>> GetSubscriptions<T>()
   {
       var consumers =  IoC.Container.ResolveAll(typeof(IConsumer<T>));
       return consumers.Cast<IConsumer<T>>();
   }
}
公共类事件订阅:ISubscriptionService
{
公共静态void Add()
{
var consumerType=typeof(T);
consumerType.GetInterfaces()
.Where(x=>x.IsGenericType)
.Where(x=>x.GetGenericTypeDefinition()==typeof(IConsumer))
托利斯先生()
.ForEach(x=>IoC.Container.RegisterType(x,
消费者类型,
consumerType.FullName));
}
公共IEnumerable GetSubscriptions()
{
var consumers=IoC.Container.ResolveAll(typeof(IConsumer));
return consumers.Cast();
}
}
我有以下几项似乎不起作用:

public class SubscriptionService : ISubscriptionService
{
    public static void Add<T>()
    {
        var consumerType = typeof(T);

        consumerType.GetInterfaces()
            .Where(x => x.IsGenericType)
            .Where(x => x.GetGenericTypeDefinition() == typeof (IConsumer<>))
            .ToList().ForEach(x => ObjectFactory.Inject(consumerType, x));                                  
    }

    public IEnumerable<IConsumer<T>> GetSubscriptions<T>()
    {
        var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer<T>));
        return consumers.Cast<IConsumer<T>>();
    }
}
公共类订阅服务:ISubscriptionService
{
公共静态void Add()
{
var consumerType=typeof(T);
consumerType.GetInterfaces()
.Where(x=>x.IsGenericType)
.Where(x=>x.GetGenericTypeDefinition()==typeof(IConsumer))
.ToList().ForEach(x=>ObjectFactory.Inject(consumerType,x));
}
公共IEnumerable GetSubscriptions()
{
var consumers=ObjectFactory.GetAllInstances(typeof(IConsumer));
return consumers.Cast();
}
}
我显然对结构图不太熟悉。一些关于我做错了什么的链接或解释将不胜感激

更新:

从亨宁的回答来看,我最终得到了-

public class SubscriptionService : ISubscriptionService
{
    public IEnumerable<IConsumer<T>> GetSubscriptions<T>()
    {
        var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer<T>));
        return consumers.Cast<IConsumer<T>>();
    }
}
公共类订阅服务:ISubscriptionService
{
公共IEnumerable GetSubscriptions()
{
var consumers=ObjectFactory.GetAllInstances(typeof(IConsumer));
return consumers.Cast();
}
}
然后在应用程序启动时调用的引导类中,我有:

public static void ConfigureStuctureMap()
        {
            ObjectFactory.Initialize(x =>
            {       
                x.Scan(y =>
                {
                    y.Assembly("Domain");           
                    y.Assembly("Website");
                    y.AddAllTypesOf(typeof(IConsumer<>));
                    y.WithDefaultConventions();
                });
            });
        }
公共静态无效配置结构映射()
{
ObjectFactory.Initialize(x=>
{       
x、 扫描(y=>
{
y、 汇编(“域”);
y、 大会(“网站”);
y、 添加所有类型(类型(IConsumer));
y、 使用默认约定();
});
});
}

虽然我不是structuremap专家,但我相信您可以用另一种方式来完成

Structuremap能够扫描给定接口的任何给定程序集,并自动注册所有实现。在我目前的项目中,我们做到了这一点,效果非常好

我不记得我们使用的确切代码,但您可以查看文档以进行程序集扫描


构建实现ITypeScanner接口的自定义TypeScanner类

public class EventSubConventionScanner : ITypeScanner
{
    public void Process(Type type, PluginGraph graph)
    {
        Type interfaceType = type.FindInterfaceThatCloses(typeof(IConsumer<>));

        if (interfaceType != null)
        {
            graph.AddType(interfaceType, type);
        }
    }
}
公共类事件SubcontventionScanner:ITypeScanner
{
公共作废流程(类型、PluginGraph图)
{
Type interfaceType=Type.FindInterfaceThatCloses(typeof(IConsumer));
if(interfaceType!=null)
{
graph.AddType(interfaceType,type);
}
}
}
之后,在注册表中或初始化例程写入:

 Scan(x =>
        {
            x.With<EventSubConventionScanner>();
        });
扫描(x=>
{
x、 带();
});