In Part I, I introduced the concept of the Streetlight Store and the architecture. In this segment, we will walk through the creation of a data contract for products, interfaces for managing transactions, and a database-first Entity Framework implementation of the interfaces.

The initial plan was to distribute the store as a single library with the intent of using a SQL server database with a fixed schema. However, just as flexibility is important in the front end, it is also important in the back end so I decided to abstract the back-end logic. The namespaces listed in Part I (Streetlight.Store.Contracts and Streetlight.Store.DataAccess) have been separated into two assemblies. The DataManager class described in Part I has been abstracted into the Streetlight.Store.Contracts.ITransactionManager interface and the other DataManagers have been similarly abstracted into IProductManager, IAccountManager, IOrderManager, and IShipmentManager.

The Product Data Contract

As I mentioned in Part I, the Streetlight Store is centered around POCO classes. The first of these classes we are going to look at is the Product class. This class encapsulates all of the pertinent information about a product.

Database Schema

I’m sure some of you are asking, “Why database-first? Why not use code-first or model-first?” The short answer is I already have a database. While I’m sure it is possible to use one of the other techniques, simply generating the model from the database was the easiest approach.

Notice that not everything is an exact match between the database schema and the object model. Strings are used instead of numeric types in the object model to allow flexibility in the back end. Also, the naming convention of the object model is not identical to the database schema. For example, the ProductId database field maps to the Id property of the object model. A string is used in the object model instead of an integer so that GUIDs and other non-integer and non-numeric primary keys are supported.

Interfaces or “Operation Contracts”

Now that we have our database and object model, we need to be able to do some basic CRUD operations. Following the paradigm established in Part I, the ITransactionManager interface contains properties for the other manager interfaces. The other interfaces define the operations that can be performed following the “operation contract” paradigm from WCF. For now we will focus just on the IProductManager and ITransactionManager. These interfaces loosely follow unit of work pattern (ITransactionManager) and repository pattern (IProductManager).

/// <summary> /// Checks to see if a product exists with the specified item number and different /// ID. This function can be used to ensure exclusivity of item numbers. Returns true /// if a product exists with the specified item number and different ID, otherwise /// returns false. /// </summary> /// <param name="id">ID of item to validate item number for. If parameter is null, /// empty, or whitespace, all existing items will be searched.</param> /// <param name="itemNumber">Item number to check for duplicates</param> /// <returns>True if a product exists with the specified product number and different ID, /// otherwise false.</returns> bool DuplicateItemNumberExists(string id, string itemNumber);

/// <summary> /// Checks to see if a product exists with the specified global ID and manufacturer and /// different ID. This function can be used to ensure exclusivity of Global ID/ /// Manufacturer combinations. Returns true if a product exists with the specified /// global ID and manufacturer and different ID, otherwise returns false. /// </summary> /// <param name="id">ID of item to validate item number for. If parameter is null, /// empty, or whitespace, all existing items will be searched.</param> /// <param name="globalId">Global ID to find duplicates for</param> /// <param name="manufacturer">Manufacturer to find duplicates for</param> /// <returns>True if a product exists with the specified /// global ID and manufacturer and different ID, otherwise false.</returns> bool DuplicateGlobalIdManufacturerExists(string id, string globalId, string manufacturer);

Putting the Pieces Together

Now that we have our object model, our database schema, and our operation contract, let’s actually implement the contract to interact with the database. Most of this code is pretty straight-forward as defined in the comments with the exception of the last four methods: CreateContract, CopyToContract, CreateEntity, and CopyToEntity. These methods deal with the conversions between the model class created by the Entity Framework and the data contract class. While it is possible to map POCO classes to entity models, it is not always best practice map your POCO classes exactly to your database. For example, in this implementation we use a ProductStatuses enum top populate the Product.Status property which is stored as a string. The translation to and from the enum values is performed by the CopyToContract and CopyToEntity methods. While this may seem cumbersome on the surface, it gives you very granular control over mapping the object model to the database.

/// <summary> /// Checks to see if a product exists with the specified item number and different /// ID. This function can be used to ensure exclusivity of item numbers. Returns true /// if a product exists with the specified item number and different ID, otherwise /// returns false. /// </summary> /// <param name="id">ID of item to validate item number for. If parameter is null, /// empty, or whitespace, all existing items will be searched.</param> /// <param name="itemNumber">Item number to check for duplicates</param> /// <returns>True if a product exists with the specified product number and different ID, /// otherwise false.</returns> public bool DuplicateItemNumberExists(string id, string itemNumber) { IQueryable<DataModels.Product> query = _dataManager.Context.Products.Where(p => p.ItemNumber == itemNumber);

The Architecture Improvement Workshop (AIW) is a method for evaluating and ... Software product or project managers, software product line. In Part I, you introduced the concept of the Streetlight Store and the architecture. Goatripsindia.com