实体和依赖注入

时间:2015-12-02 15:56:33

标签: c# dependency-injection

我有这样的服务和一个响应实体:

public class MyService : IMyService
{
   private readonly IService1 _service1;
   private readonly IService2 _service2;

   public MyService(IService1 service1, IService2 service2)
   {
      _service1 = service1;
      _service2 = service2;
   }

   public Response method1()
   {
      //DO SOMETHING
      return new Response() { ... };
   }

   public Response method2()
   {
      //DO SOMETHING
      return new Response() { ... };
   }
}

//Entities folder
public class Response
{
   public bool Success { get; set; }
   public string ErrorMessage { get; set; }
}

有人试图说服我应该像这样注入Response实体:

public class MyService : IMyService
{
   private readonly IService1 _service1;
   private readonly IService2 _service2;
   private readonly IResponse _response;

   public MyService(IService1 service1, IService2 service2, IResponse response)
   {
      _service1 = service1;
      _service2 = service2;
      _response = response;
   }

   public Response method1()
   {
      //DO SOMETHING
      return _response() { ... };
   }

   public Response method2()
   {
      //DO SOMETHING
      return _response() { ... };
   }
}

public class Response : IResponse
{
   public bool Success { get; set; }
   public string ErrorMessage { get; set; }
}

我确信第一个解决方案是正确的,但我想确定一下。任何人都可以给我一个意见和理由吗?感谢

0 个答案:

没有答案