在同一类中的方法之间共享变量



我计划将验证移动到一个单独的函数中,以实现粒度和易于维护的单元测试。但是,我需要在 Process 方法中重用一些在验证方法中命中数据库的变量,并确保它是单元可测试的。

当前实施情况

interface ICustomerService
{
   void Process();
}
public class CustomerService: ICustomerService
{
  ICustomerDbService db;
  public CustomerService(ICustomerDbService customerDbService)
  {
     db = customerDbService;
  }

  public void Process()
  {
    //validations
    var customer = db.GetCustomer(customerId);
    if(customer == null)
      return "Not found";
    //processing
  }
}
//Usage
ICustomerService service = new CustomerService(dbService);
service.Process();

未来的实施

interface ICustomerService
{
   bool Validate(int customerId);
   void Process();
}
public class CustomerService: ICustomerService
{
  ICustomerDbService db;
  public CustomerService(ICustomerDbService customerDbService)
  {
     db = customerDbService;
  }
  public bool Validate(int customerId)
  {
    var customer = db.GetCustomer(customerId);
    if(customer == null)
      return "Not found";
    //other processing with multiple(3-4) common variables 
  }
  public void Process()
  {
    var customer = db.GetCustomer(customerId); // How to avoid this call
  }
}
//Usage
ICustomerService service = new CustomerService(dbService);
bool isValid = service.Validate(10)
if(isValid)
{
   service.Process();
}

似乎你的CustomerService有不止一个职责
- 验证客户
- 过程验证结果

您可以介绍三个具有职责的
类 - 客户验证
- 处理客户数据
- 结合验证和处理

并使Validation方法返回Process方法所需的数据,这使您有可能具有分离的逻辑,并使您免于共享类的状态/变量的可能"问题"。

public class CustomerValidation
{
    public Customer Validate(int customerId)
    {
        // Validation logic which "produce" instance of customer by given id
        return customer;
    }
}
public class CustomerProcess
{
    public void Process(Customer customer)
    {
        // Process given customer
    }
}
public class CustomerService
{
    private CustomerValidation _validation;
    private CustomerProcess _process;
    public CustomerService(CustomerValidation validation, CustomerProcess process)
    {
        _validation = validation;
        _process = process;
    }
    public void DoStaff(int customerId)
    {
        var customer = _validation.Validate(customerId);
        if (customer != null)
        {
            _process.Process(customer);
        }
    }
}

将像这样使用

var validation = new CustomerValidation();
var process = new CustomerProcess();
var service = new CustomerService(validation, process);
service.DoStaff(customerId);

您可以引入抽象(接口),而不是Validation类和Process类的"实际"实现,这使您可以替换不同的验证实现并编写单元测试来测试 Service 类的实际逻辑 - 它结合了验证和处理逻辑。

这是我选择的模式,它最接近你似乎想要做的事情

public class CustomerService: ICustomerService
{
  ICustomerDbService db;
  public CustomerService(ICustomerDbService customerDbService)
  {
     db = customerDbService;
  }
  public bool Validate(int customerId)
  {
    var customer = db.GetCustomer(customerId);
    return Validate(customer);
  }
  public void Process(int customerId)
  {
    var customer = db.GetCustomer(customerId);
    if(Validate(customer))
    {
      //do processing...
    }
  }
  private bool Validate(Customer customer, /*other args*/)
  {
    if(customer == null)
      return "Not found";
    //other processing with multiple(3-4) common variables 
  }
}

最新更新