dedicated to write powerful yet elegant software by using minimum code and maximizing the benefits for all our clients.

Using MEF with Common Service Locator by navin 10/26/2010 2:52:00 AM

"The Managed Extensibility Framework (MEF) simplifies the creation of extensible applications. MEF offers discovery and composition abilities that you can leverage to load application extensions." - (mef.codeplex.com)

"The common service locator library contains a shared interface for service location which application and framework developers can reference"  - (commonservicelocator.codeplex.com)

Wow!

How to get them both working together, is as below.

NOTE: I am assuming that the reader is aware of Dependency Injection pattern & the Common Service Locator.

The Steps:

  1. Download the CommonServiceLocator_MefAdapter.4.0.zip file & extract the MefServiceLocator class written by Glen Block
  2. Create a new ASP.Net MVC 2.0 Web Application Project
  3. Include the MefServiceLocator class into this project
  4. Open the Global.asax.cs file
  5. Make the following code change in the Application_Start event:
    protected void Application_Start(){
    RegisterDIContainer();
    AreaRegisteration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    }
  6. Write the RegisterDIContainer method as:
    protected void RegisterDIContainer(){
    var catalog = new AggregateCatalog(
    new AssemblyCatalog(Assembly.GetExecutingAssembly()),
    new DirectoryCatalog(Server.MapPath("~/Plugins"))
    );
    var composition = new CompositionContainer(catalog);
    composition.ComposeParts(this);
    var mef = new MefServiceLocator(composition);
    ServiceLocator.SetLocatorProvider(() => mef);
    }
  7. With this in place, you are now good to do:
    var controllers = ServiceLocator.Current.GetAllInstances<IController>();

Code on...

Currently rated 1.5 by 239 people

  • Currently 1.487319/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Category: ASP.Net MVC | C# | Design Patterns | Web Development

An attempt at Enum.TryParse by 5/2/2010 7:57:31 AM

It is well known fact that Enum.TryParse does not exist in .net 3.5 although it is available in 4.0 There have been some previous implementations already made here, here and here. I thought I should add my $0.02 into the mix & put out my own implementation, so here goes: Note: line breaks added for [...]

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Category: C#