在使用 CSLA 框架和 WCSF 时实现 Presenter 的单元可测试性

发布于 2024-08-10 06:59:07 字数 171 浏览 4 评论 0原文

WCSF 使用模型视图演示者 (MVP) 模式来组织/构建网站的源代码。当正确使用 MVP 模式时,它提供关注点分离、呈现器逻辑的单元可测试性等。

如何使 WCSF 和 CSLA 框架很好地发挥(协同工作)以实现呈现器逻辑的单元可测试性。为了实现呈现器逻辑的单元可测试性,需要模拟或消除所有数据访问和其他依赖项。

The WCSF uses Model View Presenter (MVP) pattern for organizing/structuring the source code for a website. When MVP pattern is used correctly, it provides seperation of concerns, unit testability of presenter logic, etc.

How to make WCSF and CSLA framework play well (work together) for achieving unit testability of the presenter logic. For achieving the unit testability of the presenter logic it is required that all data access and other dependencies needs to be mocked or stubbed out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2024-08-17 06:59:07

在 CSLA 对象内执行数据门户方法时,这些数据门户方法的内容应调用数据服务来获取和更新该数据。这些 Web 服务应该只是基于接口,以便可以模拟它们。

下面是填充员工对象的示例:

private void DataPortal_Fetch(SingleCriteria<Employee, int> criteria)
{
  IEmployeeDTO data = DataServiceContext.Current.EmployeeDataService.GetEmployee(criteria.Value);
  // Just do left to right copying here
  LoadProperty<int>(IdProperty, data.Id);
}

那么 EmployeeDataService 只是一个接口。我们使用 Ninject 为该接口创建一个具体类,该类将指向您要使用的数据访问技术的另一个程序集。然后,您可以为测试类指定不同的具体类。

以下是示例数据服务接口:

public interface IEmployeeDataService
{
  IEmployeeDTO GetEmployee(int id);
}

以下是使用 Linq 2 SQL 的示例数据服务 concreate 类:

namespace XXX.DataServices.Linq
{
  public class EmployeeDataService : IEmployeeDataService
  {
    public IEmployeeDTO GetEmployee(int id)
    {
      // use Linq to SQL to get the data and transform that class into IEmployeeDTO
    }
  }
}

以下是用于测试的示例数据服务:

namespace XXX.DataServices.Testing
{
  public class IEmployeeDTO GetEmployee(int id)
  {
    // mock up several IEmployeeDTO objects with known data
  }
}

When executing the data portal methods within a CSLA object, the content of those data portal methods should call dataservices to get and update that data. Those web services should just be interface based so that they can be mocked out.

Here is an example of populating an employee object:

private void DataPortal_Fetch(SingleCriteria<Employee, int> criteria)
{
  IEmployeeDTO data = DataServiceContext.Current.EmployeeDataService.GetEmployee(criteria.Value);
  // Just do left to right copying here
  LoadProperty<int>(IdProperty, data.Id);
}

Then EmployeeDataService is just an interface. We use Ninject to create a concreate class for that interface that will point to another assembly for the data access technology you want to use. You can then specify a different concrete class for the test class.

Here is the example data service interface:

public interface IEmployeeDataService
{
  IEmployeeDTO GetEmployee(int id);
}

Here is the example data service concreate class for using Linq 2 SQL:

namespace XXX.DataServices.Linq
{
  public class EmployeeDataService : IEmployeeDataService
  {
    public IEmployeeDTO GetEmployee(int id)
    {
      // use Linq to SQL to get the data and transform that class into IEmployeeDTO
    }
  }
}

Here is the example data service to use for testing:

namespace XXX.DataServices.Testing
{
  public class IEmployeeDTO GetEmployee(int id)
  {
    // mock up several IEmployeeDTO objects with known data
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文