在SOLID应用程序架构中使用Ninject

Mig*_*oso 9 architecture ninject solid-principles asp.net-mvc-3

我从MVC3开始,想要使用一些灵活的架构,所以我读了几十篇博客,一本书(Pro ASP.NET MVC 3),阅读了有关SOLID原理的内容,最后得到了一个我喜欢的应用程序结构(或者至少我是这么认为的,到目前为止,因为我还没有建立任何东西):

在此输入图像描述

在这个结构中:

  • Domain保存POCO类并定义服务接口
  • 服务实现服务接口并定义存储库接口
  • 数据实现了存储库接口
  • WebUI和域使用服务
  • 服务使用存储库
  • WebUI,服务和数据依赖于POCO类的域

Domain using Services的主要原因是验证POCO(IValidatable)类的Validate方法的唯一键.

我开始用这种结构构建一个参考应用程序,但到目前为止,我遇到了两个问题:

  1. 我正在使用Data.Tests项目和存储库的单元测试,但是还没有找到一种方法来注入(使用Ninject)服务的实现(在构造函数中或其他方面),因此Validate方法可以在服务上调用CheckUniqueKey.

  2. 我还没有找到关于将Ninject连接到TEST项目的任何参考(很多用于WebUI项目).

我想在这里实现的目标是通过改变DATA组件,能够从EF切换到其他类似DAPPER的东西.

UPDATE

现在(截至2011年8月9日)Ninject正在工作,但我想我错过了一些东西.

我有一个带有两个构造函数的CustomerRepository:

public class CustomerRepository : BaseRepository<Customer>, ICustomerRepository
{
    // The repository usually receives a DbContext
    public CustomerRepository(RefAppContext context)
        : base(context)
    {
    }

    // If we don't receive a DbContext then we create the repository with a defaulte one
    public CustomerRepository()
        : base(RefApp.DbContext())
    {
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

在TestInitialize上:

// These are for testing the Repository against a test database

[TestInitialize()]
public void TestInitialize()
{
    // Context used for tests
    this.context = new RefAppContext();

    // This is just to make sure Ninject is working, 
    // would have used: repository = new CustomerRepository(context);

    this.kernel = NinjectMVC3.CreateKernel();

    this.kernel.Rebind<ICustomerRepository>().To<CustomerRepository>().WithConstructorArgument("context", context);

    this.repository = kernel.Get<ICustomerRepository>();

}
Run Code Online (Sandbox Code Playgroud)

在Customer类上:

public class Customer : IValidatableObject 
{
    ...

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        // I want to replace this with a "magic" call to ninject
        CustomerRepository rep = new CustomerRepository();

        Customer customer = rep.GetDupReferenceCustomer(this);

        if (customer != null)
            yield return new ValidationResult("Customer \"" + customer.Name + "\" has the same reference, can't duplicate", new [] { "Reference" });
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下使用Ninject的最佳方法是什么?

任何帮助将受到高度赞赏.

答案,排序

到目前为止,我会认为这个问题是有问题的.我可以让Ninject工作,但看起来像实现SOLID的依赖倒置原则(DIP)将花费更多的时间.

在这方面,我不得不将域名,服务和数据混为一谈,我将在其他时间创建另一个问题,并保持项目按照通常的方式进行.

谢谢大家.

Rem*_*oor 3

单元测试应该在没有 Ninject 的情况下完成。只需创建被测对象的实例并手动为每个依赖项注入模拟即可。

对于集成测试,您可以使用内核,包括应用程序引导程序中的所有绑定,并重新绑定您想要用模拟替换的所有内容。例如,将会话绑定替换为使用内存数据存储而不是真实数据库的会话绑定。