簡體   English   中英

使用雙構造函數與使用DI框架的單個構造函數的依賴注入

[英]Dependency injection using double constructors versus single constructor that uses a DI Framework

在單元測試的上下文中,可以創建兩個控制器構造函數,一個默認情況下控制器工廠將使用該構造函數,而另一個專門用於單元測試。

public class ProductController : Controller 
{
  private IProductRepository repository;
  public int PageSize = 10;

  // default constructor
  public ProductController() 
  {
    this.repository = new ProductRepository();
  }

  // dedicated for unit testing
  public ProductController(IProductRepository productRepository) 
  {
    this.repository = productRepository;
  }

  public ViewResult List(int page=1) 
  {
    return View(repository.Products
       .OrderBy(p => p.ProductID)
       .Skip((page - 1) * PageSize)
       .Take(PageSize));
  }
}

單元測試可以通過這種方式實現

[TestMethod]
public void Can_Paginate() 
{
  // Arrange
  Mock<IProductRepository> mock = new Mock<IProductRepository>();
  mock.Setup(m => m.Products).Returns(new Product[] 
  {
    new Product {ProductID = 1, Name = "P1"},
    new Product {ProductID = 2, Name = "P2"},
    new Product {ProductID = 3, Name = "P3"},
    new Product {ProductID = 4, Name = "P4"},
    new Product {ProductID = 5, Name = "P5"}
  }.AsQueryable());

  ProductController controller = new ProductController(mock.Object);
  controller.PageSize = 3;

  // Act
  IEnumerable<Product> result =
  (IEnumerable<Product>)controller.List(2).Model;
  // Assert
  Product[] prodArray = result.ToArray();
  Assert.IsTrue(prodArray.Length == 2);
  Assert.AreEqual(prodArray[0].Name, "P4");
  Assert.AreEqual(prodArray[1].Name, "P5");
}

在上面,我能夠實現單元測試。 我的問題是,如果我可以使用專用的構造函數來實現DI,為什么我會選擇使用DI框架(例如Unity,Ninject等)? 我一定在這里想念一些明顯的東西。

(順便說一句,上面的代碼示例主要來自Adam Freeman的Pro ASP.NET MVC 4書,我對其進行了一些修改以適合需要提出的問題)

在這個簡單的例子中,從技術上講沒有理由。

框架的重點是全局管理依賴關系及其存在的各個部分……而不僅僅是注入依賴關系。 生命周期/范圍,方式/時間,地點/原因。 您在這里擁有的仍然與ProductRepository緊密相關……僅此而已。

在一個復雜的應用程序中,您當前擁有的內容如下所示:

this.repository = new ProductRepository(
                          new ShoppingCartRepository(
                                  new Logger()),
                          new ReviewsRepository(
                                  new Logger()),
                          new Logger());

..而使用DI / IoC框架,則不必擔心任何這種連接以及所有底層的生命周期/連接邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM