统一结构图

发布于 2024-08-17 04:12:34 字数 2671 浏览 7 评论 0原文

我正在尝试 事件驱动架构(顺便说一下,非常有趣)。他的 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>>();
    }
}

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

更新:

根据 Henning 的回答,我最终得到 -

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

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

public static void ConfigureStuctureMap()
        {
            ObjectFactory.Initialize(x =>
            {       
                x.Scan(y =>
                {
                    y.Assembly("Domain");           
                    y.Assembly("Website");
                    y.AddAllTypesOf(typeof(IConsumer<>));
                    y.WithDefaultConventions();
                });
            });
        }

I am trying out the code from this post on Event Driven Architecture (very interesting by the way). His IOC container is Unity though and I would like to do this using Structure map.

His code is:

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

I have the following which does not seem to be working:

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

I am obviously not too familiar with Structure Map. Some links or explanation on what I am doing wrong would be really appreciated.

Update:

From Henning's answer, I ended up with -

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

And then in my bootstrapping class that is called on application startup I have:

public static void ConfigureStuctureMap()
        {
            ObjectFactory.Initialize(x =>
            {       
                x.Scan(y =>
                {
                    y.Assembly("Domain");           
                    y.Assembly("Website");
                    y.AddAllTypesOf(typeof(IConsumer<>));
                    y.WithDefaultConventions();
                });
            });
        }

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

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

发布评论

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

评论(2

林空鹿饮溪 2024-08-24 04:12:34

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

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

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

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

Although I'm not a structuremap expert, I do believe you can do it in another way.

Structuremap has the ability to scan any given assembly for a given interface and automatically register all the implementations. We do that in my current project and it works really great.

I don't remember the exact code we use, but you can check out the documentation for assembly scanning

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

冷血 2024-08-24 04:12:34

构建实现 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);
        }
    }
}

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

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

Build custom TypeScanner class that implement ITypeScanner interface.

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

        if (interfaceType != null)
        {
            graph.AddType(interfaceType, type);
        }
    }
}

After, in registry or initialize routine write:

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