Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在同一类中的方法之间共享变量_C#_Unit Testing - Fatal编程技术网

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

C# 在同一类中的方法之间共享变量,c#,unit-testing,C#,Unit Testing,我计划将验证转移到一个单独的功能中,以实现粒度和易于维护的单元测试。然而,我需要在Validate方法中访问数据库的一些变量在Process方法中重用,并确保它是单元可测试的 当前实施 interface ICustomerService { void Process(); } public class CustomerService: ICustomerService { ICustomerDbService db; public CustomerService(ICustom

我计划将验证转移到一个单独的功能中,以实现粒度和易于维护的单元测试。然而,我需要在Validate方法中访问数据库的一些变量在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();
}
未来实施

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();
}

这是我想要的模式,最接近你想要做的

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 
  }
}
似乎你的客户服务有不止一项职责 -验证客户 -过程验证结果

你可以介绍三个有责任感的课程 -客户验证 -处理客户数据 -结合验证和处理

并使验证方法返回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);

您可以引入抽象接口,而不是实际实现验证和流程类,这使您有可能替换不同的验证实现,并编写单元测试来测试服务类的实际逻辑,即组合验证和流程逻辑。

服务将从调用类接收最小参数,即,customerId,需要在此基础上在服务中执行处理。一次只能验证一个客户吗?现在Validate返回一个字符串,而不是bool@yinnonsanders,是,仅验证和处理一个客户。@这似乎是一个。你想要达到的最终目标是什么?此外,目前的问题还不清楚,因为它是不完整的。阅读并提供一个可以用来更好地理解您的问题的方法。如果不调用Validate调用Process是无效的,反之亦然,那么我不会将它们都公开,即使是为了单元测试目的。这是一种更可靠的方法。在提出这个建议之前,我一直在等待OP提供一个合适的mcve。