static NewExpression BuildNewExpression(Container container, ConstructorInfo constructor, ParameterExpression[] funcParameterExpression) { var ctorParameters = constructor.GetParameters(); var ctorParameterTypes = ctorParameters.Select(p => p.ParameterType).ToArray(); var funcParameterTypes = funcParameterExpression.Select(p => p.Type).ToArray(); var funcParametersIndex = IndexOfSubCollection(ctorParameterTypes, funcParameterTypes); if (funcParametersIndex == -1) { throw new ActivationException( $"The constructor of type {constructor.DeclaringType.FullName} did not contain the sequence of the following " + $"constructor parameters: {string.Join(", ", funcParameterTypes.Select(t => t.Name))}."); } var firstCtorParameterExpressions = ctorParameterTypes .Take(funcParametersIndex) .Select(type => container.GetRegistration(type, true).BuildExpression()); var lastCtorParameterExpressions = ctorParameterTypes .Skip(funcParametersIndex + funcParameterTypes.Length) .Select(type => container.GetRegistration(type, true).BuildExpression()); var expressions = firstCtorParameterExpressions .Concat(funcParameterExpression) .Concat(lastCtorParameterExpressions) .ToArray(); return(Expression.New(constructor, expressions)); }
private static IEnumerable<IWebDriver> AllBrowsers(Container container) { if (container.GetRegistration(typeof(ChromeDriver)) != null) yield return container.GetInstance<ChromeDriver>(); if (container.GetRegistration(typeof(InternetExplorerDriver)) != null) yield return container.GetInstance<InternetExplorerDriver>(); if (container.GetRegistration(typeof(FirefoxDriver)) != null) yield return container.GetInstance<FirefoxDriver>(); }
public void RegistersIDbInitializer_UsingGreenfieldDbInitializer_Transiently_WhenSettingIsGreenfield() { var container = new Container(); container.ComposeRoot(new RootCompositionSettings { IsGreenfield = true, }); var instance = container.GetInstance<IDatabaseInitializer<EntityDbContext>>(); var registration = container.GetRegistration(typeof(IDatabaseInitializer<EntityDbContext>)); instance.ShouldNotBeNull(); instance.ShouldBeType<GreenfieldDbInitializer>(); registration.Lifestyle.ShouldEqual(Lifestyle.Transient); }
public void RegistersICustomizeDb_UsingSqlServerDbCustomizer_Transiently_WhenSettingIsGreenfield() { var container = new Container(); container.ComposeRoot(new RootCompositionSettings { IsGreenfield = true, }); var instance = container.GetInstance<ICustomizeDb>(); var registration = container.GetRegistration(typeof(ICustomizeDb)); instance.ShouldNotBeNull(); instance.ShouldBeType<SqlServerScriptsCustomizer>(); registration.Lifestyle.ShouldEqual(Lifestyle.Transient); }
// This extension method is equivalent to the following registration, for each and every T: // container.RegisterSingleton<Func<T>>(() => container.GetInstance<T>()); // This is useful for consumers that need to create multiple instances of a dependency. // This mimics the behavior of Autofac. In Autofac this behavior is default. public static void AllowResolvingFuncFactories(this Container container) { container.ResolveUnregisteredType += (sender, e) => { if (!e.UnregisteredServiceType.IsGenericType || (e.UnregisteredServiceType.GetGenericTypeDefinition() != typeof(Func <>))) { return; } var serviceType = e.UnregisteredServiceType.GetGenericArguments()[0]; var registration = container.GetRegistration(serviceType, true); ConfirmTransient(registration, serviceType); var funcType = typeof(Func <>).MakeGenericType(serviceType); var factoryDelegate = Expression.Lambda(funcType, registration.BuildExpression()).Compile(); e.Register(Expression.Constant(factoryDelegate)); }; }
private static void RegisterArrayResolver(UnregisteredTypeEventArgs e, Container container, Type elementType) { var producer = container.GetRegistration(typeof(IEnumerable<>) .MakeGenericType(elementType)); var enumerableExpression = producer.BuildExpression(); var arrayMethod = typeof(Enumerable).GetMethod("ToArray") .MakeGenericMethod(elementType); var arrayExpression = Expression.Call(arrayMethod, enumerableExpression); e.Register(arrayExpression); }
public bool HasRegistration <TService>() { return(Container.GetRegistration(typeof(TService), false) != null); }
/* * public static void AllowResolvingExportFactories(this Container container) { * container.ResolveUnregisteredType += (s, e) => { * var type = e.UnregisteredServiceType; * if (!type.IsGenericType || * type.GetGenericTypeDefinition() != typeof (ExportFactory<>)) * return; * * var args = type.GetGenericArguments(); * var method = typeof (ResolvingFactoriesExtensions).GetMethod("CreateEF") * .MakeGenericMethod(args[0]); * var factoryDelegate = Expression.Lambda<Func<object>>(Expression.Call(method, * Expression.Constant(container))).Compile()(); * e.Register(Expression.Constant(factoryDelegate)); * }; * } * * public static ExportFactory<T> CreateEF<T>(Container container) where T : class { * ConfirmTransient(container, typeof (T)); * return new ExportFactory<T>(() => { * var instance = new Lazy<T>(container.GetInstance<T>); * var act = typeof (IDisposable).IsAssignableFrom(typeof (T)) * ? () => { * if (instance.IsValueCreated) * ((IDisposable) instance.Value).Dispose(); * } * : TaskExt.NullAction; * return new Tuple<T, Action>(instance.Value, act); * }); * }*/ static void ConfirmTransient(Container container, Type serviceType) { var registration = container.GetRegistration(serviceType); ConfirmTransient(registration, serviceType); }
public void RegistersIAuthenticationManager_UsingOwin_WhenCurrentHttpContext_HasOwinEnvironment() { HttpContext.Current = new HttpContext(new HttpRequest(null, "http://localhost", null), new HttpResponse(null)); var owinEnvironment = new Dictionary<string, object>(); var userStore = new Mock<IUserStore<User, int>>(); var userManager = new UserManager<User, int>(userStore.Object); owinEnvironment["AspNet.Identity.Owin:" + userManager.GetType().AssemblyQualifiedName] = userManager; HttpContext.Current.Items.Add("owin.Environment", owinEnvironment); var container = new Container(); container.RegisterConfiguration(); container.RegisterCryptography(); container.RegisterEntityFramework(); container.RegisterSecurity(); container.Verify(); var registration = container.GetRegistration(typeof(IAuthenticationManager)); registration.Lifestyle.ShouldEqual(Lifestyle.Transient); var instance = container.GetInstance<IAuthenticationManager>(); instance.ShouldNotBeNull(); }
public void RegistersIAuthenticationManager_UsingBigFatPhony_WhenCurrentHttpContext_HasNoOwinEnvironment() { HttpContext.Current = new HttpContext(new HttpRequest(null, "http://localhost", null), new HttpResponse(null)); var container = new Container(); container.RegisterConfiguration(); container.RegisterCryptography(); container.RegisterEntityFramework(); container.RegisterSecurity(); container.Verify(); var registration = container.GetRegistration(typeof(IAuthenticationManager)); registration.Lifestyle.ShouldEqual(Lifestyle.Transient); var instance = container.GetInstance<IAuthenticationManager>(); instance.ShouldNotBeNull(); instance.ShouldBeType<BigFatPhonyAuthenticationManager>(); }
public void RegistersIAuthenticationManager_UsingBigFatPhony_WhenCurrentHttpContext_IsNull() { //var registration = Container.GetRegistration(typeof (IAuthenticationManager)); //registration.Lifestyle.ShouldEqual(Lifestyle.Transient); //var instance = Container.GetInstance<IAuthenticationManager>(); //instance.ShouldNotBeNull(); //instance.ShouldBeType<BigFatPhonyAuthenticationManager>(); HttpContext.Current = null; var container = new Container(); container.RegisterConfiguration(); container.RegisterCryptography(); container.RegisterEntityFramework(); container.RegisterSecurity(); container.Verify(); var registration = container.GetRegistration(typeof(IAuthenticationManager)); registration.Lifestyle.ShouldEqual(Lifestyle.Transient); var instance = container.GetInstance<IAuthenticationManager>(); instance.ShouldNotBeNull(); instance.ShouldBeType<BigFatPhonyAuthenticationManager>(); }