concept BasketController in category .net

This is an excerpt from Manning's book Dependency Injection in .NET.
To further complicate matters, I need an ASP.NET MVC controller called BasketController that wraps around the IBasketService interface that I again inject into it via its constructor:
You want the currency conversion feature on all pages that display prices, so you need it in both the HomeController and the BasketController. Because both implementations are quite similar, I’ll only show you the BasketController.
A CurrencyProvider is likely to represent an out-of-process resource, such as a web service or a database that can supply conversion rates. This means that it would be most fitting to implement a concrete CurrencyProvider in a separate project (such as a Data Access library). Hence, there’s no reasonable LOCAL DEFAULT. At the same time, the BasketController class will need a CurrencyProvider to be present; CONSTRUCTOR INJECTION is a good fit. The following listing shows how the CurrencyProvider DEPENDENCY is injected into the BasketController.
Because the BasketController class already had a DEPENDENCY on IBasketService, you add the new CurrencyProvider DEPENDENCY as a second constructor argument
and then follow the same sequence outlined in listing 4.1: Guard Clauses guarantee that the DEPENDENCIES aren’t null
, which means it’s safe to store them for later use
in read-only fields
.
Now that the CurrencyProvider is guaranteed to be present in the BasketController, it can be used from anywhere—for example, in the Index method:
In section 4.1.4, I started adding currency conversion functionality to the sample commerce application, and I briefly showed you some of the implementation of the BasketController’s Index method—but glossed over the appearance of a CurrencyProfileService. Here’s the deal:
In ASP.NET MVC (and ASP.NET in general), you have a well-known piece of infrastructure that deals with such a scenario: the Profile service. An excellent LOCAL DEFAULT implementation of CurrencyProfileService is one that wraps around the ASP.NET Profile service and provides the necessary functionality defined by the GetCurrencyCode and UpdateCurrencyCode methods. The BasketController will use this DefaultCurrencyProfileService as the default while exposing a property that will allow the caller to substitute it by something else.
The DefaultCurrencyProfileService itself uses CONSTRUCTOR INJECTION because it requires access to the HttpContext, and because the HttpContext isn’t available to the BasketController at creation time, it has to defer creation of the DefaultCurrencyProfileService until the property is requested for the first time. In this case, lazy initialization
is required, but in other cases, the LOCAL DEFAULT could have been assigned in the constructor. Notice that the LOCAL DEFAULT is assigned through the public setter, which ensures that all the Guard Clauses get evaluated.
and then follow the same sequence outlined in
, which means it’s safe to store them for later use
in read-only fields
.