构建可测试的业务层逻辑

本文关键字:业务 测试 构建 | 更新日期: 2023-09-27 18:30:10

我正在.net/c#/Entity Framework中构建一个使用分层体系结构的应用程序。应用程序与外部世界的接口是WCF服务层。在这一层之下,我有BL、共享库和DAL。

现在,为了使我的应用程序中的业务逻辑可测试,我试图引入关注点分离、松耦合和高内聚性,以便能够在测试时注入依赖关系。

我需要一些指针来判断我下面描述的方法是否足够好,或者我是否应该进一步解耦代码。

以下代码片段用于使用动态linq查询数据库。我需要使用动态linq,因为直到运行时我才知道表的名称或要查询的字段。代码首先将json参数解析为类型对象,然后使用这些参数构建查询,最后执行查询并返回

以下是在下面的测试中使用的GetData函数

IQueryHelper helper = new QueryHelper(Context.DatabaseContext);
//1. Prepare query
LinqQueryData queryData = helper.PrepareQueryData(filter);
//2. Build query
IQueryable query = helper.BuildQuery(queryData);
//3. Execute query
List<dynamic> dalEntities = helper.ExecuteQuery(query);

以下是DAL中查询助手类的高级定义及其接口

public interface IQueryHelper
{
   LinqQueryData PrepareQueryData(IDataQueryFilter filter);
   IQueryable BuildQuery(LinqQueryData queryData);
   List<dynamic> ExecuteQuery(IQueryable query);
}
public class QueryHelper : IQueryHelper
{  
  ..
  ..
}

以下是使用上述逻辑的测试。测试构造函数将模拟数据库注入Context.DatabaseContext

[TestMethod]
public void Verify_GetBudgetData()
{
  Shared.Poco.User dummyUser = new Shared.Poco.User();
  dummyUser.UserName = "dummy";
  string groupingsJSON = "['"1'",'"44'",'"89'"]";
  string valueTypeFilterJSON = "{1:1}";
  string dimensionFilter = "{2:['"200'",'"300'"],1:['"3001'"],44:['"1'",'"2'"]}";
  DataQueryFilter filter = DataFilterHelper.GetDataQueryFilterByJSONData(
    new FilterDataJSON()
    {
      DimensionFilter = dimensionFilter,  
      IsReference = false,
      Groupings = groupingsJSON, 
      ValueType = valueTypeFilterJSON
    }, dummyUser);
    FlatBudgetData data = DataAggregation.GetData(dummyUser, filter);
    Assert.AreEqual(2, data.Data.Count);
    //min value for january and february
    Assert.AreEqual(50, Convert.ToDecimal(data.Data.Count > 0 ? data.Data[0].AggregatedValue : -1));
}

回答我的问题

  1. 这种业务层逻辑是否"足够好",或者还可以做些什么来实现松耦合、高内聚性和可测试代码
  2. 我应该在构造函数中注入要查询的数据上下文吗?请注意,QueryHelper定义位于DAL中。代码使用它的位于BL

请让我知道我是否应该张贴额外的代码,以澄清。我最感兴趣的是接口IQueryHelper是否足够。。

构建可测试的业务层逻辑

我通常使用IServices、Services和MockServices。

  • IServices提供了所有业务逻辑都必须调用其方法的可用操作
  • 服务是我的代码注入视图模型(即实际数据库)的数据访问层
  • MockServices是我的单元测试注入到视图模型中的数据访问层(即模拟数据)

I服务:

public interface IServices
{
    IEnumerable<Warehouse> LoadSupply(Lookup lookup);
    IEnumerable<Demand> LoadDemand(IEnumerable<string> stockCodes, int daysFilter, Lookup lookup);
    IEnumerable<Inventory> LoadParts(int daysFilter);
    Narration LoadNarration(string stockCode);
    IEnumerable<PurchaseHistory> LoadPurchaseHistory(string stockCode);
    IEnumerable<StockAlternative> LoadAlternativeStockCodes();
    AdditionalInfo GetSupplier(string stockCode);
}

模拟服务:

