Building DCI Context with ASP.NET MVC
To that end lets expand a bit on the DCI with ASP.NET MVC sample that I showed in an earlier post. The focus of that post was how to fit ASP.NET MVC and DCI together in a nice cohesive architecture. In this post I will zoom in on how the controller can build up the DCI context object in a conversation with the user.
The example I’m using is still that of a very simplified online bank, where the user can trasfer money between accounts. The core DCI parts of this example is explained in this post, and the connection with ASP.NET MVC in this post. Whereas the sample in the last post simply asked the user to input all the information about the transfer in one screen, this version asks for the information one thing at a time in a wizard like fashion. Whether this is good UX, is besides the point of this post. I just want to use an example where the context is build in a series of interactions with the user.
Interaction Between User and Code
First off the user is asked to choose a source account on this simple (ugly) page:
Serving up this page is done by this rather straight forward action method:
public ActionResult SelectSource()
{
Session["Context"] = new TransferMoneyContext();
return View(new SelectAccountVm
{
SelectedAccountId = string.Empty,
Accounts = accountRepo.Accounts
});
}
Worth noticing though, is that a MoneyTransferContext object is created and stored in the session. When the user hits the Next button this action is hit:
[HttpPost]
public ActionResult SelectSource(SelectAccountVm model)
{
var ctx = Session["Context"] as TransferMoneyContext;
ctx.Source =
accountRepo.GetById(int.Parse(model.SelectedAccountId)) as TransferMoneySource;
return View("SelectDestination",
new SelectAccountVm
{
SelectedAccountId = string.Empty,
Accounts = accountRepo.Accounts
});
}
This code updates the context with the chosen source account, and serves the next view in the interaction, where the user is asked for a destination account:
When the user hits this Next button this action is hit:
[HttpPost]
public ActionResult SelectDestination(SelectAccountVm model)
{
var ctx = Session["Context"] as TransferMoneyContext;
ctx.Sink =
accountRepo.GetById(int.Parse(model.SelectedAccountId)) as TransferMoneySink;
return View("SelectAmount", new SelectAmountVm());
}
which does almost the same as the previous action, except it puts the chosen account in the Sink property on the context, and then shows this page:
The post from this page does a bit more work. First it updates the context with the amount given by the user. At this point the MoneyTransferContext is complete and ready to be exectued. Finally a result page is shown:
[HttpPost]
public ActionResult SelectAmount(SelectAmountVm model)
{
var ctx = Session["Context"] as TransferMoneyContext;
ctx.Amount = model.SelectedAmount;
ctx.Execute();
return ResultPage(ctx);
}
and the result page is:
Summing Up
To sum up: To build up a DCI context in ASP.NET MVC simply store a context object in the session context when a use case is started and execute it when it makes sense from a functionality perspective. This way the controller code is kept very simple, concentrating only on building up the context through interaction with the user, and the details of the use case execution is – from the perspective of the cotroller – hidden behind the context.
Furthermore because of the way the controller builds the context the context object only knowns the domain objects through the roles they have been assigned by the controller – or in fact the user – allowing for the full strenght of DCI to play out there.
You can find the complete code from this post on GitHub.
Reference: Building DCI Context with ASP.NET MVC from our NCG partner Christian Horsdal at the Christian Horsda’s blog.