public void RegisterWithNullContextFactoryThrowsArgumentNullException() { var container = new Container(); var ex = Assert.Throws<ArgumentNullException>(() => container.RegisterWithContext<ILog>(null)); Assert.Equal("contextBasedFactory", ex.ParamName); }
public void RegisterWithContextProvidesRootContextOnResolve() { var container = new Container(); container.RegisterWithContext<ILog>(dependencyContext => { return new Logger(dependencyContext.ImplementationType); }); var logger = container.GetInstance<ILog>(); Assert.NotNull(logger); Assert.Null(logger.Type); }
public void RegisterWithContextProvidesDependencyContextOnResolveForDependentType() { var container = new Container(); container.RegisterWithContext<ILog>(dependencyContext => { return new Logger(dependencyContext.ImplementationType); }); var controller = container.GetInstance<Controller>(); Assert.NotNull(controller); Assert.NotNull(controller.Logger); Assert.Equal(controller.GetType(), controller.Logger.Type); }
protected void Application_Start(object sender, EventArgs e) { var container = new Container(); //Package registration; container.RegisterPackages(); //ILogger registration; container.RegisterWithContext<ILogger>(context => LogManager.GetLogger(context.ImplementationType.Name)); //WCF integration; SimpleInjectorServiceHostFactory.SetContainer(container); //Service initialisation; var initialiser = container.GetInstance<IInitialiser>(); initialiser.Initialise(); }
internal void InitContainer() { var assembly = typeof(IOptionsDialogWatcher).Assembly; var exportedTypes = assembly.GetExportedTypes(); var classes = exportedTypes.Where(t => t.IsClass); var interfaces = exportedTypes.Where(t => t.IsInterface); var registrations = from @interface in interfaces let types = classes.Where(@interface.IsAssignableFrom) where types.Count() == 1 select new { Interface = @interface, Implementation = types.First() }; _container = new Container(); _container.RegisterAll<IWindowModifier>(classes.Where(typeof(IWindowModifier).IsAssignableFrom)); _container.RegisterWithContext(CyclicWorkerContexts); registrations.ToList().ForEach(r => _container.Register(r.Interface, r.Implementation)); }