-
-
Notifications
You must be signed in to change notification settings - Fork 164
Understanding Your Domain
Its really important to understand what you will develop and its crucial that defining your use cases. So this run-aspnetcore is a base repository but we are implementing this basement into run-aspnetcore-realworld repository which basically create a e-commerce web application.
This page mostly referring the ZAN KAVTASKIN - Applied Domain-Driven Design (DDD), Part 0 - Requirements and Modelling
Lets analysis e-commerce domain in order to provide our development roadmap.
- Identify User Stories
- Identify the Nouns in the user stories
- Identify the Verbs in the user stories
- Put together object interaction diagram
- Put together object responsibilities diagram
- Put together class digram UML showing only interesting interactions
So here are made up user stories:
- As a customer I want to list products
- As a customer I want to be able to filter products as per brand and categories
- As a customer I want to see the supplier of product in the product detail screen with all characteristics of product
- As a customer I want to be able to put products that I want to purchase in to the shopping cart so that I can check out quickly later on
- As a customer I want to see the total cost all for all of the items that are in my cart so that I see if I can afford to buy everything
- As a customer I want to see the total cost of each item in the shopping cart so that I can re-check the price for items
- As a customer I want to be able to specify the address of where all of the products are going to be sent to
- As a customer I want to be able to add a note to the delivery address so that I can provide special instructions to the postman
- As a customer I want to be able to specify my credit card information during check out so that I can pay for the items
- As a customer I want system to tell me how many items are in stock so that I know how many items I can purchase
- As a customer I want to receive order confirmation email with order number so that I have proof of purchase
- As a customer I want to list my old orders and order items history
- As a customer I want to login the system as a user and the system should remember my shopping cart items
Now I am going extract nouns and verbs from the stories above. I am looking for the nouns that will become my main objects and not the attributes.
- Customer
- Order
- Order Details
- Product
- Shopping Cart
- Shopping Cart Items
- Supplier
- User
- Address
- Brand
- Category
Verbs:
- List products applying to paging
- Filter products by brand, category and supplier
- See product all information in the details screen
- Put products in to the shopping cart
- See total cost for all of the items
- See total cost for each item
- Checkout order with purchase steps
- Specify delivery address
- Specify delivery note for delivery address
- Specify credit card information
- Pay for the items
- Tell me how many items are in stock
- Receive order confirmation email
- List the order and details history
- Login the system and remember the shopping cart items
By using above nouns and verbs we can put together a diagram such as this:
Once we have object interaction diagram we can start thinking about object responsibilities. One of the most common mistakes is to push responsibilities on to the actor object i.e. Customer. We need to remember that objects must take care of themselves and objects need to be closed for direct communication and that you need go through the functions to communicate with them.
So let's follow above approach and assign responsibilities:
Now that we have object interaction and responsibilities diagram in place we can start thinking about lower level UML class diagram.
Methods, class names, dependencies, interfaces and compositions should be defined as per conceptual responsibility diagram. And we should define methods the main objects. For example ShoppingCart.cs should have AddItemToCart, GetItem vs.. methods.
When you are implementing the domain related functions, there should be some rules to be apply. So after defining your class and methods also we need to clarify exceptional cases with defining business rules, domain policies;
- Shopping cart should not have more then 5 item.
- Shopping cart item quantity should not have more then 10.
- All orders must have a non-empty delivery address.
- The day of delivery cannot be Sunday.
So after this analysis we can start coding and develop our model to code. Lets continue..
After defining verbs of project, its good to start coding from Application layer. Because in Application Layer we are building application use cases and its good starting point of over the AspNetRun template codes. For example lets check for below verbs;
Verbs:
- Filter products by brand, category and supplier
- Put products in to the shopping cart
So for the first verb directly we can add related methods into IProductAppService.cs class in below way. If there is no application layer interface we should add this interface into Application-Interfaces folder.
public interface IProductAppService
{
....
Task<IEnumerable<ProductDto>> GetProductByBrand(int brandId);
Task<IEnumerable<ProductDto>> GetProductBySupplier(int supplierId);
....
}
So by this way, we will continue to implement this interface in Application-Services folder.
public class ProductAppService : IProductAppService
{
public async Task<IEnumerable<ProductDto>> GetProductByCategory(int categoryId)
{
var productList = await _productRepository.GetProductByCategoryAsync(categoryId);
var mapped = ObjectMapper.Mapper.Map<IEnumerable<ProductDto>>(productList);
return mapped;
}
public Task<IEnumerable<ProductDto>> GetProductByBrand(int brandId)
{
// TODOX : develop
throw new NotImplementedException();
}
public Task<IEnumerable<ProductDto>> GetProductBySupplier(int supplierId)
{
// TODOX : develop
throw new NotImplementedException();
}
}
When we try to implement this methods, figure out that we should also create _productRepository methods as expected way and so on. Developments of AspNetRun web application should continue like that. After finished back-end side, we should call this application methods from Web projects. But you got the idea of starting point of developments from business requirements with making analysis.
For example the second verb classes should become as below;
- Verb : Put products in to the shopping cart
- Classes :
- IShoppingCartAppService.cs - AddToShoppingCart method in Application layer.
- Implementation of this method in ShoppingCartAppService.cs - AddToShoppingCart in Application layer.
- Preparing shopping cart item insert method in IShoppingCartRepository in Core layer.
- Implementation of this method in ShoppingCartRepository.cs in Infrastructure layer.
This page mostly referring the ZAN KAVTASKIN - Applied Domain-Driven Design (DDD), Part 0 - Requirements and Modelling
You can check full repository documentations, step by step development and how to build your custom scenario's on this basement in 100+ page eBook PDF from here - http://www.aspnetrun.com.