Product Module

Now the path was clear, and we just had to go ahead and begin developing the modules.

Product Module implementation

In this module, the user can search any product, see its details, set a quantity and add it to the shopping cart. This is a business module which has a default view and 3 services:
  • CategoriesService: this service exposes methods to manage the categories.
  • ItemsService: exposes methods to manage the product items.
  • NavigationService: exposes methods to navigate to other views.
structure.png
Fig. 1 | On the left, the solution structure showing the modules implemented. On the right, an initial diagram showing Product Module definitions and dependencies.

Some notes regarding the structure described in the picture:

The Business Entities are placed in a separate project (Class library). This permits that can be used by different modules.
All business modules (Product, User and Admin) have their own Interfaces projects to expose services and functionality (through the service interface). This allows loosely coupling between modules while taking advantage of Dependency Injection.

Designing the business entities

Doing this task was fairly straightforward, because of the:
  • Knowledge we have of E-Commerce systems.
  • Entities draft that we had done when describing the modules.
entities.png
Fig. 2 | Business Entities diagram
The entities draft defined previously are now turned into classes that will allow the communication along modules, as well as the persistence of the information.

Persist data

Storing data in memory (instead of persisting in, for example, a database) would allow us to be more focused in our development. A CatalogDataSet was created in order to work this better.
To do this, we created a CatalogData foundational module that has the dataset and exposes a global service that is used by the rest of the application to access the data.

Test driven developing our global services

To develop the application we used the technique named Test Driven Development, it consists in doing the tests first and then write the necessary code to make it pass.

We began with a simple service, CategoriesService. Here’s the process we followed:

1. Create the unit test (in this case, CategoriesServiceFixture) under Product.Tests module.
testProject.png

2. Make the tests list.
  • Should get all categories
  • Should get all parent categories
  • Should get sub Categories by id
  • Should get category by id
  • Should get parent category by id

3. Implement a TestInitialize method in order to have a dataset available for all our tests.
private CatalogDataSet ds;
private CategoriesService categoriesService;

[TestInitialize()]
public void MyTestInitialize() {
    ds = new CatalogDataSet();
    ds.Category.AddCategoryRow("C1", "General1", 0);
    ds.Category.AddCategoryRow("C2", "General2", 0);
    ds.Category.AddCategoryRow("C1-A", "1-SubGeneral1", 1);
    ds.Category.AddCategoryRow("C1-B", "1-SubGeneral2", 1);
    ds.Category.AddCategoryRow("C2-A", "General3", 2);
    ds.AcceptChanges();

    categoriesService = new CategoriesService(ds);
}


4. Take one of the tests (in this case, we chose the ShouldGetCategoryById) to begin.

5. Write the test and try to compile. It shouldn’t compile since the getCategoryById is not implemented.
[TestMethod]
public void ShouldGetCategoryById()
{
	Category category = categoriesService.getCategoryById(3);
	Assert.AreEqual<int>(3, category.CategoryId);
	Assert.AreEqual<int>(1, category.ParentCategory.CategoryId);
}


6. Implement the code needed to make your solution compile. Compile your solution and run your tests.
(Here, under CategoriesService)
public Category getCategoryById(int parentId)
{
	return null;
}

After compiling and running your tests, the result should be:
testFailed.png

7. Add enough code to make your test pass.
public Category getCategoryById(int categoryId)
{
	Category parentCategory = null;
	CatalogDataSet.CategoryRow categoryRow = _catalogDataSet.Category.FindByCategoryId(categoryId);

	if (categoryRow == null)
		return null;

	parentCategory = getCategoryById(categoryRow.ParentCategory);

	return new Category(categoryRow.CategoryId, categoryRow.Code, categoryRow.Name, parentCategory);           
}


8. Now the test should pass:
testPassed.png

9. You should repeat these steps for all your tests. After that, the result should be:
testsCompleted.png

This was an example on how we develop one of the services using test driven development methodology. Now we have done the CatalogService, next, we need to do the same steps in order to implement the rest of the services for the product module.
Once we have done all the services only is left to implement the default view, in the next section is a walkthrough where you can see the behavior of this module.

E-Commerce Catalog – Product Module - Walkthrough

1. Click on the Products node from the navigation TreeView control.

2. Here, you can search a specific product. Enter your search words in the text box.
searchProduct.png
Fig. 3 | The RealTimeSearchMonitor control in Search Products page

Note: This was based on the Search Bundle
3. Also, you can see the product details, just click on the Details icon (productDetailsIcon.png)
productDetails.png
Fig. 4 | Product details pop up.

4. You can change the Quantity and add the product to shopping cart.

Last edited Mar 14, 2008 at 3:44 PM by mconverti, version 4

Comments

No comments yet.