Пример #1
0
        public void TestCodeBasedDiContainerBuilder(DiImplementationType diImplementationType,
                                                    IEnumerable <string> assemblyProbingPaths,
                                                    bool isPresetDiContainer)
        {
            var          diImplementationInfo = DiManagerHelpers.GetDiImplementationInfo(diImplementationType);
            IDiContainer presetDiContainer    = null;

            var diContainerBuilder = new DiContainerBuilder.DiContainerBuilder();

            var diContainerConfigurator = diContainerBuilder.StartCodeBasedDi(diImplementationInfo.DiManagerClassName,
                                                                              diImplementationInfo.DiManagerAssemblyPath,
                                                                              new ParameterInfo[0], Helpers.TestsEntryAssemblyFolder,
                                                                              assemblyProbingPaths.ToArray());

            ICodeBasedDiModulesConfigurator diModulesConfigurator = null;

            if (isPresetDiContainer)
            {
                presetDiContainer     = CreateDiContainer(diImplementationType);
                diModulesConfigurator = diContainerConfigurator.WithDiContainer(presetDiContainer);
            }
            else
            {
                diModulesConfigurator = diContainerConfigurator.WithoutPresetDiContainer();
            }

            string nativeModuleClassName             = null;
            var    nativeModuleClassAssemblyFilePath = Path.Combine(DiManagerHelpers.DynamicallyLoadedDllsFolder, "TestProjects.Modules.dll");

            switch (diImplementationType)
            {
            case DiImplementationType.Autofac:
                nativeModuleClassName = "Modules.Autofac.AutofacModule1";
                break;

            case DiImplementationType.Ninject:
                nativeModuleClassName = "Modules.Ninject.NinjectModule1";
                break;
            }

            using (var containerInfo = diModulesConfigurator
                                       .AddNativeModule(nativeModuleClassName, nativeModuleClassAssemblyFilePath,
                                                        new[] { new ParameterInfo(typeof(int), 18) })
                                       .AddDiModules(new DiModule1())
                                       .RegisterModules()
                                       .Start())
            {
                if (isPresetDiContainer)
                {
                    Assert.AreSame(presetDiContainer, containerInfo.DiContainer);
                }

                ValidateDiContainer(diImplementationType, containerInfo.DiContainer);
            }
        }
        public void LoadFromModules()
        {
            var diImplementationInfo = DiManagerHelpers.GetDiImplementationInfo(DiImplementationType.Autofac);

            var assemblyProbingPaths = new[]
            {
                DiManagerHelpers.ThirdPartyLibsFolder,
                diImplementationInfo.DiManagerFolder
            };

            using (var containerInfo = new DiContainerBuilder.DiContainerBuilder()

                                       // Class IoC.Configuration.DiContainerBuilder.DiContainerBuilder has two overloaded methods StartCodeBasedDi(...)
                                       // DiContainerBuilder.StartCodeBasedDi(IoC.Configuration.DiContainer.IDiManager diManager,...) and
                                       // DiContainerBuilder.StartCodeBasedDi(string diManagerClassFullName, string diManagerClassAssemblyFilePath,...).
                                       // if the project references the library with implementation of IoC.Configuration.DiContainer.IDiManager,
                                       // the first one can be used. Otherwise the second overloaded method can be used, in which case reflection will be used to
                                       // create an instance of IoC.Configuration.DiContainer.IDiManager.
                                       .StartCodeBasedDi("IoC.Configuration.Autofac.AutofacDiManager",
                                                         Path.Combine(Helpers.GetTestFilesFolderPath(),
                                                                      @"ContainerImplementations\Autofac\IoC.Configuration.Autofac.dll"),
                                                         Array.Empty <ParameterInfo>(), Helpers.TestsEntryAssemblyFolder, assemblyProbingPaths)
                                       // Note, most of the time we will need to call method WithoutPresetDiContainer().
                                       // However, in some cases, we might need to create an instance of IoC.Configuration.DiContainer.IDiContainer,
                                       // and call the method WithDiContainer(IoC.Configuration.DiContainer.IDiContainer diContainer) instead.
                                       // This might be necessary when using the IoC.Configuration to configure dependency injection in
                                       // ASP.NET Core projects.
                                       // An example implementation of IDIContainer is IoC.Configuration.Autofac.AutofacDiContainer in
                                       // Nuget package IoC.Configuration.Autofac.
                                       .WithoutPresetDiContainer()

                                       // The methods AddDiModules(params IDiModule[] diModules),
                                       // AddNativeModules(params object[] nativeModules), and
                                       // AddNativeModules(string nativeModuleClassFullName, string nativeModuleClassAssemblyFilePath, ...)
                                       // are used to load IoC.Configuration modules (instances of IoC.Configuration.DiContainer.IDiModule), as well
                                       // as native (e.g, Ninject or Autofac) modules.
                                       // Also, these three methods can be called multiple times in any order.
                                       .AddDiModules(new TestDiModule())
                                       .AddNativeModule("Modules.Autofac.AutofacModule1",
                                                        Path.Combine(Helpers.GetTestFilesFolderPath(),
                                                                     @"DynamicallyLoadedDlls\TestProjects.Modules.dll"),
                                                        new ParameterInfo[] { new ParameterInfo(typeof(int), 5) })

                                       .RegisterModules()
                                       .Start())
            {
                var diContainer = containerInfo.DiContainer;

                // Once the configuration is loaded, resolve types using IoC.Configuration.DiContainer.IDiContainer
                // Note, interface IoC.Configuration.DiContainerBuilder.IContainerInfo extends System.IDisposable,
                // and should be disposed, to make sure all the resources are properly disposed of.
                var resolvedInstance = containerInfo.DiContainer.Resolve <IInterface2>();
            }
        }
