public void Dispose() { foreach (var datasource in DataSourceExtractor.DataSources) { DbSetupUtility.DropDb((dynamic)Sp.GetRequiredService(typeof(IDbContextFactory <>).MakeGenericType(datasource))); } }
public UnityAddonEfTest(UnityAddonTestFixture testFixture, bool isDefered) : base(testFixture, isDefered) { foreach (var datasource in DataSourceExtractor.DataSources) { DbSetupUtility.CreateDb((dynamic)Sp.GetRequiredService(typeof(IDbContextFactory <>).MakeGenericType(datasource))); } }
public void Instance() { var service = new Service(); ServicePostRegistry.AddSingleton(typeof(IService), service, null); Assert.Same(service, Sp.GetRequiredService <IService>()); }
public void FactorySingleton() { ServicePostRegistry.AddSingleton(typeof(IService), (sp, t, n) => new Service(), null); var a = Sp.GetRequiredService <IService>(); var b = Sp.GetRequiredService <IService>(); Assert.IsType <Service>(a); Assert.Same(a, b); }
public void ImplTypeSingleton() { ServicePostRegistry.AddSingleton(typeof(IService), typeof(Service), null); var a = Sp.GetRequiredService <IService>(); var b = Sp.GetRequiredService <IService>(); Assert.IsType <Service>(a); Assert.Same(a, b); }
public void FactoryTransient() { ServicePostRegistry.AddTransient(typeof(IService), (sp, t, n) => new Service(), null); var a = Sp.GetRequiredService <IService>(); var b = Sp.GetRequiredService <IService>(); Assert.IsType <Service>(a); Assert.IsType <Service>(b); Assert.NotSame(a, b); }
public void ImplTypeTransient() { ServicePostRegistry.AddTransient(typeof(IService), typeof(Service), null); var a = Sp.GetRequiredService <IService>(); var b = Sp.GetRequiredService <IService>(); Assert.IsType <Service>(a); Assert.IsType <Service>(b); Assert.NotSame(a, b); }
public void Unregister() { ServicePostRegistry.AddSingleton(typeof(IService), typeof(Service), null); var service = Sp.GetRequiredService <IService>(); Assert.True(Sp.IsRegistered <IService>()); Assert.False(service.Disposed); ServicePostRegistry.Unregister(typeof(IService), null); Assert.False(Sp.IsRegistered <IService>()); Assert.True(service.Disposed); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <CatalogDatabaseSettings>(Configuration.GetSection(nameof(CatalogDatabaseSettings))); services.AddSingleton <ICatalogDatabaseSettings>(Sp => Sp.GetRequiredService <IOptions <CatalogDatabaseSettings> >().Value); services.AddControllers(); services.AddTransient <ICatalogContext, CatalogContext>(); services.AddTransient <Catalog.API.Repositories.Interfaces.IProductRepository, Catalog.API.Repositories.ProductRepository>(); services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo() { Title = "Catalog Api", Version = "v1" }); }); }
public void Unregister(Type type, string name) { var container = Sp.UnityContainer; var beanDefContainer = Sp.GetRequiredService <IBeanDefinitionContainer>(); var bean = Sp.GetRequiredService(type, name); var beanDef = beanDefContainer.RemoveBeanDefinition(type, name); var matchedList = container.Registrations.Where(p => p.RegisteredType == beanDef.Type && p.Name == beanDef.Name); foreach (var registration in matchedList) { registration.LifetimeManager.RemoveValue(); container.RegisterFactory(beanDef.Type, beanDef.Name, (c, t, n) => { throw new NoSuchBeanDefinitionException($"Type {beanDef.Type} with name '{beanDef.Name}' is unregistered."); }, (IFactoryLifetimeManager)Activator.CreateInstance(registration.LifetimeManager.GetType())); } }
public TDbContext GetDbContext <TDbContext>() where TDbContext : DbContext { return(Sp.GetRequiredService <IDbContextFactory <TDbContext> >().Get()); }