1

So, I've decided to use the Microsoft Extensibility Framework(MEF) with my new ASP.NET MVC web project. The project is a very typical employee management system, with 3 traditional layers :- a presentation layer (with views and controllers), a business layer (with business objects) and ofcourse, a data access layer. After some research, I read a lot, that MEF is supposed to help us implement the plug-in architecture.And this is where, I seem to get stuck. I'm not able to relate with this pluggable part. I'm pretty sure that, since MEF is the part of the core .NET framework, it is not limited to any specific kind of application, and is supposed to be useful in general. I just need to see my application structure in a new light, and that's where I need some helpful insights.

As I'm still trying to get started with MEF, my main question is; what would be the main extensible(pluggable) points in my application? What objects should one be typically composing using the MEF's compose method, and what would be the advantages of doing so using MEF instead of the traditional way?

khellang
  • 17,550
  • 6
  • 64
  • 84
MrClan
  • 6,402
  • 8
  • 28
  • 43
  • 2
    This sounds like a solution looking for a problem... – khellang May 18 '14 at 11:29
  • If you're not trying to solve a particular problem with MEF, then why are you trying to use it? You bought a shiny new hammer and are roaming the countryside looking for a nail? – Phil Sandler May 18 '14 at 13:20
  • 1) For now, I just want to get my head around the concepts behind MEF, so that I'm comfortable using it later. 2) so that I can be ready for the war, where I actually will have to use my shiny new hammer. – MrClan May 18 '14 at 13:50
  • @MrClan Any comment on my answer? Does it help or it is not what you expected? – Ilija Dimov May 21 '14 at 18:38

1 Answers1

1

MEF will not help you solve extensibility issues in the case you have described in the question, because it doesn't seem to have any. What MEF can bring to the table for this particular case is Dependency Injection. If you develop your application in a way that the presentation layer depends on business abstraction and the business layer depends on a data access abstraction rather than on concrete implementation, you will get a highly decoupled application that is easy to test, maintain and change without affecting unnecessary components. Here is one way to do this:

Lets say you have data access class that performs CRUD operations on employees:

[Export(typeof(IEmployeeRepository))]
public class EmployeeRepository : IEmployeeRepository
{
    //Some logic here
}

As you can see the EmployeeRepository class implements IEmployeeRepository interface which adds the level of abstraction that is needed to develop a decoupled application. Now lets say that in the business layer you have a class that needs to call some method from the EmployeeRepository class. To be able to call a method of EmployeeRepository, you would need an instance of it. Without using MEF (or any other IoC framework, this would be a way to do it:

public class EmployeeManager
{
    private EmployeeRepository _employeeRepository;

    public EmployeeManager
    {
        _employeeRepository = new EmployeeRepository();
    } 
}

By using the above code sample, a hard dependency between the EmployeeManager and EmployeeRepository classes is created. This hard dependency is difficult to isolate when writing unit tests and causes any change of the EmployeeRepository class to directly affect the EmployeeManager class. By refactoring the code sample a little bit and putting MEF into the game, you'll get this:

[Export(typeof(IEmployeeManager))]
public class EmployeeManager : IEmployeeManager
{
    private IEmployeeRepository _employeeRepository;

    [ImportingConstructor]
    public EmployeeManager(IEmployeeRepository employeeRepository)
    {
        _employeeRepository = employeeRepository;
    } 
}

As you can see the EmployeeManager class now doesn't depend on the EmployeeRepository class, but it depends on IEmployeeRepository interface, in other words it depends on abstraction. And it doesn't create the instance of EmployeeRepository class by itself. That job is left to MEF. By now it should be clear that export and ImportingConstructor attributes are part of MEF and are used by it to discover parts and resolve dependencies at runtime. With the last code sample the classes are decoupled, easy to test and maintain and you can change the internal logic in EmployeeRepository class without making EmployeeManager class aware of it. Of course the contract between them, IEmployeeRepository have to be maintained.

The above said, can be used to decouple the presentation layer from the business layer also. Also the above said, can be implemented by using any other IoC framework, like Ninject, Autofac, StructureMap etc. The difference between these IoC frameworks and MEF is that if you use them, you'll need to configure at application start which instance gets created when some interface is encountered:

//something like this, in the case of Ninject
this.Bind<IEmployeeRepository>().To<EmployeeRepository>();

On the other hand, MEF has the ability to discover parts at runtime. On application start, you'll just need to inform it where to look for parts: Directory, Assembly, Type etc. The auto-wire capabilities of MEF (the ability to discover parts at runtime), make it more than a regular IoC framework. This ability makes MEF great for developing pluggable and extensible applications because you'll be able to add plugins while the application is running. MEF is able load them and let the application to use them.

khellang
  • 17,550
  • 6
  • 64
  • 84
Ilija Dimov
  • 5,221
  • 7
  • 35
  • 42
  • Well, not really blocks... If you inspect the grey elements in your answer (the ones wrapped in ` (backticks)), you'll see that they're tags. MEF, Dependency Injection, IoC, Export, Ninject, Autofac, StructureMap, Directory, Assembly and Type is not code – khellang May 20 '14 at 22:46
  • You are right. I've been using backticks for highlighting, but it should be used for inline code. – Ilija Dimov May 20 '14 at 22:58
  • Yes. If you _absolutely_ have to highlight something. Use **bold** :) – khellang May 20 '14 at 23:04
  • @IlijaDimov Thanks for your reply. Could you provide some practical examples of **extensible points** in any application ? – MrClan May 22 '14 at 16:04
  • @MrClan Checkout nopcommerce architecture (https://nopcommerce.codeplex.com/). It is pluggable e-commerce system. You should get an idea what does it mean to implement pluggable architecture. How to implement pluggable architecture using MEF and ASP.NET MVC? Take a look at the following link: http://stackoverflow.com/questions/21017036/mef-with-mvc-4-or-5-pluggable-architecture-2014/21037036#21037036. – Ilija Dimov May 23 '14 at 12:23