internal DbConfiguration(ResolverChain appConfigChain, ResolverChain normalResolverChain, RootDependencyResolver rootResolver) { Contract.Requires(appConfigChain != null); Contract.Requires(normalResolverChain != null); _rootResolver = rootResolver; _resolvers = new CompositeResolver<ResolverChain, ResolverChain>(appConfigChain, normalResolverChain); _resolvers.Second.Add(_rootResolver); }
public InternalConfiguration(ResolverChain appConfigChain, ResolverChain normalResolverChain, RootDependencyResolver rootResolver) { DebugCheck.NotNull(appConfigChain); DebugCheck.NotNull(normalResolverChain); _rootResolver = rootResolver; _resolvers = new CompositeResolver <ResolverChain, ResolverChain>(appConfigChain, normalResolverChain); _resolvers.Second.Add(_rootResolver); }
public void RootResolver_is_added_to_the_non_app_config_resolver_chain() { var normalChain = new ResolverChain(); var mockRootResolver = new Mock <RootDependencyResolver>(); new InternalConfiguration(new Mock <ResolverChain>().Object, normalChain, mockRootResolver.Object); normalChain.GetService <object>("Foo"); mockRootResolver.Verify(m => m.GetService(typeof(object), "Foo")); }
public InternalConfiguration( ResolverChain appConfigChain = null, ResolverChain normalResolverChain = null, RootDependencyResolver rootResolver = null, AppConfigDependencyResolver appConfigResolver = null) { _rootResolver = rootResolver ?? new RootDependencyResolver(); _resolvers = new CompositeResolver <ResolverChain, ResolverChain>(appConfigChain ?? new ResolverChain(), normalResolverChain ?? new ResolverChain()); _resolvers.Second.Add(_rootResolver); _resolvers.First.Add(appConfigResolver ?? new AppConfigDependencyResolver(AppConfig.DefaultInstance, this)); }
public void Adds_a_singleton_resolver_with_a_key() { var normalChain = new ResolverChain(); new InternalConfiguration( new Mock <ResolverChain>().Object, normalChain, new RootDependencyResolver(), new Mock <AppConfigDependencyResolver>().Object).RegisterSingleton("Bilbo", "Baggins"); Assert.Equal("Bilbo", normalChain.GetService <string>("Baggins")); Assert.Null(normalChain.GetService <string>("Biggins")); }
public void Adds_a_singleton_resolver_with_a_key() { var normalChain = new ResolverChain(); new InternalConfiguration( new Mock<ResolverChain>().Object, normalChain, new RootDependencyResolver(), new Mock<AppConfigDependencyResolver>().Object).RegisterSingleton("Bilbo", "Baggins"); Assert.Equal("Bilbo", normalChain.GetService<string>("Baggins")); Assert.Null(normalChain.GetService<string>("Biggins")); }
public void Adds_a_singleton_resolver_with_a_key_predicate() { var normalChain = new ResolverChain(); new InternalConfiguration( new Mock <ResolverChain>().Object, normalChain, new RootDependencyResolver(), new Mock <AppConfigDependencyResolver>().Object).RegisterSingleton("Bilbo", k => ((string)k).StartsWith("B")); Assert.Equal("Bilbo", normalChain.GetService <string>("Baggins")); Assert.Equal("Bilbo", normalChain.GetService <string>("Biggins")); Assert.Null(normalChain.GetService <string>("More than half a Brandybuck")); }
public void Adds_a_singleton_resolver_with_a_key_predicate() { var normalChain = new ResolverChain(); new InternalConfiguration( new Mock<ResolverChain>().Object, normalChain, new RootDependencyResolver(), new Mock<AppConfigDependencyResolver>().Object).RegisterSingleton("Bilbo", k => ((string)k).StartsWith("B")); Assert.Equal("Bilbo", normalChain.GetService<string>("Baggins")); Assert.Equal("Bilbo", normalChain.GetService<string>("Biggins")); Assert.Null(normalChain.GetService<string>("More than half a Brandybuck")); }
public void Resolvers_property_returns_resolvers_in_same_order_that_they_were_added() { var resolvers = new[] { new Mock <IDbDependencyResolver>().Object, new Mock <IDbDependencyResolver>().Object, new Mock <IDbDependencyResolver>().Object, }; var chain = new ResolverChain(); resolvers.Each(chain.Add); Assert.Equal(resolvers, chain.Resolvers); }
public void GetService_returns_null_if_no_resolver_in_the_chain_resolves_the_dependency() { var mockResolver1 = CreateMockResolver("Steve", new Mock <IPilkington>().Object); var mockResolver2 = CreateMockResolver("Ricky", new Mock <IPilkington>().Object); var chain = new ResolverChain(); chain.Add(mockResolver1.Object); chain.Add(mockResolver2.Object); Assert.Null(chain.GetService <IPilkington>("Karl")); mockResolver1.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once()); mockResolver2.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once()); }
/// <summary> /// This method is not thread-safe and should only be used to switch in a different root resolver /// before the configuration is locked and set. It is used for pushing a new configuration by /// DbContextInfo while maintaining legacy settings (such as database initializers) that are /// set on the root resolver. /// </summary> public virtual void SwitchInRootResolver(RootDependencyResolver value) { DebugCheck.NotNull(value); Debug.Assert(!_isLocked); // The following is not thread-safe but this code is only called when pushing a configuration // and happens to a new DbConfiguration before it has been set and locked. var newChain = new ResolverChain(); newChain.Add(value); _resolvers.Second.Resolvers.Skip(1).Each(newChain.Add); _rootResolver = value; _resolvers = new CompositeResolver <ResolverChain, ResolverChain>(_resolvers.First, newChain); }
public void GetService_and_Add_can_be_accessed_from_multiple_threads_concurrently() { for (var i = 0; i < 30; i++) { var bag = new ConcurrentBag <IPilkington>(); var resolver = new ResolverChain(); var karl = new Karl(); ExecuteInParallel( () => { resolver.Add(new SingletonDependencyResolver <IPilkington>(karl, "Karl")); bag.Add(resolver.GetService <IPilkington>("Karl")); }); Assert.Equal(20, bag.Count); Assert.True(bag.All(c => karl == c)); } }
public void GetService_returns_the_service_returned_by_the_most_recently_added_resolver_that_resolves_the_dependency() { var karl = new Mock <IPilkington>().Object; var mockResolver1 = CreateMockResolver("Karl", new Mock <IPilkington>().Object); var mockResolver2 = CreateMockResolver("Karl", karl); var mockResolver3 = CreateMockResolver("Ricky", new Mock <IPilkington>().Object); var chain = new ResolverChain(); chain.Add(mockResolver1.Object); chain.Add(mockResolver2.Object); chain.Add(mockResolver3.Object); Assert.Same(karl, chain.GetService <IPilkington>("Karl")); mockResolver1.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Never()); mockResolver2.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once()); mockResolver3.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once()); }
public void GetService_registers_SQL_Server_as_a_fallback_if_it_is_not_already_registered() { var providerTypeName = string.Format( CultureInfo.InvariantCulture, "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089", new AssemblyName(typeof(DbContext).Assembly.FullName).Version); var mockSqlProvider = new Mock <DbProviderServices>(); mockSqlProvider.Setup(m => m.GetService(typeof(string), null)).Returns("System.Data.SqlClient"); var mockSection = CreateMockSectionWithProviders(); var mockFactory = CreateMockFactory(mockSection.Object); mockFactory.Setup(m => m.TryGetInstance(providerTypeName)) .Returns(mockSqlProvider.Object); var appConfig = new AppConfig(new ConnectionStringSettingsCollection(), null, mockSection.Object, mockFactory.Object); var mockConfiguration = new Mock <InternalConfiguration>(null, null, null, null); var someRandomThing = new Random(); mockSqlProvider.Setup(m => m.GetService(typeof(Random), null)).Returns(someRandomThing); var resolvers = new ResolverChain(); mockConfiguration.Setup(m => m.AddSecondaryResolver(It.IsAny <IDbDependencyResolver>())) .Callback <IDbDependencyResolver>(resolvers.Add); new AppConfigDependencyResolver(appConfig, mockConfiguration.Object, mockFactory.Object).GetService <IPilkington>(); mockConfiguration.Verify(m => m.AddSecondaryResolver(It.IsAny <DbProviderServices>()), Times.Exactly(4)); mockConfiguration.Verify(m => m.AddSecondaryResolver(It.IsAny <SingletonDependencyResolver <DbProviderServices> >()), Times.Once()); Assert.Equal("Robot.Rock", resolvers.GetService <string>()); Assert.Same(someRandomThing, resolvers.GetService <Random>()); Assert.Same(mockSqlProvider.Object, resolvers.GetService <DbProviderServices>("System.Data.SqlClient")); }
public void ResolverSnapshot_returns_resolvers_in_correct_order() { var callOrder = ""; var configResolver = new Mock <IDbDependencyResolver>(); configResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Config"); var configChain = new ResolverChain(); configChain.Add(configResolver.Object); var rootResolver = new Mock <RootDependencyResolver>(); rootResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Root"); var configuration = new DbConfiguration( new InternalConfiguration( configChain, new ResolverChain(), rootResolver.Object, new Mock <AppConfigDependencyResolver>().Object)).InternalConfiguration; var normalResolver = new Mock <IDbDependencyResolver>(); normalResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Normal"); configuration.AddDependencyResolver(normalResolver.Object); var overrideResolver = new Mock <IDbDependencyResolver>(); overrideResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Override"); configuration.AddDependencyResolver(overrideResolver.Object, overrideConfigFile: true); configuration.ResolverSnapshot.GetService <object>("Foo"); Assert.Equal(" Override Config Normal Root", callOrder); }
/// <summary> /// This method is not thread-safe and should only be used to switch in a different root resolver /// before the configuration is locked and set. It is used for pushing a new configuration by /// DbContextInfo while maintaining legacy settings (such as database initializers) that are /// set on the root resolver. /// </summary> internal virtual void SwitchInRootResolver(RootDependencyResolver value) { Contract.Requires(value != null); Contract.Assert(!_isLocked); // The following is not thread-safe but this code is only called when pushing a configuration // and happens to a new DbConfiguration before it has been set and locked. var newChain = new ResolverChain(); newChain.Add(value); _resolvers.Second.Resolvers.Skip(1).Each(newChain.Add); _rootResolver = value; _resolvers = new CompositeResolver<ResolverChain, ResolverChain>(_resolvers.First, newChain); }
public void RootResolver_is_added_to_the_non_app_config_resolver_chain() { var normalChain = new ResolverChain(); var mockRootResolver = new Mock<RootDependencyResolver>(); new DbConfiguration(new Mock<ResolverChain>().Object, normalChain, mockRootResolver.Object); normalChain.GetService<object>("Foo"); mockRootResolver.Verify(m => m.GetService(typeof(object), "Foo")); }
public void ResolverSnapshot_returns_resolvers_in_correct_order() { var callOrder = ""; var configResolver = new Mock<IDbDependencyResolver>(); configResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Config"); var configChain = new ResolverChain(); configChain.Add(configResolver.Object); var rootResolver = new Mock<RootDependencyResolver>(); rootResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Root"); var configuration = new DbConfiguration( new InternalConfiguration( configChain, new ResolverChain(), rootResolver.Object, new Mock<AppConfigDependencyResolver>().Object)).InternalConfiguration; var normalResolver = new Mock<IDbDependencyResolver>(); normalResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Normal"); configuration.AddDependencyResolver(normalResolver.Object); var overrideResolver = new Mock<IDbDependencyResolver>(); overrideResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Override"); configuration.AddDependencyResolver(overrideResolver.Object, overrideConfigFile: true); configuration.ResolverSnapshot.GetService<object>("Foo"); Assert.Equal(" Override Config Normal Root", callOrder); }