public class MockServices : IServices
{
    #region Constants
    const int DEFAULT_TIMELINE = 30;
    #endregion
    #region Singleton
    static MockServices _mockServices = null;
    private MockServices()
    {
    }
    public static MockServices Instance
    {
        get
        {
            if (_mockServices == null)
            {
                _mockServices = new MockServices();
            }
            return _mockServices;
        }
    }
    #endregion
    #region Members
    IEnumerable<Warehouse> _supply = null;
    IEnumerable<Demand> _demand = null;
    IEnumerable<StockAlternative> _stockAlternatives = null;
    IConfirmationInteraction _refreshConfirmationDialog = null;
    IConfirmationInteraction _extendedTimelineConfirmationDialog = null;
    #endregion
    #region Boot
    public MockServices(IEnumerable<Warehouse> supply, IEnumerable<Demand> demand, IEnumerable<StockAlternative> stockAlternatives, IConfirmationInteraction refreshConfirmationDialog, IConfirmationInteraction extendedTimelineConfirmationDialog)
    {
        _supply = supply;
        _demand = demand;
        _stockAlternatives = stockAlternatives;
        _refreshConfirmationDialog = refreshConfirmationDialog;
        _extendedTimelineConfirmationDialog = extendedTimelineConfirmationDialog;
    }
    public IEnumerable<StockAlternative> LoadAlternativeStockCodes()
    {
        return _stockAlternatives;
    }
    public IEnumerable<Warehouse> LoadSupply(Lookup lookup)
    {
        return _supply;
    }
    public IEnumerable<Demand> LoadDemand(IEnumerable<string> stockCodes, int daysFilter, Syspro.Business.Lookup lookup)
    {
        return _demand;
    }
    public IEnumerable<Inventory> LoadParts(int daysFilter)
    {
        var job1 = new Job() { Id = Globals.jobId1, AssembledRequiredDate = DateTime.Now, StockCode = Globals.stockCode100 };
        var job2 = new Job() { Id = Globals.jobId2, AssembledRequiredDate = DateTime.Now, StockCode = Globals.stockCode200 };
        var job3 = new Job() { Id = Globals.jobId3, AssembledRequiredDate = DateTime.Now, StockCode = Globals.stockCode300 };
        return new HashSet<Inventory>()
        {
            new Inventory() { StockCode = Globals.stockCode100, UnitQTYRequired = 1, Category = "Category_1", Details = new PartDetails() { Warehouse = Globals.Instance.warehouse1, Job = job1} },
            new Inventory() { StockCode = Globals.stockCode200, UnitQTYRequired = 2, Category = "Category_1", Details = new PartDetails() { Warehouse = Globals.Instance.warehouse1, Job = job2} },
            new Inventory() { StockCode = Globals.stockCode300, UnitQTYRequired = 3, Category = "Category_1", Details = new PartDetails() { Warehouse = Globals.Instance.warehouse1, Job = job3} },
        };
    }
    #endregion
    #region Selection
    public Narration LoadNarration(string stockCode)
    {
        return new Narration()
        {
            Text = "Some description"
        };
    }
    public IEnumerable<PurchaseHistory> LoadPurchaseHistory(string stockCode)
    {
        return new List<PurchaseHistory>();
    }
    public AdditionalInfo GetSupplier(string stockCode)
    {
        return new AdditionalInfo()
        {
            SupplierName = "Some supplier name"
        };
    }
    #endregion
    #region Creation
    public Inject Dependencies(IEnumerable<Warehouse> supply, IEnumerable<Demand> demand, IEnumerable<StockAlternative> stockAlternatives, IConfirmationInteraction refreshConfirmation = null, IConfirmationInteraction extendedTimelineConfirmation = null)
    {
        return new Inject()
        {
            Services = new MockServices(supply, demand, stockAlternatives, refreshConfirmation, extendedTimelineConfirmation),
            Lookup = new Lookup()
            {
                PartKeyToCachedParts = new Dictionary<string, Inventory>(),
                PartkeyToStockcode = new Dictionary<string, string>(),
                DaysRemainingToCompletedJobs = new Dictionary<int, HashSet<Job>>(),
.
.
.
            },
            DaysFilterDefault = DEFAULT_TIMELINE,
            FilterOnShortage = true,
            PartCache = null
        };
    }
    public List<StockAlternative> Alternatives()
    {
        var stockAlternatives = new List<StockAlternative>() { new StockAlternative() { StockCode = Globals.stockCode100, AlternativeStockcode = Globals.stockCode100Alt1 } };
        return stockAlternatives;
    }
    public List<Demand> Demand()
    {
        var demand = new List<Demand>()
        {
            new Demand(){ Job = new Job{ Id = Globals.jobId1, StockCode = Globals.stockCode100, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode100, RequiredQTY = 1}, 
            new Demand(){ Job = new Job{ Id = Globals.jobId2, StockCode = Globals.stockCode200, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode200, RequiredQTY = 2}, 
        };
        return demand;
    }
    public List<Warehouse> Supply()
    {
        var supply = new List<Warehouse>() 
        { 
            Globals.Instance.warehouse1, 
            Globals.Instance.warehouse2, 
            Globals.Instance.warehouse3,
        };
        return supply;
    }
    #endregion
}

服务:

public class Services : IServices
{
    #region Singleton
    static Services services = null;
    private Services()
    {
    }
    public static Services Instance
    {
        get
        {
            if (services == null)
            {
                services = new Services();
            }
            return services;
        }
    }
    #endregion
    public IEnumerable<Inventory> LoadParts(int daysFilter)
    {
        return InventoryRepository.Instance.Get(daysFilter);
    }
    public IEnumerable<Warehouse> LoadSupply(Lookup lookup)
    {
        return SupplyRepository.Instance.Get(lookup);
    }
    public IEnumerable<StockAlternative> LoadAlternativeStockCodes()
    {
        return InventoryRepository.Instance.GetAlternatives();
    }
    public IEnumerable<Demand> LoadDemand(IEnumerable<string> stockCodes, int daysFilter, Lookup lookup)
    {
        return DemandRepository.Instance.Get(stockCodes, daysFilter, lookup);
    }
.
.
.

单元测试:

    [TestMethod]
    public void shortage_exists()
    {
        // Setup
        var supply = new List<Warehouse>() { Globals.Instance.warehouse1, Globals.Instance.warehouse2, Globals.Instance.warehouse3 };
        Globals.Instance.warehouse1.TotalQty = 1;
        Globals.Instance.warehouse2.TotalQty = 2;
        Globals.Instance.warehouse3.TotalQty = 3;
        var demand = new List<Demand>()
        {
            new Demand(){ Job = new Job{ Id = Globals.jobId1, StockCode = Globals.stockCode100, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode100, RequiredQTY = 1}, 
            new Demand(){ Job = new Job{ Id = Globals.jobId2, StockCode = Globals.stockCode200, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode200, RequiredQTY = 3}, 
            new Demand(){ Job = new Job{ Id = Globals.jobId3, StockCode = Globals.stockCode300, AssembledRequiredDate = DateTime.Now}, StockCode = Globals.stockCode300, RequiredQTY = 4}, 
        };
        var alternatives = _mock.Alternatives();
        var dependencies = _mock.Dependencies(supply, demand, alternatives);
        var viewModel = new MainViewModel();
        viewModel.Register(dependencies);
        // Test
        viewModel.Load();
        AwaitCompletion(viewModel);
        // Verify
        var part100IsNotShort = dependencies.PartCache.Where(p => (p.StockCode == Globals.stockCode100) && (!p.HasShortage)).Single() != null;
        var part200IsShort = dependencies.PartCache.Where(p => (p.StockCode == Globals.stockCode200) && (p.HasShortage)).Single() != null;
        var part300IsShort = dependencies.PartCache.Where(p => (p.StockCode == Globals.stockCode300) && (p.HasShortage)).Single() != null;
        Assert.AreEqual(true, part100IsNotShort &&
                                part200IsShort &&
                                part300IsShort);
    }

CodeBehnd:

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += (s, e) =>
            {
                this.viewModel = this.DataContext as MainViewModel;
                var dependencies = GetDependencies();
                this.viewModel.Register(dependencies);
.
.
.

视图模型:

    public MyViewModel()
    {
.
.
.
    public void Register(Inject dependencies)
    {
        try
        {
            this.Injected = dependencies;
            this.Injected.RefreshConfirmation.RequestConfirmation += (message, caption) =>
                {
                    var result = MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);
                    return result;
                };
            this.Injected.ExtendTimelineConfirmation.RequestConfirmation += (message, caption) =>
                {
                    var result = MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);
                    return result;
                };
.
.
.
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.GetBaseException().Message);
        }
    }