public void ConfigureDependencyWithProvider() { string name = "John"; int age = 26; DateTime birthDate = new DateTime(1908, 8, 5); IDependencyCollection collection = new DependencyCollection(); collection.AddSingleton <Person>(); collection.Configure <Person>(p => { p.Name = name; p.Age = age; p.BirthDate = birthDate; }); collection.AddScoped <DependencyWithConfigure>(); collection.Configure <DependencyWithConfigure>((p, c) => { Person person = p.GetDependency <Person>(); c.MyString = person.Name; c.MyInt = person.Age; c.MyDate = person.BirthDate; }); using IDependencyProvider provider = collection.BuildProvider(); DependencyWithConfigure configureDependency = provider.GetDependency <DependencyWithConfigure>(); configureDependency.ShouldNotBeNull(); configureDependency.MyDate.ShouldBe(birthDate); configureDependency.MyInt.ShouldBe(age); configureDependency.MyString.ShouldBe(name); }
public void ConfigureDependency() { DateTime testDateTime = new DateTime(2000, 06, 01); int testInteger = 5; string testString = "Look, A wild string has appeared!"; IDependencyCollection collection = new DependencyCollection(); collection.AddSingleton <DependencyWithConfigure>(); collection.Configure <DependencyWithConfigure>(c => { c.MyDate = testDateTime; }); collection.Configure <DependencyWithConfigure>(c => { c.MyInt = testInteger; c.MyString = testString; }); using IDependencyProvider provider = collection.BuildProvider(); DependencyWithConfigure configureDependency = provider.GetDependency <DependencyWithConfigure>(); configureDependency.ShouldNotBeNull(); configureDependency.MyDate.ShouldBe(testDateTime); configureDependency.MyInt.ShouldBe(testInteger); configureDependency.MyString.ShouldBe(testString); }
public static DependencyCollection AddCommandProcessor <TCommand, TContext>( this DependencyCollection collection, Action <TCommand, TContext> processor) where TCommand : ICommand { return(collection.AddSingleton <ICommandProcessor <TCommand> >(ctx => new ActionCommandProcessor <TCommand, TContext>(processor, ctx.GetRequiredService <DependencyProvider>()))); }
public static DependencyCollection AddPool <T>(this DependencyCollection collection, Func <IDependencyScope, T> builder, int capacity = 10) where T : class { collection.AddSingleton <IPool <T> >(ctx => new Pool <T>(capacity, () => builder(ctx))); return(collection); }
public static DependencyCollection AddQueryProcessor <TQuery, TContext, TResult>( this DependencyCollection collection, Func <TQuery, TContext, TResult> processor) where TQuery : IQuery <TResult> { return(collection.AddSingleton <IQueryProcessor <TQuery, TResult> >(ctx => new ActionQueryProcessor <TQuery, TContext, TResult>(processor, ctx.GetRequiredService <DependencyProvider>()))); }
public static DependencyCollection AddJsonConfiguration(this DependencyCollection collection, Func <IDependencyScope, string> fileNameBuilder, bool required = false) { EnsureConfigurationRegistered(collection); collection.AddSingleton <IConfigurationSource>(ctx => new JsonFileSource(fileNameBuilder(ctx), required)); return(collection); }
private static void EnsureConfigurationRegistered(DependencyCollection collection) { if (!collection.Contains <IConfiguration>()) { collection .AddSingleton <IConfiguration, Configuration>() .AddFactory(new SettingsFactory()); } }
public static DependencyCollection AddLogging(this DependencyCollection collection) { collection .AddSingleton <IRendererCollection, RendererCollection>() .AddFactory(new LogProviderFactory()) .AddScoped(typeof(ILogger <>), typeof(Logger <>)); return(collection); }
// ReSharper disable once InconsistentNaming public static DependencyCollection AddECS(this DependencyCollection collection) { collection .AddSingleton <ActorContext>() .AddSingleton <AssetContext>() .AddSingleton <SystemService>() .AddSingleton <World>(); return(collection); }
public void Destroy() { var provider = _dependencies .AddSingleton <IMapper <Boo>, CompiledMapper <Boo> >() .AddSingleton <IBooRepository>(ctx => new BooRepository(ctx.Get <ISettingsProvider>(), ctx.Get <ISession>())) .AddSingleton <BooService>() .BuildProvider(); var repository = (BooRepository)provider.GetRequired <IBooRepository>(); var service = provider.GetRequired <BooService>(); provider.Dispose(); Assert.True(repository.Disposed); Assert.True(service.Disposed); }
public void ThrowSingletonDependencyExceptionInstantiateUnregistered() { IDependencyCollection collection = new DependencyCollection(); collection.AddScoped <TestDisposable>(); collection.AddSingleton <TestDependency>(); using ILifetimeScope scope = collection.CreateScope(); using IDependencyProvider provider = Should.NotThrow(scope.BuildProvider); Should.Throw <SingletonDependencyException>(provider.CreateInstance <UnregisteredDependency>); }
public static DependencyCollection AddServer(this DependencyCollection collection) { if (collection.Contains <HttpServer>()) { return(collection); } collection .AddSingleton <HttpServer>() .AddSingleton <HttpRouter>(); return(collection); }
private static void AddSingleton(DependencyCollection dependencies, ServiceDescriptor descriptor) { if (descriptor.ImplementationInstance != null) { dependencies.AddInstance(descriptor.ServiceType, descriptor.ImplementationInstance); } else if (descriptor.ImplementationFactory == null) { dependencies.AddSingleton(descriptor.ServiceType, descriptor.ImplementationType); } else { var contracts = new[] { descriptor.ServiceType }; var resolver = BuildDelegateResolver(descriptor); dependencies.AddDependency(new SingletonDependency(contracts, resolver)); } }
public void GetDependencyWithScope() { IDependencyCollection collection = new DependencyCollection(); collection.AddSingleton <DependencyWithScope, DependencyWithScope>(); using (ILifetimeScope scope = collection.CreateScope()) { using (IDependencyProvider provider = scope.BuildProvider()) { DependencyWithScope dependency = Should.NotThrow(provider.GetDependency <DependencyWithScope>); dependency.ShouldNotBeNull(); dependency.Scope.ShouldNotBeNull(); } } using (IDependencyProvider provider = collection.BuildProvider()) { Should.Throw <InvalidScopeException>(provider.GetDependency <DependencyWithScope>); } }
public void GetDependencyWithProvider() { IDependencyCollection collection = new DependencyCollection(); collection.AddScoped <DependencyWithProvider, DependencyWithProvider>(); using (IDependencyProvider provider = collection.BuildProvider()) { DependencyWithProvider dependency = Should.NotThrow(provider.GetDependency <DependencyWithProvider>); dependency.ShouldNotBeNull(); dependency.Provider.ShouldNotBeNull(); } collection = new DependencyCollection(); collection.AddSingleton <DependencyWithProvider, DependencyWithProvider>(); using (IDependencyProvider provider = collection.BuildProvider()) { Should.Throw <SingletonDependencyException>(provider.GetDependency <DependencyWithProvider>); } }
public void GetAddedSingleton() { TestDisposable disposableA; TestDisposable disposableB; TestDependency dependencyA; IDependencyCollection collection = new DependencyCollection(); collection.AddSingleton(() => new TestDisposable { IntValue = 171, StringValue = "Hello World" }); collection.AddSingleton <TestDependency, TestDependency>(); collection.TryAddSingleton(() => new TestDisposable()).ShouldBeFalse(); collection.TryAddSingleton <TestDependency, TestDependency>().ShouldBeFalse(); collection.HasDependency <TestDisposable>().ShouldBeTrue(); collection.HasDependency <TestDependency>().ShouldBeTrue(); using (ILifetimeScope scope = collection.CreateScope()) { using (IDependencyProvider provider = Should.NotThrow(scope.BuildProvider)) { provider.HasDependency <TestDisposable>().ShouldBeTrue(); provider.HasDependency <TestDependency>().ShouldBeTrue(); disposableA = Should.NotThrow(provider.GetDependency <TestDisposable>); disposableA.IsDisposed.ShouldBeFalse(); disposableA.IntValue.ShouldBe(171); disposableA.StringValue.ShouldBe("Hello World"); disposableA.IntValue = 1024; disposableA.StringValue = "No longer hello world"; disposableB = Should.NotThrow(provider.GetDependency <TestDisposable>); disposableB.IsDisposed.ShouldBeFalse(); disposableB.IntValue.ShouldBe(1024); disposableB.StringValue.ShouldBe("No longer hello world"); dependencyA = Should.NotThrow(provider.GetDependency <TestDependency>); dependencyA.TestDisposable.IsDisposed.ShouldBeFalse(); dependencyA.TestDisposable.IntValue.ShouldBe(1024); dependencyA.TestDisposable.StringValue.ShouldBe("No longer hello world"); } disposableA.IsDisposed.ShouldBeFalse(); disposableB.IsDisposed.ShouldBeFalse(); using (IDependencyProvider provider = Should.NotThrow(scope.BuildProvider)) { provider.HasDependency <TestDisposable>().ShouldBeTrue(); disposableA = Should.NotThrow(provider.GetDependency <TestDisposable>); disposableA.IsDisposed.ShouldBeFalse(); disposableA.IntValue.ShouldBe(1024); disposableA.StringValue.ShouldBe("No longer hello world"); dependencyA = Should.NotThrow(provider.GetDependency <TestDependency>); dependencyA.TestDisposable.IsDisposed.ShouldBeFalse(); dependencyA.TestDisposable.IntValue.ShouldBe(1024); dependencyA.TestDisposable.StringValue.ShouldBe("No longer hello world"); } disposableA.IsDisposed.ShouldBeFalse(); disposableB.IsDisposed.ShouldBeFalse(); } disposableA.IsDisposed.ShouldBeTrue(); disposableB.IsDisposed.ShouldBeTrue(); ILifetimeScope dependencyScope = collection.CreateScope(); IDependencyProvider dependencyProvider = dependencyScope.BuildProvider(); TestDisposable disposable = dependencyProvider.GetDependency <TestDisposable>(); disposable.IsDisposed.ShouldBeFalse(); dependencyScope.Dispose(); disposable.IsDisposed.ShouldBeTrue(); }