Monday, 23 December 2013

In the last blog post, I described a solution that was broken down into different areas per location. The MVC project has this structure:

Areas

UK

US

HK

Controllers

Views

Each area had shared UI implementations for the most part, but required specialisation on the business logic side.

Problem

Specialising the services isn’t too much of a problem. You have an interface for the basic functions, and then specialise them within. What made our implementation work difficult is that there were specific repositories for each area! This was because each area interacted with a database of differing schema AND differing platform.

Design

The design of the services is fairly straightforward. You create the interface and then specialise each implementation like this:

So what is the problem? The problem is that there are multiple controllers called OrderSummaryController. They all have the same constructor, which looks identical and does conflict with the base class.

Quick-recap

I'm throwing a lot of code at you at the moment. It is an important lesson to learn about how this solution is working in the bigger scheme of things. We want to use a controller and its base to share functionality. We also want to make sure the signatures match, as it will making unit testing much easier. My solution is attempting to keep the constructors the same signature, so that a) unit testing can be easier and b) so that when a new area is created, just the constructors need copying into the new file.

Ahh - you're back! Ok, so what are we trying to do? We want Autofac to differentiate between a class and its base. You'll find Autofac throws all sorts of resolution errors in this scenario. Luckily, if you don't mind taking the plunge and allowing a litle AutoFac code into your concrete controllers, all is not lost!

This constructor overload gives AutoFac an easy way in. The beauty of it is that the application is still unit testable via the IOrderService and ICurrencyFormatter, but the IComponentContext is giving you an easier (i.e. time-saving) way in. Now, time for the hooking-up within MVC. We need to register that Module, so that MVC resolves items correctly. The easiest (and most common way) is to inherit from the DefaultControllerFactory - the MVC built-in resolver - and register the ServiceInitializationModule. We then re-point MVC at this resolver instead of its default one.

Summary

In this blog, I've described a scenario where an MVC application is separated into areas. Those areas all use different services, due to the business logic being different for them. In addition, the repositories themselves are also different.

Therefore, you should have come away with the following points

A base controller can have multiple implementations in different areas

Areas can have different services being injected into the controllers

The services can even have repositories of differing concrete types injected as well.

This analysis took several weeks to get working. I wasn't able to find it possible with any other framework, but if you do find one that does, let me know!

In the last blog post, I described a solution that was broken down into different areas per location. The MVC project has this structure:

Areas

UK

US

HK

Controllers

Views

Each area had shared UI implementations for the most part, but required specialisation on the business logic side.

Problem

Specialising the services isn’t too much of a problem. You have an interface for the basic functions, and then specialise them within. What made our implementation work difficult is that there were specific repositories for each area! This was because each area interacted with a database of differing schema AND differing platform.

Design

The design of the services is fairly straightforward. You create the interface and then specialise each implementation like this:

So what is the problem? The problem is that there are multiple controllers called OrderSummaryController. They all have the same constructor, which looks identical and does conflict with the base class.

Quick-recap

I'm throwing a lot of code at you at the moment. It is an important lesson to learn about how this solution is working in the bigger scheme of things. We want to use a controller and its base to share functionality. We also want to make sure the signatures match, as it will making unit testing much easier. My solution is attempting to keep the constructors the same signature, so that a) unit testing can be easier and b) so that when a new area is created, just the constructors need copying into the new file.

Ahh - you're back! Ok, so what are we trying to do? We want Autofac to differentiate between a class and its base. You'll find Autofac throws all sorts of resolution errors in this scenario. Luckily, if you don't mind taking the plunge and allowing a litle AutoFac code into your concrete controllers, all is not lost!

This constructor overload gives AutoFac an easy way in. The beauty of it is that the application is still unit testable via the IOrderService and ICurrencyFormatter, but the IComponentContext is giving you an easier (i.e. time-saving) way in. Now, time for the hooking-up within MVC. We need to register that Module, so that MVC resolves items correctly. The easiest (and most common way) is to inherit from the DefaultControllerFactory - the MVC built-in resolver - and register the ServiceInitializationModule. We then re-point MVC at this resolver instead of its default one.

Summary

This is part 1 of a series of blogs, where I aim to give an answer to a question I posted on StackOverflow a while ago. Although the answer was useful, the solution was already implemented and thought it would be best to describe what the problem in more detail.

Problem

Our team were tasked with creating a consistent looking website, which would interact across several different locations. The locations would all be owned by a parent company, who were creating the site. At the project outset, an estimated 70-90% of the UI would look the same. There were caveats to be aware of when developing the site.

Each location used a different order number format

Each location has a custom currency format for the screen. For example;

United Kingdom (2 decimal places) = 1.00 GBP

Hong Kong (4 decimal places) = 1.0000 HKD

United States (3 decimal places) = 1.000 USD

Each location had a different database schema – as these companies were buyouts.

Each location may contain a different database platform.

Each location may (or may not) require customised business rules. A base set of rules would be applicable and customised as such.

Design

The application was divided into 3 parallel sections / areas. The MVC structure of the application looked like this:

Areas

UK

Controllers

Views

US

HK

Controllers

Views

We also had some idea that most of the functionality would be the same, expect for specific UI tweaks. Luckily, the idea of shared functionality became apparent very early on in the project, so we had a starting point to go from.

Solution

We decided to have a base controller to contain a usual-case scenario for a particular process in the shared ~/Controllers directory. Each area would then inherit from this base controller, so that functionality was always available for all areas by default. The only additional work required was when the area needed to specialise the behaviour.

This was a good starting point for us, because we always had out-of-the-box functionality available to us. We only implemented additional functionality when required.

Overriding a view

We also had the additional benefit of having some inside knowledge of the ViewModel coming back, because we knew what service we were calling. This came in handy when the View had to be re-implemented. For example:

Quick re-cap - what have we actually achieved here?

What we have achieved is a shared single controller, that allows customisation when required. By default, all controllers will use the same view. If you wanted to add a significantly different view, you can add it to the area, so that there is a clean separation of concerns. So lets say the UK has a customised branding of the order summary screen - radically different from the default, you can add it to the specific area:

Areas

UK

Controllers

OrderSummaryController

Views

OrderSummary

Index.cshtml

US / HK

Controllers

OrderSummaryController

Controllers

OrderSummaryBaseController

Views

OrderSummary

Index.cshtml

Summary

In this blog post, I demonstrated the use of a shared controllers, whilst allowing customisation between areas when required. This allows a new area to be built *almost* out of the box immediately.

There is a problem with this example; every controller is using a poor mans Dependency Injection. That means when a new area is brought in, every controller needs to be re-implemented.

So in the next blog, I will show you how we tackled this problem, so that new areas need only a little configuration to get up and running.
In this blog, I've described a scenario where an MVC application is separated into areas. Those areas all use different services, due to the business logic being different for them. In addition, the repositories themselves are also different.

Therefore, you should have come away with the following points

A base controller can have multiple implementations in different areas

Areas can have different services being injected into the controllers

The services can even have repositories of differing concrete types injected as well.

This analysis took several weeks to get working. I wasn't able to find it possible with any other framework, but if you do find one that does, let me know!