concept data access layer in category dependency injection

appears as: data access layer, data access layer, The data access layer, data access layers
Dependency Injection

This is an excerpt from Manning's book Dependency Injection.

To implement the data access layer, Mary adds a new library to her solution. The following listing shows her Product class.

Figure 2.4 Compared to figure 2.3, Mary has now implemented the data access layer and the domain layer. The UI layer still remains to be implemented.

02-04.eps

What Mary doesn’t realize is that by letting the ProductService depend on the data access layer’s CommerceContext class, she tightly coupled her domain layer to the data access layer. We’ll explain what’s wrong with that in section 2.2.

The next listing shows how Mary implements an Index method on her HomeController class to extract the featured products from the database and pass them to the view. To make this code compile, she must add references to both the data access layer and the domain layer. This is because the ProductService class is defined in the domain layer, but the Product class is defined in the data access layer.

Listing 2.4 Index method on the default controller class
public ViewResult Index()
{
    bool isPreferredCustomer =    #1  
        this.User.IsInRole("PreferredCustomer");    #1  

    var service = new ProductService();    #2  

    var products = service.GetFeaturedProducts(    #3  
        isPreferredCustomer);    #3  

    this.ViewData["Products"] = products;    #4  

    return this.View();
}

#1   Determines whether a customer is a preferred customer
#2   Creates ProductService from the domain layer
#3   Gets the list of featured products (defined in the data access layer) from the ProductService
#4   Stores the list of products in the controller’s generic ViewData dictionary for later use by the view
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest