In this article, we will look at Inventory Manager Application with focus on Read side. Use cases for Inventory Manager are based on “Super simple cqrs” example by Greg Young. In Inventory Manager, only the first use case of creating an inventory item has been implemented.
Code for Inventory Manager Application is on GitHub Repository - Located here
Note that this post is part of series of articles, links for all the articles in the series are provided below.
- Need for Enterprise Servicebus Frameworks and Decoupled messaging with their samples
- Inventory Manager - CQRS application (with decoupled messaging) - Commands
- Inventory Manager - CQRS application (with decoupled messaging) - Aggregate and Event Sourcing
- Inventory Manager - CQRS application (with decoupled messaging) - ReadSide (this article)
- In the worker role, viewmodel generator handles the event
InventoryCreated, as we saw in last article.
- The handler works with the
ReadModel and creates a new inventory and saves it in database on
- Database on the
Readside is Code-first EF SQL database.
- The website reads data from Read Database, using thin data access layer and shows it in MVC View.
Use of Func<> in IOC
- An interesting thing to bring to attention was how
Func<> is used in the thin Data layer for Read side.
HomeController calls upon
InventoryItemsDao to retrieve list of all inventory items.
public class HomeController : Controller
private readonly IServiceBus _bus;
private readonly InventoryItemsDao _inventoryItemsDao;
_bus = IoC.Resolve<IServiceBus>();
_inventoryItemsDao = IoC.Resolve<InventoryItemsDao>();
public ActionResult Index()
var inventoryItems = _inventoryItemsDao.GetAllInventoryItems();
- There were no registrations done for
IoC will try to create an instance of the class trying to resolve any dependencies that it may have provided in its constructor.
InventoryItemsDao has constructor as follows:
public class InventoryItemsDao
private readonly Func<InventoryManagerDbContext> _contextFactory;
public InventoryItemsDao(Func<InventoryManagerDbContext> contextFactory)
_contextFactory = contextFactory;
public IList<InventoryItem> GetAllInventoryItems()
using (var context = _contextFactory.Invoke())
InventoryManagerDbContext has a default constructor, thus it did not have to be registered with
IoC allows us to specify dependency in a
Func return argument and resolves the dependency when one calls
invoke on the
Func. Click here for more information.
- This covers up the code walkthrough of Inventory Manager Application. I hope you found the post interesting.
- The code is still a proof of concept and I will post updates to the solution as I use it. However, it surely can serve as a good playground for trying out CQRS in a practical application.
- Any feedback on the article is welcome and thanks for your patience.
- For a complete list of articles in this series, please go to the Introduction section of this article.
- “Super simple cqrs” example by Greg Young