代码之家  ›  专栏  ›  技术社区  ›  beckelmw

结构图统一

  •  1
  • beckelmw  · 技术社区  · 16 年前

    我正在尝试这个帖子的代码 Event Driven Architecture (顺便说一下,很有趣)。不过,他的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>>();
       }
    }
    

    我有以下似乎不起作用的:

    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>>();
        }
    }
    

    我对结构图显然不太熟悉。关于我做错了什么的一些链接或解释会非常感谢。

    更新:

    从汉宁的回答来看,我最终-

    public class SubscriptionService : ISubscriptionService
    {
        public IEnumerable<IConsumer<T>> GetSubscriptions<T>()
        {
            var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer<T>));
            return consumers.Cast<IConsumer<T>>();
        }
    }
    

    然后在应用程序启动时调用的bootstrapping类中,我有:

    public static void ConfigureStuctureMap()
            {
                ObjectFactory.Initialize(x =>
                {       
                    x.Scan(y =>
                    {
                        y.Assembly("Domain");           
                        y.Assembly("Website");
                        y.AddAllTypesOf(typeof(IConsumer<>));
                        y.WithDefaultConventions();
                    });
                });
            }
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   Henning    16 年前

    虽然我不是结构图专家,但我相信你可以用另一种方式来做。

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

    我不记得我们使用的确切代码了,但您可以查看汇编扫描的文档

    http://structuremap.sourceforge.net/ScanningAssemblies.htm

        2
  •  0
  •   Luca Milan    16 年前

    生成实现ITypeScanner接口的自定义类型扫描程序类。

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

    在注册表或初始化例程写入之后:

     Scan(x =>
            {
                x.With<EventSubConventionScanner>();
            });