آموزش MEF#2(استفاده از MEF در Asp.Net MVC)
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۱/۱۱/۲۹ ۲۳:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string ISBN { get; set; }
}public interface IBookRepository
{
IList<Book> GetBooks();
} [Export( typeof( IBookRepository ) )]
public class BookRepository
{
public IList<Book> GetBooks()
{
List<Book> listOfBooks = new List<Book>( 3 );
listOfBooks.AddRange( new Book[]
{
new Book(){Id=1 , Title="Book1"},
new Book(){Id=2 , Title="Book2"},
new Book(){Id=3 , Title="Book3"},
} );
return listOfBooks;
}
} [Export]
[PartCreationPolicy( CreationPolicy.NonShared )]
public class BookController : Controller
{
[Import( typeof( IBookRepository ) )]
BookRepository bookRepository;
public BookController()
{
}
public ActionResult Index()
{
return View( this.bookRepository.GetBooks() );
}
}public class MEFControllerFactory : DefaultControllerFactory
{
private readonly CompositionContainer _compositionContainer;
public MEFControllerFactory( CompositionContainer compositionContainer )
{
_compositionContainer = compositionContainer;
}
protected override IController GetControllerInstance( RequestContext requestContext, Type controllerType )
{
var export = _compositionContainer.GetExports( controllerType, null, null ).SingleOrDefault();
IController result;
if ( export != null )
{
result = export.Value as IController;
}
else
{
result = base.GetControllerInstance( requestContext, controllerType );
_compositionContainer.ComposeParts( result );
}
}
}public class MefDependencyResolver : IDependencyResolver
{
private readonly CompositionContainer _container;
public MefDependencyResolver( CompositionContainer container )
{
_container = container;
}
public IDependencyScope BeginScope()
{
return this;
}
public object GetService( Type serviceType )
{
var export = _container.GetExports( serviceType, null, null ).SingleOrDefault();
return null != export ? export.Value : null;
}
public IEnumerable<object> GetServices( Type serviceType )
{
var exports = _container.GetExports( serviceType, null, null );
var createdObjects = new List<object>();
if ( exports.Any() )
{
foreach ( var export in exports )
{
createdObjects.Add( export.Value );
}
}
return createdObjects;
}
public void Dispose()
{
}
}public class Plugin
{
public void Setup()
{
var container = new CompositionContainer( new DirectoryCatalog( HostingEnvironment.MapPath( "~/bin" ) ) );
CompositionBatch batch = new CompositionBatch();
batch.AddPart( this );
ControllerBuilder.Current.SetControllerFactory( new MEFControllerFactory( container ) );
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new MefDependencyResolver( container );
container.Compose( batch );
}
}var container = new CompositionContainer( new AssemblyCatalog( Assembly.GetExecutingAssembly() ) );
ControllerBuilder.Current.SetControllerFactory( new MEFControllerFactory( container ) );
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new MefDependencyResolver( container );
protected void Application_Start()
{
Plugin myPlugin = new Plugin();
myPlugin.Setup();
AreaRegistration.RegisterAllAreas();
RegisterRoutes( RouteTable.Routes );
}
public static void RegisterRoutes( RouteCollection routes )
{
routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Book", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}public interface IUnitOfWork
{
ISession CurrentSession { get; }
void BeginTransaction();
void Commit();
void RollBack();
}[Export(typeof(IUnitOfWork)]
public class UnitOfWork : IUnitOfWork
{
public ISession CurrentSession
{
get { throw new NotImplementedException(); }
}
public void BeginTransaction()
{
throw new NotImplementedException();
}
public void Commit()
{
throw new NotImplementedException();
}
public void RollBack()
{
throw new NotImplementedException();
}
} public class Respository
{
[Import]
private IUnitOfWork uow;
public Respository()
{
}
}@model Plugin1.Models.Post
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Model.Title