在 ServiceStack Web 服务中设置公共属性

2024-01-09

我正在尝试使用 Sqlite 为 ServiceStack 服务编写单元测试。由于 Sqlite 不支持存储过程,因此我创建了“ServiceCommand”类型的公共属性,它将命令文本和命令类型作为输入。默认情况下,它配置为运行存储过程,并且在编写单元测试用例时,我在调用之前将 SelectCommand 属性重新分配给针对 sqlite 的 sql 查询Any()方法如下。所有测试用例都工作正常。

var request = new CustomerRequest() { name = "alfki"  };
var service = new CustomerService(InMemoryTestDatabase.OpenDbConnection());
service.SelectCommand = new ServiceCommand() { SQL = "SELECT * FROM customers where customerid = {0}" };
var result = service.Any(request);

但据此thread https://stackoverflow.com/questions/15745074/how-to-assign-default-value-to-a-property,在解析引用时,CustomerService 的公共属性被 IOC 设置为 null,因此 SelectCommand 为 nullAny()方法导致对象引用错误。通过将属性设置为受保护、私有、内部或静态,我将无法运行单元测试。

public class CustomerService : Service
{
  private readonly IDbConnection _dbConnection;
  public ServiceCommand SelectCommand {get;set;}

  public CustomerService(IDBConnection dbConnection)
  {        
         _dbConnection = dbConnection;  //injected successfully
         SelectCommand =  new ServiceCommand(){ SQL = "sp_getcustomers",
            CommandType = CommandType.StoredProcedure};          
  }

  public Customer Any(CustomerRequest request)
  {
        //Select command is not accessible here.

  }
}


[Route("/customers")]
public class CustomerRequest
{
    public string name { get; set; }
}

服务命令

public class ServiceCommand
{
    public string SQL { get; set; }        
    public CommandType CommandType { get; set; }

    public ServiceCommand()
    {
        CommandType = CommandType.Text;
    }
}

为了能够运行测试用例和服务,我进行了修改Any()如果 ServiceCommand 为 null,则实例化 ServiceCommand 的方法。我想知道这是否是可行的方法或有更好的选择。

public class CustomerService : Service
{
  private readonly IDbConnection _dbConnection; // injected successfully
  public ServiceCommand SelectCommand {get;set;}

  public CustomerService(IDBConnection dbConnection)
  {        
         _dbConnection = dbConnection;  //injected successfully         
  }

  public Customer Any(CustomerRequest request)
  {
        SelectCommand = SelectCommand ?? new ServiceCommand() { SQL = "sp_getCustomers",CommandType = CommandType.StoredProcedure };

  }
}

Since 服务栈 http://www.servicestack.net/services 将为所有公共属性注入已注册的 IOC 属性,它将覆盖您在构造函数中设置的值,因此如果未在 IOC 中注册,则无法将其设为公共属性,因为它将被覆盖为 null。

鉴于此,一些可能的选择是:

使其成为公共领域

public class CustomerService : Service
{
    public ServiceCommand SelectCommand = new ServiceCommand { 
       SQL = "sp_getcustomers", 
       CommandType = CommandType.StoredProcedure };
    ...
}

使用 setter 注入只读属性

service.SetSelectCommand(
  new ServiceCommand { SQL = "SELECT * FROM customers where customerid = {0}" });

在 IOC 中注册并在构造函数中指定

container.Register(new ServiceCommand { 
   SQL = "sp_getcustomers", 
   CommandType = CommandType.StoredProcedure });

并将构造函数更改为:

public CustomerService(IDBConnection dbConnection, ServiceCommand serviceCommand)
{        
    _dbConnection = dbConnection; 
    _serviceCommand = serviceCommand;    
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ServiceStack Web 服务中设置公共属性 的相关文章

随机推荐