【问题标题】:ViewModels as Handlers with MediatR, StructureMap, Caliburn.MicroViewModels 作为带有 MediatR、StructureMap、Caliburn.Micro 的处理程序
【发布时间】:2016-06-16 02:00:22
【问题描述】:

我们将 Caliburn.Micro 用于我们的 MVVM 框架,将 StructureMap 用于我们的 IoC 容器,并将 MediatR 用于我们的中介器实现。这一切都很好,除了注册 MediatR 事件处理程序的推荐方法与 Caliburn.Micro 推荐的将 ViewModel 用作自己的处理程序的方法不兼容。

Caliburn.Micro 通过 EventAggregator 实现中介者模式,这要求您将 IEventAggregator 注入 ViewModel 并订阅自身(或实现 IHandle 接口的东西)。 MediatR 采用更加解耦的方法,建议您反射性地扫描程序集以查找关闭 IRequestHandler 和其他类型的类型。

我认为我缺乏使用 StructureMap 的经验是我的问题。

我想做的是能够在 ViewModel 本身上实现 Handler 功能(如 Caliburn.Micro 建议的那样),同时确保 ViewModel 注册为 Caliburn.Micro 的 Singletons。

    public class RibbonMenuViewModel : PropertyChangedBase, INotificationHandler<SomethingSelectedEvent> { }

当 StructureMap 处理以下 Registry 时,将有 2 个 RibbonMenuViewModel 实例:一个用于 Caliburn.Micro 的单例版本和一个关闭 MediatR INotificationHandler 泛型类型的临时版本。

结构图注册表

    public class ViewModelsRegistry : Registry
    {
        public ViewModelsRegistry()
        {

            // ensure registration for the ViewModel for Caliburn.Micro
            this.ForConcreteType<RibbonMenuViewModel>().Configure.Singleton();


            // MediatR handler registrations
            this.Scan(s =>
            {
                s.Assembly(this.GetType().Assembly);

                s.ConnectImplementationsToTypesClosing(typeof (IRequestHandler<,>));
                s.ConnectImplementationsToTypesClosing(typeof (IAsyncRequestHandler<,>));
                s.ConnectImplementationsToTypesClosing(typeof (INotificationHandler<>));
                s.ConnectImplementationsToTypesClosing(typeof (IAsyncNotificationHandler<>));
            });

        }
    }

    

我想获得有关使用 Singleton ViewModel 注册作为 MediatR 的 INotificationHandler 实例的最佳方法的建议

这里是 Caliburn.Micro 配置供参考:

Caliburn 引导程序配置

    protected override void Configure()
    {
        this.configureTypeMappings();

        if (!Execute.InDesignMode)
        {
            this.configureIocContainer();
        }
    }

    private void configureIocContainer()
    {
        this.container = new Container(this.getStructureMapConfig);
    }
    
    
    private void getStructureMapConfig(ConfigurationExpression cfg)
    {
        cfg.For<IWindowManager>().Use<WindowManager>().Singleton();

        cfg.Scan(s =>
        {
            s.AssemblyContainingType<ViewModelsRegistry>();

            s.LookForRegistries();
        });
    }
    

    protected override IEnumerable<object> GetAllInstances(Type serviceType)
    {
        return this.container.GetAllInstances(serviceType).OfType<object>();
    }


    protected override object GetInstance(Type serviceType, string key)
    {
        if (serviceType == null) serviceType = typeof(object);
        var returnValue = key == null
                ? this.container.GetInstance(serviceType) : this.container.GetInstance(serviceType, key);
        return returnValue;
    }
    protected override void BuildUp(object instance) { this.container.BuildUp(instance); }

    
    

【问题讨论】:

    标签: structuremap caliburn.micro mediator mediatr


    【解决方案1】:

    我遇到了类似的问题。 这是我修复它的方法。

    public class ViewModelsRegistry : Registry
    {
        public ViewModelsRegistry()
        {
    
            // MediatR handler registrations
            this.Scan(s =>
            {
                s.Assembly(this.GetType().Assembly);
    
                s.ConnectImplementationsToTypesClosing(typeof (IRequestHandler<,>));
                s.ConnectImplementationsToTypesClosing(typeof (IAsyncRequestHandler<,>));
                s.ConnectImplementationsToTypesClosing(typeof (INotificationHandler<>));
                s.ConnectImplementationsToTypesClosing(typeof (IAsyncNotificationHandler<>));
                s.ExcludeType<RibbonMenuViewModel>();
            });
            // ensure registration for the ViewModel for Caliburn.Micro         
    
            For(typeof(INotificationHandler<>)).Singleton().Add(typeof(RibbonMenuViewModel));
    
        }
    }
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-24
      • 2019-07-02
      • 2019-12-21
      • 2021-01-28
      • 2016-10-19
      • 2021-06-22
      • 2021-08-24
      • 2020-12-08
      相关资源
      最近更新 更多