29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 7-4. Creating the Product class<br />

You are already familiar with the definition of the Product class, as I am going to use the one you saw in the previous<br />

chapters. Edit the Product.cs class file so that it matches Listing 7-3.<br />

Listing 7-3. The Contents of the Product.cs File<br />

namespace SportsStore.Domain.Entities {<br />

}<br />

public class Product {<br />

public int ProductID { get; set; }<br />

public string Name { get; set; }<br />

public string Description { get; set; }<br />

public decimal Price { get; set; }<br />

public string Category { get; set; }<br />

}<br />

I am following the technique of defining my domain model in a separate Visual Studio project, which means that the class must<br />

be marked as public. You do not need to follow this convention, but I find that it helps keep the model separate from the<br />

controllers, which is useful in large and complex projects.<br />

Creating an Abstract Repository<br />

I need some way of getting Product entities from a database. As I explained in Chapter 3, the model includes the persistence<br />

logic for storing and retrieving the data from the persistent data store, but even within the model, I want to keep a degree of<br />

separation between the data model entities and the storage and retrieval logic, which I achieve using the repository pattern. I will<br />

not worry about how I am going to implement data persistence for the moment, but I will start the process of defining an<br />

interface for it.<br />

Create a new top-level folder inside the SportsStore.Domain project called Abstract and, within the new folder,<br />

a new interface file called IProductsRepository.cs, the contents of which Listing 7-4 shows. You can add a new<br />

interface by right-clicking the Abstract folder, selecting Add New Item, and selecting the Interface template.<br />

Listing 7-4. The Contents of the IProductRepository.cs File<br />

using System.Collections.Generic;<br />

using SportsStore.Domain.Entities;<br />

namespace SportsStore.Domain.Abstract {<br />

public interface IProductRepository {<br />

}<br />

}<br />

IEnumerable Products { get; }<br />

This interface uses IEnumerable to allow a caller to obtain a sequence of Product objects, without saying how<br />

or where the data is stored or retrieved. A class that depends on the IProductRepository interface can obtain<br />

Product objects without needing to know anything about where they are coming from or how the implementation class will<br />

deliver them. This is the essence of the repository pattern. I will revisit the IProductRepository interface throughout<br />

the development process to add features.<br />

Making a Mock Repository<br />

Now that I have defined an abstract interface, I could implement the persistence mechanism and hook it up to a database, but I<br />

want to add some of the other parts of the application first. In order to do this, I am going to create a mock implementation of the<br />

IProductRepository interface that will stand in until I return to the topic of data storage.<br />

168

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!