internal DefaultFallbackScopedHybridLifestyle( ScopedLifestyle defaultLifestyle, ScopedLifestyle fallbackLifestyle) : base("Hybrid " + GetHybridName(defaultLifestyle) + " / " + GetHybridName(fallbackLifestyle)) { this.defaultLifestyle = defaultLifestyle; this.fallbackLifestyle = fallbackLifestyle; }
public static Container RegisterServices(ScopedLifestyle lifestyle) { var container = new Container(); if (lifestyle != null) { container.Options.DefaultScopedLifestyle = lifestyle; } container.Register <IUsuarioService, UsuarioService>(); container.Register <IUsuarioRepository, UsuarioRepository>(); container.Register <IPerfilService, PerfilService>(); container.Register <IPerfilRepository, PerfilRepository>(); container.Register <IUsuarioAppService, UsuarioAppService>(); container.Register <IPerfilAppService, PerfilAppService>(); //container.Verify(); return(container); }
protected override Registration CreateRegistrationCore <TService>(Func <TService> instanceCreator, Container container) { ScopedLifestyle lifestyle = GetDefaultScopedLifestyle(container); return(lifestyle.CreateRegistration <TService>(instanceCreator, container)); }
public void DisposeInstances_WithMultipleItemsThatThrow_WillBubbleUpTheLastThrownException() { // Arrange var disposables = new List <DisposableObject> { new DisposableObject(new Exception()), new DisposableObject(new Exception()), new DisposableObject(new Exception()), new DisposableObject(), new DisposableObject(new Exception()), new DisposableObject(new Exception()) }; try { // Act ScopedLifestyle.DisposeInstances(disposables.Cast <IDisposable>().ToList()); // Assert Assert.Fail("Exception was expected to bubble up."); } catch (Exception ex) { Assert.IsTrue(object.ReferenceEquals(disposables.First().ExceptionToThrow, ex)); } }
internal DisposableRegistration( ScopedLifestyle s, Lifestyle l, Container c, Type concreteType, Func <object> ic = null) : base(l, c, concreteType, ic) { this.ScopedLifestyle = s; DisposableTransientLifestyle.TryEnableTransientDisposalOrThrow(c); }
public SimpleInjectorJobActivator(Container container) { Lifestyle = new AsyncScopedLifestyle(); container.Options.DefaultScopedLifestyle = Lifestyle; Container = container; }
public static IAppBuilder UseSimpleInjectorResolver(this IAppBuilder app, ScopedLifestyle lifestyle, Func <Container, Container> serviceRegistration) { if (app.GetSimpleInjectorResolver() == null) { //set the owin resolution context app.Properties[OWINResolutionContext] = new SimpleInjectorOwinResolutionContext(lifestyle, serviceRegistration); //shutdown delegate var token = new AppProperties(app.Properties).OnAppDisposing; if (token != CancellationToken.None) { token.Register(() => app.GetSimpleInjectorResolver().Dispose()); } app.Use(async(cxt, next) => { using (app.GetSimpleInjectorResolver().NewResolutionScope()) { await next(); } }); } return(app); }
public AbstractConsoleApplicationContext(Lifestyle defaultLifestyle, ScopedLifestyle scopedLifeStyle, params IModule[] modules) { Container = new Container(); Container.Options.DefaultLifestyle = defaultLifestyle; Container.Options.DefaultScopedLifestyle = scopedLifeStyle; foreach (var module in modules) { module.RegisterServices(Container); } if (Container.GetRegistration(typeof(IEnumerable <IBootstrapper>)) != null) { using (Container.BeginLifetimeScope()) { // Discover and run bootstrappers foreach (var bootstrapper in Container.GetAllInstances <IBootstrapper>()) { bootstrapper.Bootstrap(); } } } Container.Verify(); scope = BeginScope(); }
internal DisposableRegistration(ScopedLifestyle s, Lifestyle l, Container c, Func <TImpl> ic) : base(l, c) { this.instanceCreator = ic; this.ScopedLifestyle = s; DisposableTransientLifestyle.TryEnableTransientDisposalOrThrow(c); }
internal ScopedHybridLifestyle(Func <bool> lifestyleSelector, ScopedLifestyle trueLifestyle, ScopedLifestyle falseLifestyle) : base("Hybrid " + GetHybridName(trueLifestyle) + " / " + GetHybridName(falseLifestyle)) { this.lifestyleSelector = lifestyleSelector; this.trueLifestyle = trueLifestyle; this.falseLifestyle = falseLifestyle; }
internal LifestyleSelectorScopedHybridLifestyle( Predicate <Container> lifestyleSelector, ScopedLifestyle trueLifestyle, ScopedLifestyle falseLifestyle) : base("Hybrid " + GetHybridName(trueLifestyle) + " / " + GetHybridName(falseLifestyle)) { this.selector = lifestyleSelector; this.trueLifestyle = trueLifestyle; this.falseLifestyle = falseLifestyle; }
public void DisposeInstances_WithNullArgument_ThrowsExpectedException() { // Act Action action = () => ScopedLifestyle.DisposeInstances(null); // Assert AssertThat.ThrowsWithParamName <ArgumentNullException>("disposables", action); }
internal ScopedHybridLifestyle(Func<bool> lifestyleSelector, ScopedLifestyle trueLifestyle, ScopedLifestyle falseLifestyle) : base("Hybrid " + GetHybridName(trueLifestyle) + " / " + GetHybridName(falseLifestyle)) { this.selector = lifestyleSelector; this.trueLifestyle = trueLifestyle; this.falseLifestyle = falseLifestyle; }
public static void RegisterManyForOpenGenericAccordingTolifestyleAttribute( this Container container, Type serviceType, ScopedLifestyle scoped = null, params Assembly[] assemblies) { container.RegisterManyForOpenGeneric(serviceType, (s, i) => container.RegisterAccordingToLifestyleAttribute(s, i.Single(), scoped), assemblies); }
static void RegisterServices(ScopedLifestyle requestLifestyle) { _container.Register <IOrcamentoService, OrcamentoService>(requestLifestyle); _container.Register <IItemValorService, ItemValorService>(requestLifestyle); _container.Register <IItemSubValorService, ItemSubValorService>(requestLifestyle); //Identity _container.Register <IUsuarioService, UsuarioService>(requestLifestyle); }
public OptimizableLifestyleInfo(ScopedLifestyle lifestyle, IEnumerable <OptimizableRegistrationInfo> registrations) { this.Lifestyle = lifestyle; this.Registrations = registrations.ToArray(); this.Variable = Expression.Variable(typeof(LazyScope)); this.InitializeRegistrations(); }
public SimpleInjectorOwinResolutionContext(ScopedLifestyle lifeStyle, Func <Container, Container> serviceRegistration) { ThrowNullArguments(() => serviceRegistration); _container = new Container(); _container.Options.DefaultScopedLifestyle = lifeStyle; serviceRegistration.Invoke(_container); }
private Scope GetScopeFromDefaultScopedLifestyle(Container container) { ScopedLifestyle lifestyle = container.Options.DefaultScopedLifestyle; if (lifestyle != null) { return(lifestyle.GetCurrentScope(container) ?? ThrowThereIsNoActiveScopeException()); } return(null); }
public void CreateHybrid_WithValidScopedLifestyles_ReturnsScopedLifestyle() { // Arrange ScopedLifestyle trueLifestyle = new CustomScopedLifestyle(); ScopedLifestyle falseLifestyle = new CustomScopedLifestyle(); // Act ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => true, trueLifestyle, falseLifestyle); // Assert Assert.IsNotNull(hybrid); }
private Scope GetScopeFromDefaultScopedLifestyle(Container container) { ScopedLifestyle lifestyle = container.Options.DefaultScopedLifestyle; if (lifestyle != null) { return(lifestyle.GetCurrentScope(container) ?? ThrowThereIsNoActiveScopeException()); } return(container.GetVerificationOrResolveScopeForCurrentThread() ?? ThrowResolveFromScopeOrRegisterDefaultScopedLifestyleException()); }
public MvcResolutionContext(ScopedLifestyle defaultScope, Func <Container, Container> serviceRegistration) { ThrowNullArguments(() => serviceRegistration); var container = new Container(); if (defaultScope != null) { container.Options.DefaultScopedLifestyle = defaultScope; } _resolver = new SimpleInjectorDependencyResolver(serviceRegistration.Invoke(container)); }
private static SimpleInjector.Lifestyle GetLifestyleForType(Type implementation, ScopedLifestyle scoped) { var attr = implementation.GetCustomAttribute<LifestyleAttribute>(); var lifestyle = attr == null ? Lifestyle.Transient : attr.Lifestyle; switch (lifestyle) { case Lifestyle.Transient: return SimpleInjector.Lifestyle.Transient; case Lifestyle.Singleton: return SimpleInjector.Lifestyle.Singleton; default: if (scoped == null) throw new InvalidOperationException( "No scoped lifestyle supplied"); return scoped; } }
static void RegisterApplications(ScopedLifestyle requestLifestyle) { _container.Register <IGerenciadorDeOrcamento, GerenciadorDeOrcamento>(requestLifestyle); _container.Register <IGerenciadorDeItemValor, GerenciadorDeItemValor>(requestLifestyle); _container.Register <IGerenciadorDeItemSubValor, GerenciadorDeItemSubValor>(requestLifestyle); //Identity _container.Register <IGerenciadorDeUsuario, GerenciadorDeUsuario>(requestLifestyle); _container.Register <ApplicationUserManager>(Lifestyle.Scoped); _container.Register <ApplicationSignInManager>(Lifestyle.Scoped); _container.RegisterPerWebRequest <IUserStore <ApplicationUser> >(() => new UserStore <ApplicationUser>(new ApplicationDbContext())); }
public void DisposeInstances_ListWithOneItem_DisposesItem() { // Arrange var disposable = new DisposableObject(); var disposables = new List <IDisposable> { disposable }; // Act ScopedLifestyle.DisposeInstances(disposables); // Assert Assert.AreEqual(1, disposable.DisposeCount); }
public void WhenScopeEnds_CalledOnCreatedScopedHybridWithFalseSelector_ForwardsCallToFalseLifestyle() { // Arrange CustomScopedLifestyle trueLifestyle = new CustomScopedLifestyle(); CustomScopedLifestyle falseLifestyle = new CustomScopedLifestyle(); ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => false, trueLifestyle, falseLifestyle); // Act hybrid.WhenScopeEnds(new Container(), () => { }); // Assert Assert.AreEqual(0, trueLifestyle.WhenScopeEndsCallCount, "TrueLifestyle was NOT expected to be called."); Assert.AreEqual(1, falseLifestyle.WhenScopeEndsCallCount, "FalseLifestyle was expected to be called."); }
public void RegisterForDisposal_CalledOnCreatedScopedHybridWithFalseSelector_ForwardsCallToFalseLifestyle() { // Arrange CustomScopedLifestyle trueLifestyle = new CustomScopedLifestyle(); CustomScopedLifestyle falseLifestyle = new CustomScopedLifestyle(); ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => false, trueLifestyle, falseLifestyle); // Act hybrid.RegisterForDisposal(new Container(), new DisposableObject()); // Assert Assert.AreEqual(0, trueLifestyle.RegisterForDisposalCallCount, "TrueLifestyle was NOT expected to be called."); Assert.AreEqual(1, falseLifestyle.RegisterForDisposalCallCount, "FalseLifestyle was expected to be called."); }
public void CreateRegistrationService_WithScopedLifestylesAlways_ReturnsARegistrationThatWrapsTheOriginalLifestyle() { // Arrange ScopedLifestyle expectedLifestyle = Lifestyle.CreateHybrid(() => true, new CustomScopedLifestyle(), new CustomScopedLifestyle()); var container = ContainerFactory.New(); // Act var registration = expectedLifestyle.CreateRegistration <IUserRepository>(() => null, container); // Assert Assert.AreEqual(expectedLifestyle, registration.Lifestyle); }
public void WhenScopeEnds_CalledOnCreatedScopedHybridWithTrueSelector_ForwardsCallToTrueLifestyle() { // Arrange CustomScopedLifestyle trueLifestyle = new CustomScopedLifestyle(); CustomScopedLifestyle falseLifestyle = new CustomScopedLifestyle(); ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => true, trueLifestyle, falseLifestyle); // Act hybrid.WhenScopeEnds(new Container(), () => { }); // Assert Assert.IsTrue(trueLifestyle.ScopeUsed, "TrueLifestyle was expected to be called."); Assert.IsFalse(falseLifestyle.ScopeUsed, "FalseLifestyle was NOT expected to be called."); }
public void RegisterForDisposal_CalledOnCreatedScopedHybridWithTrueSelector_ForwardsCallToTrueLifestyle() { // Arrange CustomScopedLifestyle trueLifestyle = new CustomScopedLifestyle(); CustomScopedLifestyle falseLifestyle = new CustomScopedLifestyle(); ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => true, trueLifestyle, falseLifestyle); // Act hybrid.RegisterForDisposal(new Container(), new DisposableObject()); // Assert Assert.IsTrue(trueLifestyle.ScopeUsed, "TrueLifestyle was expected to be called."); Assert.IsFalse(falseLifestyle.ScopeUsed, "FalseLifestyle was NOT expected to be called."); }
public void DisposeInstances_ListWithMultipleItems_DisposesAllItems() { // Arrange var disposables = new List <DisposableObject> { new DisposableObject(), new DisposableObject(), new DisposableObject() }; // Act ScopedLifestyle.DisposeInstances(disposables.Cast <IDisposable>().ToList()); // Assert Assert.IsTrue(disposables.All(d => d.DisposeCount == 1)); }
static void RegisterRepositories(ScopedLifestyle requestLifestyle) { _container.Register <BudgetContext>(requestLifestyle); _container.Register <IOrcamentoRepository>(() => new OrcamentoRepository(_container.GetInstance <BudgetContext>()), requestLifestyle); _container.Register <IItemValorRepository>(() => new ItemValorRepository(_container.GetInstance <BudgetContext>()), requestLifestyle); _container.Register <IItemSubValorRepository>(() => new ItemSubValorRepository(_container.GetInstance <BudgetContext>()), requestLifestyle); _container.Register <IOrcamentoReadOnlyRepository>(() => new OrcamentoReadOnlyRepository(), requestLifestyle); _container.Register <IItemValorReadOnlyRepository>(() => new ItemValorReadOnlyRepository(), requestLifestyle); _container.Register <IItemSubValorReadOnlyRepository>(() => new ItemSubValorReadOnlyRepository(), requestLifestyle); //Identity _container.Register <ApplicationDbContext>(Lifestyle.Scoped); _container.Register <IUsuarioRepository>(() => new UsuarioRepository(), requestLifestyle); }
internal static string TheServiceIsRequestedOutsideTheContextOfAScopedLifestyle(Type serviceType, ScopedLifestyle lifestyle) => string.Format(CultureInfo.InvariantCulture, "The {0} is registered as '{1}' lifestyle, but the instance is requested outside the " + "context of a {1}.", serviceType.ToFriendlyName(), lifestyle.Name);
public AttributeBasedLifestyleSelectionBehavior(ScopedLifestyle scopedLifestyle) { this.scopedLifestyle = scopedLifestyle; }