Introduction
Application requirements change frequently and software is constantly evolving.
As a result, such applications often become monolithic making it difficult to
add new functionality. The Managed Extensibility Framework (MEF) is a new
library in .NET Framework 4 that addresses this problem by simplifying the
design of extensible applications and components.
In this blog I will be trying to simplify the importance of MEF and where can
MEF be applied. Let me ask a question before explaining the uses of MEF. Imagine
you have an application which communicates with SQL database. One fine day your
client asks you to migrate the data layer from SQL database to Oracle or MYSql
or so on… How will you accommodate the change without having an impact on the
application? Will you not want a database layer wherein any change in the
database layer does not force you to change even a single line of code any
where? Will you not want a database layer such that a dll is picked
accommodating the database changes instead of modifying the existing code? Will
you not want a form of deployment such that the dll is picked at runtime
accommodating the new database changes?
If the answer is yes for all the above questions; you should have explored MEF
by now. Today I will be taking you through how to create a database layer using
a MEF such that a new database change can be accommodated by deploying a dll in
the database folder.
Prerequisites
- Visual Studio 2010
- Knowledge of C#.Net
Project Flow
Add a class library project having namespace BSIL.POC.MEFDatabase.

Add an interface named IDatabase.cs

In the IDatabase.cs we have the following code:
namespace
BSIL.POC.MEFDatabase
{
public interface
IDatabase
{
string DatabaseName {
get; set; }
bool Login(string
userName, string password);
}
}
Add another class project having namespace BSIL.POC.MEFDatabase.SQL . Add a
class named Database.cs. This class will connect to SQL database and implements
IDatabase interface as below:
using
System;
using
System.ComponentModel.Composition;
namespace
BSIL.POC.MEFDatabase.SQL
{
[Export(typeof(IDatabase))]
public class
Database :
IDatabase
{
private string
_databaseName=string.Empty;
public Database()
{
_databaseName = "SQL";
}
public bool
Login(string userName,
string password)
{
if (userName !=
string.Empty && password !=
string.Empty)
{
return
true;
}
return false;
}
public string
DatabaseName
{
get
{
return _databaseName;
}
set
{
_databaseName = value;
}
}
}
}
Add reference for System.ComponentModel.Composition and BSIL.POC.MEFDatabase in
BSIL.POC.MEFDatabase.SQL project. Important for MEF is the "Export" attribute
from the "System.ComponentModel.Composite" (MEF) Namespace.
Export means: This is a "IDatabase" plugin.


Add another class project having namespace BSIL.POC.MEFDatabase.Oracle .

Add a class named Database.cs. This class will connect to Oracle database and
implements IDatabase interface as below:
using
System;
using
System.ComponentModel.Composition;
namespace
BSIL.POC.MEFDatabase.Oracle
{
[Export(typeof(IDatabase))]
public class
Database :
IDatabase
{
private string _databaseName=string.Empty;
public Database()
{
_databaseName = "Oracle";
}
public bool Login(string
userName, string password)
{
if (userName !=
string.Empty && password !=
string.Empty)
{
return
true;
}









KARTHIK MBPosted Apr 19, 2019, 5:04 AM
Hi Praneeth, i m getting bloe exception in runtime. you have any idea on this error "System.ComponentModel.Composition.CompositionException: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information. Not enough storage is available to process this command."
Praneeth KumarPosted May 20, 2011, 3:16 AM
Hi Max, You are correct it is IOC.Well to add on spring.net, castle windsor we can even use unity blocks to acheive the same functionality. MEF is out of the box feature in .Net 4.0 compared to the other options. To top that I find Unity is more developer friendly than MEF. Regards, Praneeth
Max CervantesPosted May 18, 2011, 10:01 AM
this sounds like an IoC to me. Configuration stuff aside, what would be the difference between MEF and, let's say, spring.net, castle windsor or structureMap?