在 PRISM WPF 应用程序中具有模块相关的状态注入

发布于 2024-11-28 19:29:37 字数 252 浏览 1 评论 0原文

您好,我们有一个 PRISM WPF MVP 应用程序,我们希望有一个状态可以在同一模块中的视图之间共享数据。由于 PRISM 默认情况下没有状态,我想知道是否有任何方法可以实现它。目前我已经注入了一个带有字典作为后备存储的状态,但问题是它的全局即跨模块可用。我真的很想将这个注入的范围限定为特定于模块。

我相信统一允许根据名称将不同的类注册到同一接口,不确定我唯一的选择是否是在我的场景中利用它。

任何帮助都会很棒!谢谢!

-ioWint

hi we have a PRISM WPF MVP application, we would like to have a state to share data between the views in the same module. Since PRISM by default doesnt have a state, was wondering if there is any way i could implement this. Presently i have injected a State with Dictionary as back-store, but the problem is its Global i.e available across the modules. i would really like to scope this injection being module specific.

I believe unity allows registering different classes to the same interface based on name, not sure if the only choice i have is to leverage that for my scenario.

Any help would be great! Thanks!

-ioWint

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

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

发布评论

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

评论(3

朕就是辣么酷 2024-12-05 19:29:37

我同意,用 ModuleName 确定 Unity 类型注册的范围将是一个起点。

I would agree, scoping Unity's type registration with the ModuleName would be a place to start.

暮光沉寂 2024-12-05 19:29:37

将本地(模块级)状态对象注入到所有想要共享状态的视图中。如果定义状态对象的接口是您的模块的本地接口,则其他模块将无法引用该状态对象,因为它们无法引用该接口。

因此:如果模块 A 有 3 个视图,这些视图采用实现 IStatefulContainer 的对象(也在模块 A 中声明),并且 IStatefulContainer 使用 RegisterInstance 而不仅仅是 RegisterType 向 Unity 注册,那么您将拥有一个作用域为该模块的单例。

我的偏好是拥有一个管理状态的“状态”服务。如果您需要的话,这可以让您在此处添加更多功能,并且是一种更“Prismy”的方法。

编辑


如果您跨模块使用此状态对象,则可以执行以下操作:

1)将接口放入程序集中,任何想要使用它的模块都将引用该程序集。

程序集 A

public interface IBlah
{
    string Add(string stateKey, string stateValue);
}





Assembly B (referencing Assembly A)



    public class Module:IModule
    {
    private IUnityContainer _container;

        public Module(IUnityContainer container)
        {
            _container=container;
        }
        public void Initialize()
        {
            IBlah blah1=new BlahContainer();
            IBlah blah2=new BlahContainer();
            _container.RegisterInstance<IBlah>(blah1,"BlahContainer1");
            _container.RegisterInstance<IBlah>(blah2,"BlahContainer2");

        }
    }

模块 C(引用程序集 A)

_container.Resolve<IBlah>("BlahContainer1");
_container.Resolve<IBlah>("BlahContainer2");

基本上,我们在我们乐意在模块之间共享的程序集中定义接口。一些项目具有“基础设施”或公共程序集,其中包含其他模块使用的服务接口 - 这很适合这里。

然后,我们让模块引用其中包含合约的程序集。

目前我在这里依靠“魔法字符串”,但有很多方法可以解决这个问题。

希望这更清楚一点。

Inject a local(module level) state object into all the views that want to have share state. If the interface that defines the state object is local to your module then other modules won't be able to reference the state object because they can't reference the interface.

So: If Module A has 3 views that take an object implementing IStatefulContainer (also declared in Module A) and IStatefulContainer is registered with Unity using RegisterInstance rather than just RegisterType you'll have a singleton that is scoped to the module.

My preference would be to have a "State" service that managed state. This could allow you to add more functionality here if you needed it and is a more "Prismy" approach.

EDIT


If you're using this state object across modules then you can do the following:

1)Put the interface in an assembly that will be referenced by any module that wants to use it.

Assembly A

public interface IBlah
{
    string Add(string stateKey, string stateValue);
}





Assembly B (referencing Assembly A)



    public class Module:IModule
    {
    private IUnityContainer _container;

        public Module(IUnityContainer container)
        {
            _container=container;
        }
        public void Initialize()
        {
            IBlah blah1=new BlahContainer();
            IBlah blah2=new BlahContainer();
            _container.RegisterInstance<IBlah>(blah1,"BlahContainer1");
            _container.RegisterInstance<IBlah>(blah2,"BlahContainer2");

        }
    }

Module C(references assembly A)

_container.Resolve<IBlah>("BlahContainer1");
_container.Resolve<IBlah>("BlahContainer2");

Basically, we define the interface in an assembly we're happy to share between modules. Some projects have "Infrastructure" or Common assemblies that contain service interfaces that are used by other modules - this would fit well here.

We then have our module reference the assembly with the contract in it.

At the moment I'm relying on "magic strings" here but there are lots of ways around this.

Hope this is a little more clear.

请爱~陌生人 2024-12-05 19:29:37

感谢您更新的解决方案。我试图避免基于名称的 Unity 注册,这会迫使我的演示者知道模块状态注册密钥。
我正在阅读有关 Unity 的 stackoverflow 帖子,并在这里找到了讨论 在使用 Unity 方法注入时是否可以覆盖参数值? .

经过几个小时的反复试验,我最终实现了所需的功能。

我所做的:

我的模块有一个 BaseClass -> BaseModule:IModule 我有一个 State 属性,它符合我在 Infrastructure.Interface 中定义的 IStateService。我在 BaseModule() 构造函数中实例化此 State 属性。
注意:要采用这种方法,我必须使我的 Presenter 具有公共 IStateService 状态;属性..

在模块中注册 Presenter 时,我指定

<UnityContainer>.RegisterType<MyPresenter, new InjectionProperty("State", State).

覆盖 Presenter 中的一个公共属性,该属性的名称为“State”,其 State 实例值在模块中定义。
这样我就能够获取模块状态作为每个视图演示者的状态。

感谢大家指导我找到解决方案。

-ioWint

thanks for your updated solution. I was trying to avoid a name based Unity registration, which would force my Presenter in knowing the Modules State registration Key.
I was reading stackoverflow posts on Unity and found the discussion over here Is it possible to override parameter values when using Method Injection with Unity? .

After couple of hours of trial and errors, i ended up achieving the desired functionality.

What i have done:

I have a BaseClass for my Modules -> BaseModule:IModule i have a State Property in it which conforms to my IStateService defined in the Infrastructure.Interface. I Instantiate this State property in the BaseModule() constructor.
Note: to go with this approach i have to make my Presenter's have a public IStateService State; property..

At the time of registering the Presenter in the module, i am specifying

<UnityContainer>.RegisterType<MyPresenter, new InjectionProperty("State", State).

Am overriding a public property in Presenter which has name "State" with the State instance value defined in the Module.
this way i am able to get the Modules State as the State for each of the View's presenter.

Thanks guys for directing me towards a solution.

-ioWint

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