本文介绍了简单的注射器中注入多的BaseClass依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 BaseViewModel 这是由多重继承视图模型类。在我的 BaseViewModel 我有一对夫妇其中获得从视图模型注入依赖。现在,如果我需要添加一个新的依赖我的 BaseViewModel 我需要的所有更改的继承 BaseViewModel 虚拟机。请让我知道如何在这简单的注射器进行处理。以下是我的代码结构:

I have a BaseViewModel which is inherited by multiple ViewModel classes. In my BaseViewModel I have a couple of dependencies which get injected from ViewModel. Now if I need to add a new dependency in my BaseViewModel I need to change all the VM which inherit BaseViewModel. Please let me know how can it be handled in Simple Injector. Following is my code structure:

我怎样才能让我独立的基类注射,这样我不需要做在我所有的继承类的变化

How can I make my base class injection independent so that I don't need to make changes in all my inherited class?

代码:

public class BaseViewModel
{
    protected readonly IAESEnDecrypt AESEnDecrypt;
    protected readonly IDataService DataService;
    protected readonly INavigationService NavigateToPage;
    public BaseViewModel(INavigationService nav, IDataService data, IAESEnDecrypt encrypt)
    {
        AESEnDecrypt= encrypt;
        NavigateToPage = nav;
        DataService = data;
    }
}


public class ViewModel
{
   public ViewModel(INavigationService nav, IDataService data, IAESEnDecrypt encrypt) : base (nav, data, encrypt)
   {

   }
}

我BaseViewModel包含以下一些接口的实现是通过构造函数注入的:

My BaseViewModel Contains some of the following Interfaces whose implementation is injected through constructor:

- NavigationService
- DataService
- GeoLocationService
- SmartDispatcher
- MessageBus which implement Message Aggregator



它也包含了一些常用的属性,静态变量的数据被用于整个就像是UserDetails中的应用。并且还包含的CancellationToken,IsBusy显示进度。

It also Contains some common properties as static variables whose data is used throughout the application like UserDetails. And also contains CancellationToken, IsBusy to display progressbar.

BaseViewModel还含有处理来自所有视图模型所有传入的异常HandleException方法。
也包含在所有喜欢思
gnoutCommand,导航栏命令视图中常用的一些命令。

BaseViewModel also contain HandleException method which handle all the incoming exceptions from all ViewModel.Also Contains some common Commands which are used in all the Views like SignoutCommand, NavigationBar Commands.

其实它已经开始遏制各类各种视图模型中使用的通用方法。

Actually it has started to contain all kinds of common methods used among various ViewModel.

请建议我如何可以重构这个代码?

Please suggest how can i refactor this code?

推荐答案

防止不得不摆在首位这个基类。这个基类是一个很大的代码味道,结果是你目前的痛苦。这样的基类将违反单一职责原则(SRP),将只是作为一个大的辅助类为所有派生视图模型,或者甚至好像你是把横切关注点在那里。基类甚至可能隐藏您的视图模型违反了SRP的事实。他们可能做的太多; 。有太多的责任

Prevent having this base class in the first place. This base class is a big code smell and the result is your current pain. Such a base class will violate the Single Responsibility Principle (SRP) and will just act as a big helper class for all derived view models, or it even seems that you are putting cross-cutting concerns in there. The base class might even hide the fact that your view models violate the SRP. They probably do too much; have too many responsibilities.

相反,尽量做到以下几点:

Instead, try to do the following:


  • 移动横切关注点出了基类的装饰成或另谋出路应用横切关注点

  • 集团相关的依赖性汇集成的并注入其总服务到您的视图模型。

  • Move cross-cutting concerns out of the base class into decorators or find another way to apply cross-cutting concerns.
  • Group related dependencies together into a aggregate service and inject such aggregate service into your view model.

在一个精心设计的应用程序,几乎没有过需要具有这样的基类,它需要依赖关系。

In a well designed application, there is hardly ever a need for having such base class that takes dependencies.

如果你不能够改变你的设计(但请不要看看它这一点;你会在没有基类一个更好的地方),就可以恢复到明确的财产注入。简单的喷油器不这样做外的开箱,但文档的如何做到这一点。

If you aren't able to change your design (but please do take a look it this; you will be in a much better place without that base class), you can revert to explicit property injection. Simple Injector does not do this out-of-the-box, but the documentation describes how to do this.

基本上,它归结为编写自定义 IPropertySelectionBehavior ,移动 BaseViewModel 来的公共属性和自定义属性标记它们。

Basically, it comes down to writing a custom IPropertySelectionBehavior, moving the constructor dependencies of the BaseViewModel to public properties and marking them with a custom attribute.

但同样,只使用属性注入作为最后的手段。物业注入只会隐藏设计的问题;它不会解决这个问题。

But again, only use property injection as a last resort. Property injection will only hide the design problem; it will not solve it.

这篇关于简单的注射器中注入多的BaseClass依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:49