Creating an ASP.NET MVC Application with n-Tier Decoupling Using MEF

I have an MVC project. the model, controller and View have a project in which, therefore, there are 3 dlls. I also have IFACADE, IBUSINESS and IDATALAYER (DLL) interfaces with a specific implementation in FACADE, BUSINESS and DATACCESS DLLS. How can I link them all together using MEF?

+3
source share
1 answer

What I would like to offer is to determine the first extension points in your system, at what level or which components you want to allow third-party developers to extend or completely replace. You must determine where extensibility begins and where it ends.

, . , , , . , .

(), MEF . MEF .

ex. ProductRepository, , IProductRepository, , ProductRepository :

public interface IProductRepository
{
    IEnumerable<Product> GetProducts(Expression<Func<Product, bool>> query);
}

[Export(typeof(IProductRepository))]
public class ProductRepository : IProductRepository
{
    public IEnumerable<Product> GetProducts(Expression<Func<Product, bool>> query)
    {
        throw new NotImplementedException();
    }
}

, :

public interface IProductService
{
    IEnumerable<Product> GetLatestProducts(int items);
}

[Export(typeof(IProductService))]
public class ProductService : IProductService
{
    private IProductRepository _repository;


    [ImportingConstructor]
    public ProductService(IProductRepository repository)
    {
        this._repository = repository;
    }

    public IEnumerable<Product> GetLatestProducts(int items)
    {
        return _repository.GetProducts(p => p.DateCreated == DateTime.Today).OrderByDescending(p => p.DateCreated).Take(items);
    }
}

Export, MEF. , MEF CompositionContainer, ProductService ProductRepository... , Injection Dependency .

MVC- , ProductService, ProductRepository... - :

[Export(typeof(IController))]
[ExportMetadata("Name","Product")]
public class ProductController : Controller
{
    private IProductService _service;

    [ImportingConstructor]
    public ProductController(IProductService service)
    {
        _service = service;
    }

    public ActionResult LatestProducts()
    {
        var model = _service.GetLatestProducts(3);
        return View(model);
    }
}

, , , . , , [ExportMetadata ( "", "" )]. , , .

, , - match maker = > - : TypeCalog, DirectoryCatalog, AssemblyCatalog AggregateCatalog. , . codeplex

- CompositionContainer. , MVC, - ControllerFactory. ControllerFactory ( DefaultControllerFactory IControllerFactory). factory .

, , , factory: ProductController ProductService ProductService ProductRepository. MEF .

0

All Articles