首页 > 解决方案 > 依赖注入过多

问题描述

我目前有一个大约 40 个依赖注入的类。这是一个难以维护和单元测试。我不确定一个好方法。

该代码适用于需要处理的任何类型的申请流程(新许可证、许可证更新、学生注册……),大约有 80 种不同类型的申请,与每种申请类型相关联的部分由数据库表。

我有一个包含所有可能属性的类,除了列出的属性之外还有几个,但你应该明白这一点。每个属性都有自己的一组属性,这些属性是基本数据类型或指向其他类的对象。

class Application
{
        [JsonProperty(PropertyName = "accounting")]
        public Accounting Accounting { get; set; }
   
        [JsonProperty(PropertyName = "application")]
        public Application Application { get; set; }

        [JsonProperty(PropertyName = "applicationType")]
        public ApplicationType ApplicationType { get; set; }
   
        [JsonProperty(PropertyName = "document")]
        public List<Attachment> Document { get; set; }

        [JsonProperty(PropertyName = "employment")]
        public List<Employment> Employment { get; set; }

        [JsonProperty(PropertyName = "enrollment")]
        public Enrollment Enrollment { get; set; }
       
        [JsonProperty(PropertyName = "individualAddressContact")]
        public IndividualAddressContact IndividualAddressContact { get; set; }

        [JsonProperty(PropertyName = "instructors")]
        public List<Instructor> Instructors { get; set; }

        [JsonProperty(PropertyName = "license")]
        public License License { get; set; }

        [JsonProperty(PropertyName = "licenseRenewal")]
        public LicenseRenewal LicenseRenewal { get; set; }
   
        [JsonProperty(PropertyName = "MilitaryService")]
        public List<MilitaryService> MilitaryService { get; set; }

        [JsonProperty(PropertyName = "paymentDetail")]
        public PaymentDetail PaymentDetail { get; set; }

        [JsonProperty(PropertyName = "photo")]
        public List<Attachment> Photo { get; set; }

        [JsonProperty(PropertyName = "portal")]
        public Portal Portal { get; set; }
   
        [JsonProperty(PropertyName = "section")]
        public List<Section> Section { get; set; }

        [JsonProperty(PropertyName = "testingCalendar")]
        public TestingCalendar TestingCalendar { get; set; }

        [JsonProperty(PropertyName = "testingScore")]
        public List<TestingScore> TestingScore { get; set; }

        [JsonProperty(PropertyName = "USCitizen")]
        public USCitizen USCitizen { get; set; }
}

因此,此类使用 Web API 发送/接收到 Angular 10 前端。

当请求应用程序时,会启动部分和不同的属性,如果应用程序已启动,则将重新加载进度。因此,可能会从数据库中提取一些属性并将其发送到 Angular 应用程序。

所以我有一些东西,比如

Load(applicationTypeId, applicationId)
{
  Get the sections for the application type
  For each section in the sections
     switch sectionid
        case Documents
           Load all of the documents required for the application type and get any documents uploaded
        case Accounting
           Load the payment details, if no payment made calculate the payment
        case IndividualAddressContact
           Load the person name/address/contact and set a few defaults if the person hasn't started.
        .....
  next
}

Save()
{
     Save the application
     switch current section
        case Documents
           Save all of the documents for the application
        case Accounting
           Save the payment details for the application
        case IndividualAddressContact
           Save the person name/address/contact for the application
        .....       
     get the next section
     Update the application current section
}

我已将交换机中的所有项目放入它们自己的类中,但最后我仍然有 1 分用于序列化/反序列化,并且最终仍然注入了许多依赖项。创建一个包含 40 多个依赖项的单元测试似乎很难维护,并且在从数据库请求和加载应用程序之前,我不知道哪些属性将/不会使用。我不确定如何绕过开关,而不必在某个时间点将所有依赖项注入到 1 个类中。

我将不胜感激有关如何解决此问题的一些想法。

标签: c#asp.net-core-webapi

解决方案


“我目前有一堂大约有 40 次依赖注入的课程......” - 哦,我的天哪!

“这是一个难以维护和单元测试......” - 至少我不怀疑!

建议重构:

  1. 创建一个管理“应用程序”的类(例如“ApplicationManager”)。

  2. 创建一个抽象类“应用程序”。

    “抽象类”相对于“接口”的一个优势是您可以将“通用代码”放在抽象基类中。

  3. 为每个“应用程序”创建一个具体的子类:public class NewLicense : Application、、public class LicenseRenewal : Application等等。

... 和 ...

  1. 将 DI主要用于每个具体类所需的那些“服务” 。

    我敢打赌,您的各个具体类的构造函数只需要注入三个或四个服务……而不是 40 个。谁知道呢?也许您的基类根本不需要任何 DI。

这实际上是我们在我们的一个生产系统中实际使用的设计。这很简单; 它很健壮;它很灵活。它对我们很有效:)


推荐阅读