concept Mary in category .net

This is an excerpt from Manning's book Dependency Injection in .NET.
In general, Mary is happy about the work that she’s doing, but it frustrates her that milestones are often being missed, forcing her and her colleagues to work long hours and weekends to meet deadlines. She suspects that there must be more efficient ways to build software. In an effort to learn about efficiency, she buys a lot of programming books—but she rarely has time to read them.
Much of her spare time is spent with her husband and two girls. Mary likes to go hiking in the mountains. She’s also an enthusiastic cook, and she definitely knows how to make a real sauce béarnaise.
Because she’ll need to pull data from a database table, Mary has decided to begin by implementing the data layer. The first step is to define the database table itself. Mary uses SQL Server Management Studio to create the table shown in figure 2.4.
Figure 2.4. Mary creates the Product table using SQL Server Management Studio; alternative approaches include writing a T-SQL script or creating the table through Visual Studio, or by some other means.
![]()
To implement the Data Access Layer, Mary adds a new library to her solution. From Visual Studio, she uses the Entity Data Model Wizard to generate an entity model from the database she just created. She changes a few names to finalize the model, as shown in figure 2.5.
Figure 2.5. The Product entity generated from the Product database table shown in figure 2.4. Mary has changed the name of the Featured column to IsFeatured, as well as changed a few names in the generated ObjectContext (not shown).
![]()
Jens is basing his advice on his technical knowledge of ASP.NET. As the discussion takes him away from his comfort zone, he steamrolls Mary with a triple combo of buzzwords. Be aware that he doesn’t know what he is talking about: he misuses the concept of separation of concerns, completely misses the point with the SINGLE RESPONSIBILITY PRINCIPLE, and he only mentions Agile because he recently heard someone else talk enthusiastically about it.
Armed with Jens’ advice, Mary creates a new C# library project and adds a class called ProductService, shown in the following listing. To make the ProductService class compile, she must add a reference to her Data Access library, because the CommerceObjectContext class is defined there.
Listing 2.1. Mary’s ProductService class
public partial class ProductService { private readonly CommerceObjectContext objectContext; public ProductService() { this.objectContext = new CommerceObjectContext(); } public IEnumerable<Product> GetFeaturedProducts( bool isCustomerPreferred) { var discount = isCustomerPreferred ? .95m : 1; var products = (from p in this.objectContext .Products where p.IsFeatured select p).AsEnumerable(); return from p in products select new Product { ProductId = p.ProductId, Name = p.Name, Description = p.Description, IsFeatured = p.IsFeatured, UnitPrice = p.UnitPrice * discount }; } }Mary is happy that she has encapsulated data access technology (LINQ to Entities), configuration, and domain logic in the ProductService class. She has delegated the knowledge of the user to the caller by passing in the isCustomerPreferred parameter, and she uses this value to calculate the discount for all the products.
Further refinement could include replacing the hard-coded discount value (.95) with a configurable number, but, for now, this implementation will suffice. Mary is almost done—the only thing still left is the User Interface. Mary decides that can wait until the next day.
Figure 2.7 shows how far Mary has come with implementing the architecture envisioned in figure 2.2.
Figure 2.7. At this point, Mary has implemented the Data Access Layer and the Domain Logic Layer. Compared to figure 2.6, the Domain Logic Layer has been added. The User Interface Layer still remains to be implemented.
![]()