IOC di unity, service locator, and MEF implemented by Microsoft

Source: Internet
Author: User
ArticleDirectory
    • Example
    • Example
    • Servicelocator. setlocatorprovider () => provider );
    • Example
Sites of these tools

Microsoft unity http://unity.codeplex.com

Service locator http://commonservicelocator.codeplex.com

Included in MEF. net4.0, which was open-source on codeplex before 3.x

Utility

The main reasons to use Unity (or any other IOC container) are if:

You have dependencies between your objects.

You need to manage the life time of an object.

You want to manage dependencies at runtime, such as cache, constructors, and properties.

You need to intercept the creation of an object.

Unity is a lightweight, extensible dependency injection container that supports interception, constructor injection, property injection, and method call injection. you can use unity in a variety of different ways to help decouple the components of your applications, to maximize coherence in components, and to simplify design, implementation, testing, and administration of these applications.

Unity is a general-purpose container for use in any type of Microsoft. net Framework-based application. it provides all of the features commonly found in dependency injection mechanisms, including methods to register type mappings and object instances, resolve objects, manage object lifetimes, and inject dependent objects into the parameters of constructors and methods and as the value of properties of objects it resolves.

Example

[Injectionconstructor]
Public writer (ilogger logger)
{
This. Logger = logger;
}

// Prepare the container
VaR Container = new unitycontainer ();
// We specify that the logger to be used is the filelogger
Container. registertype <ilogger, filelogger> ();
// And how to instantiate a new writer
Container. registertype <writer> ();
// Here unity knows how to create the new Constructor
VaR writer = container. Resolve <writer> ();
Writer. Write ("some text .");

Unitycontainer for Injection

Service locator example

//


// utility to configure the container
///
Public sealed class unitycontainerconfigurator
{
//
// configures this instance.
//
//
Public static iunitycontainer configure ()
{< br> var Container = new unitycontainer ()
. registertype ()
. registertype ();
return container;
}< BR >}

// Create a new instance of Microsoft unity container
VaR provider = new unityservicelocator (unitycontainerconfigurator. Configure ());
// Assign the container to the service locator provider

Servicelocator. setlocatorprovider () => provider );

// Resolve objects using the service locator
VaR writer = servicelocator. Current. getinstance <writer> ();
Writer. Write ("some text .");

The implementation of servicelocator is very simple, andCodeVery few

MEF

The main reasons to use MEF are if:

Ø you need to implement external and reusable extensions in your client application, but you might have different implementations in different hosts.

You need to auto-discover the available extensions at runtime.

Ø you need a more powerful and extensible framework than a normal dependency injection framework, and you want to get rid of the various boot-strapper and initializer objects.

Ø you need to implement extensibility and/or modularity in your components.

If your application doesn't require any of the items in these lists, you probably shocould not implement the IoC pattern, and you might not need to use unity and MEF.

Example

///


// logger customized for MEF
///
[Export (typeof (ilogger ))]
public class meflogger: ilogger
{< br> ///
// writes the log.
///
/// the message.
Public void writelog (string message)
{< br> console. writeline ("string built from MEF: {0 }. ", message);
}< BR >}

/// <Summary>
/// Gets or sets the writer.
/// </Summary>
/// <Value> the writer. </value>
[Import]
Public ilogger writer {Get; set ;}
Public void run ()
{
// First we build the catalog
VaR catalog = new assemblycatalog (assembly. getexecutingassemb
// Create the container using the catalog
VaR Container = new compositioncontainer (Catalog );
Container. composeparts (this );
// Use the resolved Property
Writer. writelog ("MEF message ");
}

Simple comparison

Service locator: the simplest form of implementation. For simple applications, the implementation code is also very simple.

Utility: Medium in complexity, between service locator and MEF

MEF: it is presented in a complete framework with built-in support for. Net 4 and various management functions such as life cycle.

Instance code: http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.Public/Ioc%5E_Di.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.