I need some expert guidance. I'm trying to learn the concepts as well as implement them, and it's possible I might be over-engineering some. But that's OK, because I am wanting to learn and gain experience more so than keep it simple. I'd like to apply the concepts of DDD and CQRS in my project but I'm having trouble figuring out how to put it all together. I need to design and implement the ItemManagement bounded context, which is part of a larger system.
First, there is going to be an MVC web application that users can use to manage item inventory. Some basic use cases are create item, edit, check-in/check-out.
Next, I want an external Web API that will this site will use for querying data and respond to commands (and also be available to other bounded contexts).
Putting these two things together is where I need guidance, especially with the command side. The way I have it imagined is that I can send command objects to their respective url of the API i.e. api/v1/items/create, api/v1/items/{id}/edit, api/v1/items/{id}/checkin, api/v1/items/{id}/checkout. But then I get stuck. After receiving a command in the API, what steps are followed to apply the command to a domain entity, validate against any business rules, and ultimately persist the changes? Here's how I was imagining it
public class Item
{
public int Id { get; private set; }
public string ItemName { get; private set; }
public bool Available { get; private set; }
public Item(int id, string itemName, bool available)
{
Id = id;
itemName = ItemName;
Available = available;
}
public Item(CreateItemCommand command)
{
Id = command.Id;
ItemName = command.ItemName;
Available = command.Available;
}
public void Edit(EditItemCommand command)
{
ItemName = command.ItemName;
Available = command.Available;
}
public void CheckIn()
{
Available = true;
}
public void CheckOut()
{
Available = false;
}
}
public class CreateItemCommand
{
public int Id { get; private set; }
public string ItemName { get; private set; }
public bool Available { get; private set; }
}
public class EditItemCommand
{
public int Id { get; private set; }
public string ItemName { get; private set; }
public bool Available { get; private set; }
}
Then, in my API actions, I would handle these commands as such:
Create
Item item = new Item(createCommand);
_repository.Add(item);
Edit
Item item = _repository.Get(id);
item.Edit(editCommand);
_repository.Save(item);
Checkout
Item item = _repository.Get(id);
item.CheckOut();
_repository.Save(item);
There's none in this example, but any domain invariant violations would result in exception being thrown from the domain model and ultimately coming back to client in form of 500 error.
I am on the right path here or is this way off from proper form/practice? Thank you very much for your time.