public void DisposalTest()
        {
            FauxInjectionScope injectionScope = new FauxInjectionScope();

            CompiledExportDelegateInfo info =
                new CompiledExportDelegateInfo
                {
                    ActivationType = typeof(DisposableService),
                    IsTransient = true,
                    TrackDisposable = true,
                    Attributes = new Attribute[0]
                };

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new DisposableService(), null, injectionScope);

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);

            FauxInjectionContext context = new FauxInjectionContext
                                                     {
                                                         DisposalScope = new DisposalScope()
                                                     };

            IDisposableService disposableService = (IDisposableService)activationDelegate(injectionScope, context);

            bool eventFired = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            context.DisposalScope.Dispose();

            Assert.True(eventFired);
        }
        public void EnrichWithTest()
        {
            CompiledExportDelegateInfo info = new CompiledExportDelegateInfo
                                                         {
                                                             ActivationType = typeof(IBasicService),
                                                             Attributes = new Attribute[0]
                                                         };

            info.EnrichWithDelegate((scope, context, injectObject) => new EnrichContainer(injectObject));

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new BasicService(), null, new FauxInjectionScope());

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);
            EnrichContainer container =
                (EnrichContainer)activationDelegate(new FauxInjectionScope(), new FauxInjectionContext());

            Assert.NotNull(container);
            Assert.NotNull(container.EnrichedObject);
            Assert.IsType(typeof(BasicService), container.EnrichedObject);
        }
        public void ImportMethodTest()
        {
            FauxExportStrategy basicService = new FauxExportStrategy(() => new BasicService())
                                                         {
                                                             ExportTypes = new[] { typeof(IBasicService) }
                                                         };

            FauxInjectionScope injectionScope = new FauxInjectionScope();

            injectionScope.AddStrategy(basicService);

            CompiledExportDelegateInfo info = new CompiledExportDelegateInfo
                                                         {
                                                             ActivationType = typeof(ImportMethodService),
                                                             Attributes = new Attribute[0]
                                                         };

            info.ImportMethod(new ImportMethodInfo
                                    {
                                        MethodToImport =
                                            typeof(ImportMethodService).GetRuntimeMethod("ImportMethod", new[] { typeof(IBasicService) })
                                    });

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new ImportMethodService(), null, injectionScope);

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);

            ImportMethodService propertyService =
                (ImportMethodService)activationDelegate(injectionScope, new InjectionContext(null, injectionScope));

            Assert.NotNull(propertyService);
        }
        public void SimpleExportTest()
        {
            CompiledExportDelegateInfo info = new CompiledExportDelegateInfo
                                                         {
                                                             ActivationType = typeof(IBasicService),
                                                             Attributes = new Attribute[0]
                                                         };

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new BasicService(), null, new FauxInjectionScope());

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);
            Assert.IsType(typeof(BasicService), activationDelegate(new FauxInjectionScope(), new FauxInjectionContext()));
        }
        public void SimpleActivationMethodTest()
        {
            CompiledExportDelegateInfo info = new CompiledExportDelegateInfo
                                                         {
                                                             ActivationType = typeof(ActivateService),
                                                             IsTransient = true,
                                                             TrackDisposable = true,
                                                             Attributes = new Attribute[0]
                                                         };

            info.ActivateMethod(typeof(ActivateService).GetMethod("SimpleActivate"));

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new ActivateService(), null, new FauxInjectionScope());

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);

            ActivateService activateService =
                (ActivateService)activationDelegate(new FauxInjectionScope(), new FauxInjectionContext());

            Assert.NotNull(activateService);
            Assert.True(activateService.SimpleActivateCalled);
            Assert.False(activateService.InjectionContextActivateCalled);
        }
        public void PropertyImportProviderTest()
        {
            FauxInjectionScope injectionScope = new FauxInjectionScope();

            CompiledExportDelegateInfo info = new CompiledExportDelegateInfo
                                                         {
                                                             ActivationType = typeof(ImportPropertyService),
                                                             Attributes = new Attribute[0]
                                                         };

            info.ImportProperty(new ImportPropertyInfo
                                      {
                                          Property = typeof(ImportPropertyService).GetProperty("BasicService"),
                                          ValueProvider = new FuncValueProvider<IBasicService>(() => new BasicService())
                                      });

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new ImportPropertyService(), null, injectionScope);

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);

            IImportPropertyService propertyService =
                (IImportPropertyService)activationDelegate(injectionScope, new InjectionContext(null, injectionScope));

            Assert.NotNull(propertyService);
            Assert.NotNull(propertyService.BasicService);
        }
        public void MissingDisposalScopeTest()
        {
            FauxInjectionScope injectionScope = new FauxInjectionScope();

            CompiledExportDelegateInfo info =
                new CompiledExportDelegateInfo
                {
                    ActivationType = typeof(DisposableService),
                    IsTransient = true,
                    TrackDisposable = true,
                    Attributes = new Attribute[0]
                };

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new DisposableService(), null, injectionScope);

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);

            FauxInjectionContext context = new FauxInjectionContext();

            try
            {
                IDisposableService disposableService = (IDisposableService)activationDelegate(injectionScope, context);

                throw new Exception("Should have thrown a DisposalScopeMissingException");
            }
            catch (DisposalScopeMissingException)
            {
            }
        }
        public void ImportPropertyRootTransientTest()
        {
            FauxExportStrategy basicService = new FauxExportStrategy(() => new BasicService())
                                                         {
                                                             ExportTypes = new[] { typeof(IBasicService) }
                                                         };

            FauxInjectionScope injectionScope = new FauxInjectionScope();

            injectionScope.AddStrategy(basicService);

            CompiledExportDelegateInfo info = new CompiledExportDelegateInfo
                                                         {
                                                             ActivationType = typeof(ImportPropertyService),
                                                             IsTransient = true,
                                                             Attributes = new Attribute[0]
                                                         };

            info.ImportProperty(new ImportPropertyInfo
                                      {
                                          Property =
                                              typeof(ImportPropertyService).GetProperty("BasicService")
                                      });

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new ImportPropertyService(), null, injectionScope);

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);

            IImportPropertyService propertyService =
                (IImportPropertyService)activationDelegate(injectionScope, new InjectionContext(null, injectionScope));

            Assert.NotNull(propertyService);
            Assert.NotNull(propertyService.BasicService);
        }
        public void ImportPropertyNonRootTransienTest()
        {
            FauxExportStrategy basicService = new FauxExportStrategy(() => new BasicService())
                                                         {
                                                             ExportTypes = new[] { typeof(IBasicService) }
                                                         };

            InjectionKernelManager manager =
                new InjectionKernelManager(null, DependencyInjectionContainer.CompareExportStrategies, new BlackList());
            InjectionKernel injectionScope =
                new InjectionKernel(manager, null, null, "Root", DependencyInjectionContainer.CompareExportStrategies);

            injectionScope.AddStrategy(basicService);

            CompiledExportDelegateInfo info = new CompiledExportDelegateInfo
                                                         {
                                                             ActivationType = typeof(ImportPropertyService),
                                                             IsTransient = true,
                                                             Attributes = new Attribute[0]
                                                         };

            info.ImportProperty(new ImportPropertyInfo
                                      {
                                          Property =
                                              typeof(ImportPropertyService).GetProperty("BasicService")
                                      });

            FuncCompiledExportDelegate compiledExport =
                new FuncCompiledExportDelegate(info, (x, y) => new ImportPropertyService(), null, injectionScope);

            ExportActivationDelegate activationDelegate = compiledExport.CompileDelegate();

            Assert.NotNull(activationDelegate);

            IInjectionScope requestingScope = injectionScope.CreateChildScope();

            IImportPropertyService propertyService =
                (IImportPropertyService)activationDelegate(injectionScope, new InjectionContext(null, requestingScope));

            Assert.NotNull(propertyService);
            Assert.NotNull(propertyService.BasicService);
        }
Example #10
0
        public virtual void Initialize()
        {
            FuncCompiledExportDelegate exportDelegate = new FuncCompiledExportDelegate(delegateInfo,
                (scope, context) => context.Locate(CONTEXT_KEY),null, null);

            activationDelegate = exportDelegate.CompileDelegate();
        }