简体   繁体   English

在C#中的静态对象中保存类实例

[英]Holding a class instance in a Static object in C#

We have a class " DataAccessServiceConnector ", in which we have few methods to communicate with Data Access Service. 我们有一个“ DataAccessServiceConnector ”类,在其中没有几种与数据访问服务进行通信的方法。

public class DataAccessServiceConnector: IDataAccessServiceConnector
{
     public async Task<HttpResponseMessage> GetDataAccessServiceResponse()
     {
        //Some code
        return GetDataFromDataAccessService();
     }        
}

We have an Interface: 我们有一个接口:

public interface IDataAccessServiceConnector
{
    Task<HttpResponseMessage> GetDataAccessServiceResponse();
}

And having a different class, that is holding the instance of "DataAccessServiceConnector" class in the as static object. 并且具有不同的类,该类将“ DataAccessServiceConnector”类的实例作为静态对象保存。

public class ClassA
{
  public static IDataAccessServiceConnector DataAccessConnector;
  //Constructor of the Class
  ClassA()
  {
     DataAccessConnector = DataAccessConnector ?? new DataAccessServiceConnector();
  }
}

Is it bad practice to hold the class instance (ie DataAccessServiceConnector ) in a static object(ie DataAccessConnector )? 将类实例(即DataAccessServiceConnector )保存在静态对象(即DataAccessConnector )中是不好的做法吗?

I think, the answer to your question is opinion-based in context of StackOverflow. 我认为,您的问题的答案是基于StackOverflow的基于意见的。

This is a classic "Is Singleton Pattern Bad?" 这是经典的单例模式不好吗?” question: 题:


There are numerous issues with Singletons in particular and with shared-write-access memory in general. 特别是Singletons以及通常的共享写访问内存有很多问题。

  • As others mentioned above, sharing resources requires extreme caution; 如上所述,共享资源需要格外谨慎。
  • Similarly, [unit] testing of such class is not easy; 同样,此类的[单元]测试也不容易;
  • The (inter)dependencies between classes are less discoverable; 类之间的(相互)依赖性很少被发现;
  • The code is harder to follow; 该代码很难遵循。
  • The bugs are harder to fix; 这些错误很难修复。
  • Code refactoring becomes harder in certain cases; 在某些情况下,代码重构变得更加困难。
  • ...this list goes and goes on. ...这个清单不胜枚举。

I personally decided to use Singletons as a last resort solution only. 我个人决定仅将Singletons用作最后的解决方案。 Even then, I would rely on a dependency injection framework's singleton registration instead of a static field. 即使那样,我仍将依赖于依赖项注入框架的单例注册,而不是static字段。 (Eg SimpleInjector's singleton registration .) (例如, SimpleInjector的单例注册 。)

But before registering something as a Singleton, please reconsider using regular objects first. 但是在将某些内容注册为Singleton之前,请先重新考虑使用常规对象。

Option 1. 选项1。

If there is already a top level object such as 'Program' or 'Application' that everyone already has access to, you can make the instance object a new field or property on it. 如果已经存在每个人都可以访问的顶级对象(例如“程序”或“应用程序”),则可以将实例对象设为新的字段或属性。

Option 2. 选项2。

Provide static access to the instance object, from the instance object, as a static method/field/property. 提供从实例对象到实例对象的静态访问,作为静态方法/字段/属性。

Or, as you have described, provide static access to the instance object from some other arbitrary type, if doing so is convenient for your design. 或者,如上所述,如果这样做对您的设计方便,则可以从其他任意类型提供对实例对象的静态访问。 The class heirarchy as you have described it sounds perfectly normal to me, actually. 实际上,您所描述的类层次结构对我来说听起来很正常。

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

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