简体   繁体   English

有没有更好的方法来实现依赖关系?

[英]Is there a better way to implement dependencies?

Below is my use case: 以下是我的用例:

Employee Management is a separate C# class library project. 员工管理是一个单独的C#类库项目。 And there is a setting for Employee Management at application level. 在应用程序级别有一个“员工管理”设置。 It means there is a global Boolean variable isEmployeeManagement 这意味着存在一个全局布尔变量isEmployeeManagement

If the variable is true, then all the code related with Management used across whole application should get executed for eg 如果变量为true,则应在整个应用程序中使用与管理相关的所有代码,例如

IEmployeeManagement.AddUser();
IEmployeeManagement.DeleteUser();

But if the setting is false then; 但是如果设置为false,那么; the below lines should not through any exception. 以下几行不应有任何例外。 One of the approaches I thought of is like checking the condition. 我想到的方法之一就是检查条件。

If (isEmployeeManagement)
  IEmployeeManagement.AddUser();

But I want to achieve this using Interfaces without putting any code in the condition. 但是我想使用Interfaces来实现这一目标,而无需在其中添加任何代码。 So if the setting is false, the above code should not through any error. 因此,如果设置为false,则上面的代码不应出现任何错误。

Appreciate if you could help me with the implementation. 感谢您是否可以帮助我进行实施。

So if you have the interface IEmployeeManagement: 因此,如果您具有IEmployeeManagement接口:

public interface IEmployeeManagement {
    void AddUser();
}

And then you have your class that implements it: 然后,您就有了实现它的类:

public class RealManagement : IEmployeeManagement {
    public void AddUser() {  do lots of stuff  }
}

Instead of having an if condition for each AddUser call, make another interface implementation: 而不是为每个AddUser调用设置if条件,而是进行另一个接口实现:

public class VoidManagement : IEmployeeManagement {
    public void AddUser() {  do nothing  }
}

This way you won't need to do if conditions on each method call, and instead you will do it only on class initialization. 这样,您无需在每个方法调用上都执行条件,而是仅在类初始化时执行。

If you use some kind of dependency injection framework, you can have that logic set only once instead, of on each class creation. 如果使用某种依赖注入框架,则只能在每个类创建时都设置一次该逻辑。

If the setting is on entire application level, for all users, you could have something like this 如果设置是在整个应用程序级别上,则对于所有用户,您可能会有类似这样的信息

Startup.cs(assuming .net core): Startup.cs(假设.net核心):

public void ConfigureServices(IServiceCollection services)
        {
            if(Configuration["isEmployeeManagement"] == "true") {
                services.AddTransient<IEmployeeManagement>(RealManagement);
            } else {
                services.AddTransient<IEmployeeManagement>(VoidManagement);
            }
        }

How to exactly do initialization of correct instance changes based on how you get that isEmployeeManagement, what is the lifespan of the service, and what DI library you are using, if using anything. 如何根据获得的isEmployeeManagement,服务的寿命以及所使用的DI库(如果使用)来准确地初始化正确的实例更改。 But the overall idea is same - two implementations of interface, call the methods where you need to, but use some kind of logic to have correct initialization of the instance. 但是总体思路是相同的-接口的两种实现在需要的地方调用方法,但使用某种逻辑对实例进行正确的初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM