One of the most important concepts when applying either SOA or DDD is the definition of Services (or Bounded Contexts in the DDD lingo).

Each of these services will be responsible for its own data and behavior and could also own the UI components of that service.

Let’s see an example with the typical Ecommerce domain:


In this case we have two services, Sales & Shipping, and each of them owns its UI components that will be rendered by the UI host. That’s the result:

Ecommerce composition

Composition with ASP.NET MVC

In this example, I’m using Razor Generator in order to let services to have their own MVC components in class libraries, which will be referenced by the central UI host:

VS solution

The use of Razor Generator is very straightforward:

  1. Install RazorGenerator from VS gallery
  2. On each class library, install RazorGenerator.Mvc from Nuget
  3. Create the folder structure following MVC conventions (Controllers, Views, etc.)
  4. Add a web.config to get intellisense for your views
  5. Change “Custom Tool” to RazorGenerator on your views in order to precompile it

Then, decorate your actions as ChildActionOnly to behave like a widget:

public class FinishOrderController : Controller
{
	private readonly IBus bus;

	public FinishOrderController(IBus bus)
	{
		this.bus = bus;
	}

	[ChildActionOnly]
	public ActionResult SubmitOrCancel()
	{
		ViewBag.OrderId = TheSession.OrderId;

		return PartialView();
	}

	[HttpPost]
	public ActionResult SubmitOrder()
	{
		var cmd = new SubmitOrder()
		{
			OrderId = TheSession.OrderId
		};

		this.bus.Send(cmd);

		return RedirectToAction("Processed", "Order");
	}

	[HttpPost]
	public ActionResult CancelOrder()
	{
		var cmd = new CancelOrder()
		{
			OrderId = TheSession.OrderId
		};

		this.bus.Send(cmd);

		return RedirectToAction("Cancelled", "Order");
	}
}

Take a look at the sample on github and let me know!!

https://github.com/mserrate/serrate.net/tree/master/UIComposition