在视图模型类中注入许多类

发布于 2024-10-11 19:01:23 字数 817 浏览 7 评论 0原文

我使用 MVVM 设计开发 wpf 应用程序。作为 MVVM 框架,我使用 Caliburn Micro。我在外部程序集中拥有服务,它使用 MEF 注入到视图模型类中。

服务中的问题我有很多小班,我尝试尊重SOLID原则。

[Export(typeof(IClassA)]
public class ClassA : IClassA
{}

[Export(typeof(IClassB)]
public class ClassB : IClassB
{}

[Export(typeof(IClassC)]
public class ClassC : IClassC
{}

//...
[Export(typeof(IClassK)]
public class ClassK : IClassK
{}

班级人数约为12-15人。

我需要在视图模型类中使用此类。所以我用这个:

public class MyViewModelClass
{
 private interface IClassA _a;
 private interface IClassB _b;
 private interface IClassC _c;


//...
 private interface IClassK _k;


[ImportingConstructor]
public MyViewModelClass(IClassA a, IClassB b, IClass c, ..., IClassK k)
{
_a=a; _b=b; _c=c; ...  _k=k

}

}

我不认为这种方式是正确的。或者它存在一些优雅、简单的东西。感谢您的意见和建议。

I develop wpf app with MVVM design. As MVVM framework I use Caliburn Micro. Service I have in external assembly, and it is injected in view models classes with MEF.

Problem in service I have many small class, I try respect SOLID principe.

[Export(typeof(IClassA)]
public class ClassA : IClassA
{}

[Export(typeof(IClassB)]
public class ClassB : IClassB
{}

[Export(typeof(IClassC)]
public class ClassC : IClassC
{}

//...
[Export(typeof(IClassK)]
public class ClassK : IClassK
{}

Classes count is about 12-15.

I need use this classes in view model class. So I use this:

public class MyViewModelClass
{
 private interface IClassA _a;
 private interface IClassB _b;
 private interface IClassC _c;


//...
 private interface IClassK _k;


[ImportingConstructor]
public MyViewModelClass(IClassA a, IClassB b, IClass c, ..., IClassK k)
{
_a=a; _b=b; _c=c; ...  _k=k

}

}

I don’t that this way is correct. Or it exist something elegant, simple. Thank for your opinion and advices.

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

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

发布评论

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

评论(2

静若繁花 2024-10-18 19:01:23

致:阿贝·海德布雷希特。

我有点困惑。如果我用这个:

public class MyViewModelClass : IPartImportsSatisfiedNotification
{
    [Import]
    private IClassA _a;  // I need init _a?
    [Import]
    private IClassB _b; 
    [Import]
    private IClassC _c;

    ...

    public void OnImportsSatisfied()
    {
        // what code must be here ?
        //how can I check if all import was success
    }
}

因为。

IPartImportsSatisfiedNotification 只有一个方法:OnImportsSatisfied,当所有可以满足的导入都已满足时调用该方法。

我认为在视图模型类的构造函数中对服务类的初始化接口进行单元测试更好。

To: Abe Heidbrecht.

I am little confuse. If I use this:

public class MyViewModelClass : IPartImportsSatisfiedNotification
{
    [Import]
    private IClassA _a;  // I need init _a?
    [Import]
    private IClassB _b; 
    [Import]
    private IClassC _c;

    ...

    public void OnImportsSatisfied()
    {
        // what code must be here ?
        //how can I check if all import was success
    }
}

Because.

IPartImportsSatisfiedNotification has only a single method: OnImportsSatisfied, which is called when all imports that could be satisfied have been satisfied.

I think it is better for unit testing init interfaces of services class in contstructor of view model class.

一江春梦 2024-10-18 19:01:23

MEF 可以导入到字段(甚至是私有字段)。如果您想让您的生活更轻松一些,您可以使用 ImportAttribute 修饰字段。如果您想知道所有导入何时完成,只需实现 IPartImportsSatisfiedNotification 接口:

public class MyViewModelClass : IPartImportsSatisfiedNotification
{
    [Import]
    private IClassA _a; 
    [Import]
    private IClassB _b; 
    [Import]
    private IClassC _c;

    ...

    public void OnImportsSatisfied()
    {
        // add initialization code here
    }
}

这几乎需要您使用 MEF 来确保正确实例化您的 ViewModel,但根据您的场景,这可能不会是一件大事。

MEF can import to fields (even private ones). If you want to make your life a little easier, you can just decorate the fields with the ImportAttribute. If you want to know when all the imports have finished, just implement the IPartImportsSatisfiedNotification interface:

public class MyViewModelClass : IPartImportsSatisfiedNotification
{
    [Import]
    private IClassA _a; 
    [Import]
    private IClassB _b; 
    [Import]
    private IClassC _c;

    ...

    public void OnImportsSatisfied()
    {
        // add initialization code here
    }
}

This pretty much requires you to use MEF to ensure that your ViewModel is instantiated correctly, but depending on your scenario that may not be a big deal.

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