Scenario: Implement Dependency inversion. Managed extensibility framework (MEF) is a scalable management framework.
Preparation: defined interface, interface implementation class, introduction of namespace, system. componentmodel. Composition
[Implementation in the console]
---------------------------------------------------------NamespaceLeleapplicationdemo. icontract {Public InterfaceIexpor {StringGettxt (StringName,StringPassword );}}
---------------------------------------------------------------
using leleapplicationdemo. icontract; using system. componentmodel. composition; namespace leleapplicationdemo. impl {[Export ( "ex1" , typeof (iexpor)] Public class demo1: iexpor { Public string gettxt ( string name, string password) { return string . format ( "{0} --- {1}" , name, password) ;}}-----------------------------------------------------------
using leleapplicationdemo. icontract; using system. componentmodel. composition; namespace leleapplicationdemo. impl2 {[Export ( "ex2" , typeof (iexpor)] Public class demo1: iexpor { Public string gettxt ( string name, string password) { return string . format ( "***** {0} ^ {1}" , name, password) ;}}
---------------------------------------------------------------
Class used for the interface:
using system. componentmodel. composition; using leleapplicationdemo. icontract; namespace consoleapplication. demo {[Export] class demoexpor {[import ( "ex1" )] Public iexpor expor {Get; set ;} [import ( "ex2" )] Public iexpor expor2 {Get; set ;}}
---------------------------------------------------------------
Call:
static void main ( string [] ARGs) {aggregatecatalog catalog = New aggregatecatalog (); catalog. catalogs. add ( New directorycatalog (directory. getcurrentdirectory (); catalog. catalogs. add ( New assemblycatalog (assembly. getexecutingassembly (); compositioncontainer Tainer = New compositioncontainer (catiner); demoexpor dd = Tainer. getexportedvalue
(); console. writeline (DD. expor. gettxt (
"N @ me" ,
"P @ $ w0rd" ); console. writeline (DD. expor2.gettxt (
"N @ me" ,
"P @ $ w0rd" ); console. read () ;}
---------------------------------------------------------------
Result:
----------------------------------------------------------------------------------
In mvc4, MEF implements Dependency inversion:
Preparation: interface, implementation class, introduction of namespace, system. componentmodel. Composition
namespaces namespace mefiocdemo { Public interface iaccountsitecontract { string test ( string name, string password) ;}
UsingSystem. componentmodel. composition;UsingSystem. componentmodel. Composition. primitives ;---------------------------------------------------------------NamespaceMefiocdemo. impl {[Export (Typeof(Iaccountsitecontract)]Public ClassAccountcontract: iaccountsitecontract {Public StringTest (StringName,StringPassword ){Return String. Format ("Name: {0} password: {1 }", Name, password );}}}
---------------------------------------------------------------
// Create a MEF implementation class that implements the idependencyresolver Interface // Web applicationProgramStateless, that is, each access is performed independently, so the object instance generated by the IOC component must also be unique. // Otherwise, operations by different users may be connected in a string, causing mutual interference. // Use the httpcontext. Current. items set to save the compositioncontainer instance of the combined container, so that each user's data remains independent and the same object instance is used in the same HTTP request of the same user. // In addition, you may need to manually obtain the instances in the combined container and cache the combined container to the application in the current context. // (Refer to http://www.cnblogs.com/guomingfeng/archive/2013/05/21/mvc-ioc-mef.html MVC practical Architecture Design (2) -- use MEF practical IOC (dependency inversion )) Using System; Using System. Collections. Generic; Using System. componentmodel. composition; Using System. componentmodel. Composition. Hosting; Using System. componentmodel. Composition. primitives; Using System. Web. MVC; Using System. Web; Namespace Mefiocdemo. IOC { Public Class Iocdemo: idependencyresolver { Private Composablepartcatalog _ Catalog; Private Const String Iocdemokey = "Iocdemokey" ; Public Iocdemo (composablepartcatalog log) {_ catalog = log ;} Public Compositioncontainer container {get { If (! Httpcontext. Current. Items. Contains (iocdemokey) {httpcontext. Current. Items. Add (iocdemokey, New Compositioncontainer (_ Catalog);} compositioncontainer triner = (compositioncontainer) httpcontext. Current. items [iocdemokey]; httpcontext. Current. application [ "Container" ] = Triner; Return Triner ;}} Public Object Getservice (type servicetype ){ String Contractname = attributedmodelservices. getcontractname (servicetype ); Return Container. getexportedvalueordefault < Object > (Contractname );} Public Ienumerable < Object > Getservices (type servicetype ){ Return Container. getexportedvalues < Object > (Servicetype. fullname );}}}
---------------------------------------------------------------
Initialize in route Configuration
Using Mefiocdemo. IOC; Using System. componentmodel. Composition. Hosting; Namespace Demomvc4 { // Note: For instructions on enabling the IIS6 or iis7 Classic mode, // Please visit http://go.microsoft.com /? Linkid = 9394801 Public Class Mvcapplication: system. Web. httpapplication { Protected Void Application_start () {arearegistration. registerallareas (); webapiconfig. register (globalconfiguration. configuration); filterconfig. registerglobalfilters (globalfilters. filters); routeconfig. registerroutes (routetable. routes); bundleconfig. registerbundles (bundletable. bundles); directorycatalog catalog = New Directorycatalog (appdomain. currentdomain. setupinformation. privatebinpath); iocdemo demod = New Iocdemo (Catalog); dependencyresolver. setresolver (demod );}}}
---------------------------------------------------------------
Usage:
using system. componentmodel. composition; namespace demomvc4.controllers {[Export] Public class homecontroller: controller {[import] private mefiocdemo. iaccountsitecontract accountcontract; Public actionresult index () {viewbag. title = accountcontract. test ( "ABC" , "P @ ssw0rd" ); return View () ;}}
On the page:
@ {Viewbag. Title = viewbag. Title;} <H2 >@viewbag. Title </H2>
Result: