public void Setup() { // See notes in TrailTest class about wiring up the repository DirectoryCatalog repositoryCatalog = new DirectoryCatalog(@".\PlugIns"); _repositoryFactory = new RepositoryFactory(); // Now, set up the MEF container and use it to hydrate the _repositoryFactory object _container = new CompositionContainer(repositoryCatalog); _container.SatisfyImportsOnce(_repositoryFactory); }
public void Setup() { // Options for setting up dependency injection using MEF: // e.g. see http://msdn.microsoft.com/en-us/magazine/ee291628.aspx // and http://buksbaum.us/2011/08/22/gentle-introduction-to-mefpart-two/ // 1. Use the TypeCatalog and explicitly reference the types - this requires a reference to the injected type, // which kind of defeats the purpose of having it as a pluggable component //TypeCatalog repositoryCatalog = new TypeCatalog(typeof(IRepository), typeof(EFRepository)),typeof(NHibernateRepository)) // 2. Use the Directory Catalog and point to the folder in which our executable is. // (which in this case is likely to be C:\Projects\Personal\Trails2012\Trails2012.Tests\bin\Debug) // NOTE: we do not (indeed, we should not) have an explicit reference to the assembly containing our plugabble repository // NOTE: (i.e. the one that uses Entity Framework - "Trails2012.DataAccess.EF") // NOTE: so a version of the assembly file is _not_ copied into the executing folder automatically whenever this ("Trails2102.Test") assembly is built. // NOTE: We need to explicitly place a _current_copy of the Trails2012.DataAccess.EF.dll file into our executing folder, // NOTE: as well as all of its dependencies DirectoryCatalog repositoryCatalog = new DirectoryCatalog(@".\PlugIns"); // 3. Use the Directory Catalog and point to the folder which contains our pluggable component // (which in this case is likely to be C:\Projects\Personal\Trails2012\Trails2012.DataAccess.EF\bin\Debug) //Assembly executingAssembly = Assembly.GetExecutingAssembly(); //bool found = executingAssembly.GetCustomAttributes(typeof(DebuggableAttribute), false).Length > 0; //string configString = (found ? "debug" : "release"); //DirectoryCatalog repositoryCatalog = new DirectoryCatalog(@"..\..\..\Trails2012.DataAccess.EF\bin\" + configString); // 4. Use an AssemblyCatalog??? Not sure this works - it would need the pluggable component to be part of the assembly //AssemblyCatalog repositoryCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); // Of course, we can use all/any of the above and combine the using an AggregateCatalog (http://mef.codeplex.com/wikipage?title=Using%20Catalogs) // e.g. var catalog = new AggregateCatalog( // new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()), // new DirectoryCatalog(@".\Extensions")); // 5. Skip dependency injection and inject the repository explicitly (just for this Test assembly). // However, to do that we need to get a reference to the pluggable repository, in which case, we would need a reference in this test assembly // Use an internal constructor on the RepositoryFactory class and set the InternalsVisibleTo attribute of the DataAccess assembly to include this Test assembly //EFRepository repository = new EFRepository(); //_repositoryFactory = new RepositoryFactory(repository); // or //_repositoryFactory = new RepositoryFactory(); // _repositoryFactory.Repository = repository; _repositoryFactory = new RepositoryFactory(); // Now, set up the MEF container and use it to hydrate the _repositoryFactory object _container = new CompositionContainer(repositoryCatalog); _container.SatisfyImportsOnce(_repositoryFactory); }
//[TestInitialize] public void SetupRepositoryUsingMEF() { // It would be nice to specify a Moq wrapper of an IRepository // and register it with MEF // so that it gets injected into the RepositoryFactory TypeCatalog repositoryCatalog = new TypeCatalog(typeof(IRepository), typeof(Mock<IRepository>)); _repositoryFactory = new RepositoryFactory(); _container = new CompositionContainer(repositoryCatalog); _container.SatisfyImportsOnce(_repositoryFactory); // However this does not work. // It causes the following MEF error: 1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "Trails2012.DataAccess.IRepository") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Trails2012.DataAccess.IRepository".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected. // because the Moq wrapper is not decorated with the MEF attribute: // [Export(typeof(IRepository))] // Hmm...I do not like that MEF requires you to decorate classes with attributes - // other IOC containers do not do that. // Still, no biggie. RepositoryFactory is a test-specific artifact anyway. // Instead, we will not use MEF, but inject the IRepository manually. // I have set the controllers up with constructors which MEF uses, // and which take IRepository as parameter, so we can just use those constructors. }