使用结构图注册通用类型

发布于 2024-09-28 22:39:13 字数 3137 浏览 0 评论 0原文

我是结构图新手。我试图让 Structuremap 自动注册

public void RegisterAllEventHandlers()
{
    Scan(cfg =>
        {
            cfg.TheCallingAssembly();
            //cfg.IncludeNamespaceContainingType<NewCustomerCreated>();
            cfg.IncludeNamespace("ParentNameSpace");
            cfg.AddAllTypesOf(typeof (IHandle<NewCustomerCreated>));
        });

        //For(typeof (IHandle<>)).Use(typeof (NewCustomerCreated));
}

NewCustomerCreated 是事件,我想注册该事件的所有处理程序,即使用 IHandle

以下代码正在工作但我确信可以通过扫描来完成:-

ObjectFactory.Initialize(x =>
    {                                                      
        x.For(typeof(IHandle<NewCustomerCreated>))
            .Add(new NewCustomerCreatedHandler());                                                                        
        x.For(typeof(IHandle<NewCustomerCreated>))
            .Add(new SendWelcomeEmailToNewCustomer());
    });

我正在尝试使用 DomainEvent raiser http://blog.robustsoftware.co.uk/2009 /08/better-domain-event-raiser.html

** 如果有人可以编辑问题以更好地反映我的要求,我将不胜感激 **

谢谢,

Mar

编辑 1:从博客添加代码

 public interface IEventDispatcher
    {

        void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent;

    }


    public static class DomainEventDispatcher
    {

        public static IEventDispatcher Dispatcher { get; set; }



        public static void Raise<TEvent>(TEvent eventToRaise) where TEvent : IDomainEvent
        {

            Dispatcher.Dispatch(eventToRaise);

        }

    }


    public class StructureMapEventDispatcher : IEventDispatcher
    {

        public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
        {

            foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
            {

                handler.Handle(eventToDispatch);

            }

        }

从我的测试项目中,我调用注册表类,它将扫描程序集

  public void RegisterAllEventHandlers()
        {
            Scan(cfg =>
            {
                cfg.TheCallingAssembly();
                cfg.IncludeNamespace("Project1");
                //cfg.AddAllTypesOf(typeof(IHandle<NewCustomerCreated>));
                cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));

            });

            // This initializes correctly 
            //ObjectFactory.Initialize(x =>
            //{
            //    x.For(typeof(IHandle<NewCustomerCreated>)).Add(new NewCustomerCreatedHandler());

            //}); 



          // Handler  returns 0 count 

          var handler =ObjectFactory.GetAllInstances<IHandle<NewCustomerCreated>>();





        }

,然后

var eventDispatcher = new StructureMapEventDispatcher();
            DomainEventDispatcher.Dispatcher = eventDispatcher;

I am new to structuremap. I am trying to get Structuremap to auto-register

public void RegisterAllEventHandlers()
{
    Scan(cfg =>
        {
            cfg.TheCallingAssembly();
            //cfg.IncludeNamespaceContainingType<NewCustomerCreated>();
            cfg.IncludeNamespace("ParentNameSpace");
            cfg.AddAllTypesOf(typeof (IHandle<NewCustomerCreated>));
        });

        //For(typeof (IHandle<>)).Use(typeof (NewCustomerCreated));
}

NewCustomerCreated is the event and I want to register all Handlers for this event ie ones using IHandle<NewCustomerCreated>

The following code is working but I am sure there it can be done by scanning :-

ObjectFactory.Initialize(x =>
    {                                                      
        x.For(typeof(IHandle<NewCustomerCreated>))
            .Add(new NewCustomerCreatedHandler());                                                                        
        x.For(typeof(IHandle<NewCustomerCreated>))
            .Add(new SendWelcomeEmailToNewCustomer());
    });

I am trying to use DomainEvent raiser from
http://blog.robustsoftware.co.uk/2009/08/better-domain-event-raiser.html

** I would appreciate if someone can edit the question to reflect what I am asking in a better way **

Thank you,

Mar

Edit 1: Adding Code from blog

 public interface IEventDispatcher
    {

        void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent;

    }


    public static class DomainEventDispatcher
    {

        public static IEventDispatcher Dispatcher { get; set; }



        public static void Raise<TEvent>(TEvent eventToRaise) where TEvent : IDomainEvent
        {

            Dispatcher.Dispatch(eventToRaise);

        }

    }


    public class StructureMapEventDispatcher : IEventDispatcher
    {

        public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
        {

            foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
            {

                handler.Handle(eventToDispatch);

            }

        }

From my test project I am calling registry class which will scan the assembly

  public void RegisterAllEventHandlers()
        {
            Scan(cfg =>
            {
                cfg.TheCallingAssembly();
                cfg.IncludeNamespace("Project1");
                //cfg.AddAllTypesOf(typeof(IHandle<NewCustomerCreated>));
                cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));

            });

            // This initializes correctly 
            //ObjectFactory.Initialize(x =>
            //{
            //    x.For(typeof(IHandle<NewCustomerCreated>)).Add(new NewCustomerCreatedHandler());

            //}); 



          // Handler  returns 0 count 

          var handler =ObjectFactory.GetAllInstances<IHandle<NewCustomerCreated>>();





        }

and then

var eventDispatcher = new StructureMapEventDispatcher();
            DomainEventDispatcher.Dispatcher = eventDispatcher;

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

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

发布评论

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

评论(1

醉态萌生 2024-10-05 22:39:13

试试这个:

cfg.AddAllTypesOf(typeof (IHandle<>));

然后你的容器将完成剩下的工作:

var handler = container.GetInstance<IHandler<NewCustomerCreated>>(); 

如果你需要更多的控制,请看一下:
https://stackoverflow.com/questions/516892/使用扫描的通用类型的结构图自动注册

Try this:

cfg.AddAllTypesOf(typeof (IHandle<>));

Then your container will do the rest:

var handler = container.GetInstance<IHandler<NewCustomerCreated>>(); 

If you need more control take a look at this:
https://stackoverflow.com/questions/516892/structuremap-auto-registration-for-generic-types-using-scan

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