0
votes

I am trying to implement MEF framework in my .Net core and .Net framework application. Here is my sample.

Project One class library:

[Export(typeof(IProductRepository))]

public class ProductRepository : IProductRepository
{

    public ProductRepository(ApplicationDBContext context)
    {
        this.context = context;
    }
}

Project Two class library:

[Export(typeof(IProductService))]

public class ProductService : IProductService
{

    public ProductService(IProductRepository _ProductRepository)
    {

    }
}

So here both project depends on interface injection on constructor, and in another class library i am implementing the MEF like

    [System.Composition.ImportMany]
    public static IProductService ProductService{ get; set; }

    [System.Composition.ImportMany]
    public static IProductRepository ProductRepository{ get; set; }

        var executableLocation = Assembly.GetEntryAssembly().Location;
        var assemblies = Directory
                    .GetFiles(executableLocation , "*.dll", SearchOption.TopDirectoryOnly)
                    .Select(AssemblyLoadContext.Default.LoadFromAssemblyPath)
                    .ToList();
        var configuration = new ContainerConfiguration().WithAssemblies(assemblies);
        using (var container = configuration.CreateContainer())
        {
            ProductRepository= container.GetExport<IProductRepository>();
            ProductService= container.GetExport<IProductService>();
        }

Here I am getting error 'No export was found for the contract 'IProductRepository"IProductService"'

My question is, how to pass an interface to constructor of class using MEF framework? how to pass an object to constructor of class using MEF framework? How to implement MEF when multiple projects are involved?

1

1 Answers

0
votes

After searching I have found the answer it will goes as,

The repository code should be like below

[Export(typeof(IProductRepository))]
public class ProductRepository : IProductRepository
{
    [ImportingConstructor]
    public ProductRepository(ApplicationDBContext context)
    {
        this.context = context;
    }
}

The service code should be like below

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

    [ImportingConstructor]
    public ProductService(IProductRepository _ProductRepository)
    {

    }
}

And using System.ComponentModel.Composition DLL implement the MEF container part as below

[Import(typeof(IProductService))]
public static IProductService ProductService { get; set; }

[Import(typeof(IProductRepository))]
public static IProductRepository ProductRepository { get; set; }

var catalog = new DirectoryCatalog(GlobalVariables.ApplicationAssemblyPath, "*.dll");
container = new CompositionContainer(catalog);

ProductRepository = container.GetExportedValue<IProductRepository>();
ProductService = container.GetExportedValue<IProductService>();

Hence from the ProductRepository, ProductService we will get the instance