Пример #3
0
        public void TestCodeBasedDiContainerBuilder(DiImplementationType diImplementationType, bool isLoggerNotSetTest)
        {
            var assemblyProbingPaths = new List <string>
            {
                DiManagerHelpers.ThirdPartyLibsFolder,
                DiManagerHelpers.DynamicallyLoadedDllsFolder,
                DiManagerHelpers.GetDiImplementationInfo(diImplementationType).DiManagerFolder
            };

            RuntTests(isLoggerNotSetTest, () =>
            {
                TestCodeBasedDiContainerBuilder(diImplementationType, assemblyProbingPaths, true);
                TestCodeBasedDiContainerBuilder(diImplementationType, assemblyProbingPaths, false);
            });
        }
        protected static IContainerInfo CreateCodeBasedContainerInfo(DiImplementationType diImplementationType)
        {
            var diImplementationInfo = DiManagerHelpers.GetDiImplementationInfo(diImplementationType);

            var assemblyProbingPaths = new[]
            {
                DiManagerHelpers.ThirdPartyLibsFolder, diImplementationInfo.DiManagerFolder
            };

            var diContainerBuilder = new DiContainerBuilder.DiContainerBuilder();

            return(diContainerBuilder.StartCodeBasedDi(diImplementationInfo.DiManagerClassName,
                                                       diImplementationInfo.DiManagerAssemblyPath,
                                                       new ParameterInfo[0], Helpers.TestsEntryAssemblyFolder, assemblyProbingPaths)
                   .WithoutPresetDiContainer()
                   .AddDiModules(new TestDiModule(), new TestModule2())
                   .RegisterModules().Start());
        }
Пример #5
0
        private IDiContainer CreateDiContainer(DiImplementationType diImplementationType)
        {
            var diImplementationInfo = DiManagerHelpers.GetDiImplementationInfo(diImplementationType);

            var classFullName         = diImplementationInfo.DiContainerClassName;
            var assemblyPath          = diImplementationInfo.DiManagerAssemblyPath;
            var constructorParameters = new ParameterInfo[0];

            var probingPaths = new List <string>
            {
                DiManagerHelpers.ThirdPartyLibsFolder,
                diImplementationInfo.DiManagerFolder
            };

            using (new AssemblyResolver(probingPaths))
            {
                return(CreateObject <IDiContainer>(classFullName, assemblyPath, constructorParameters));
            }
        }
        public void ResolveBindings()
        {
            // Probing paths are used to re-solve the dependencies.
            var assemblyProbingPaths = new string[]
            {
                DiManagerHelpers.ThirdPartyLibsFolder,
                DiManagerHelpers.DynamicallyLoadedDllsFolder,
                DiManagerHelpers.GetDiImplementationInfo(DiImplementationType.Autofac).DiManagerFolder
            };

            var diImplementationInfo = DiManagerHelpers.GetDiImplementationInfo(DiImplementationType.Autofac);

            using (var containerInfo = new DiContainerBuilder.DiContainerBuilder()
                                       .StartCodeBasedDi("IoC.Configuration.Autofac.AutofacDiManager",
                                                         diImplementationInfo.DiManagerAssemblyPath,
                                                         new ParameterInfo[0],
                                                         Helpers.TestsEntryAssemblyFolder,
                                                         assemblyProbingPaths)
                                       .WithoutPresetDiContainer()
                                       // Note, AddNativeModule() to add native modules (e.g., instances of  Autofac.AutofacModule or
                                       // Ninject.Modules.NinjectModule) // and AddDiModules to add IoC.Configuration modules (i.e.,
                                       // instances IoC.Configuration.DiContainer.IDiModule), can be called multiple times, without
                                       // any restriction on the order in which these methods are called.
                                       .AddNativeModule("Modules.Autofac.AutofacModule1",
                                                        Path.Combine(DiManagerHelpers.DynamicallyLoadedDllsFolder, "TestProjects.Modules.dll"),
                                                        new[] { new ParameterInfo(typeof(int), 18) })
                                       .AddDiModules(new TestDiModule())
                                       .RegisterModules()
                                       .Start())
            {
                var diContainer = containerInfo.DiContainer;

                SelfBoundServiceDemo(diContainer);
                BindToTypeDemo(diContainer);
                BindToAValueReturnedByDelegate(diContainer);
            }
        }