Domanda

I'm reading M. Seemann book Dependency Injection in .NET, chapter 2. Source code available here: http://www.manning.com/seemann/

In ComplexCommerce solution, CommerceWebPresentationModel, HomeController we have repository passed into constructor:

public HomeController(ProductRepository repository, CurrencyProvider currencyProvider)

which is then passed to newly created service!

    public ViewResult Index()
    {
        var currencyCode = this.CurrencyProfileService.GetCurrencyCode();
        var currency = this.currencyProvider.GetCurrency(currencyCode);

        var productService =
            new ProductService(this.repository);

However, in BasketController we have a service passed into constructor, to which BasketRepository is injected.

    public BasketController(IBasketService basketService,
        CurrencyProvider currencyProvider)

As far as I can see ProductService does not implement any interface and that makes HomeController untestable.

My questions is: Why ProductService is instantiated without help of DI? Am I missing something? Or is it something the author missed (in a really good book otherwise!)?

È stato utile?

Soluzione

Hopefully, this discussion thread throws some more light on the matter.

In short, it served as an example, but isn't one of the highlights of the book. I would typically not write code like that on purpose in real production code bases.

However, I disagree that HomeController is untestable, because I wrote the entire code base for the book using Test-Driven Development. You can download the code for the book and verify that it's covered by tests. However, I do agree that HomeController is more difficult to unit test than it could have been.

For the record, I make mistakes while coding, just like anyone else...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top