Ejemplo n.º 1
0
 public void Dispose()
 {
     foreach (var datasource in DataSourceExtractor.DataSources)
     {
         DbSetupUtility.DropDb((dynamic)Sp.GetRequiredService(typeof(IDbContextFactory <>).MakeGenericType(datasource)));
     }
 }
Ejemplo n.º 2
0
 public UnityAddonEfTest(UnityAddonTestFixture testFixture, bool isDefered) : base(testFixture, isDefered)
 {
     foreach (var datasource in DataSourceExtractor.DataSources)
     {
         DbSetupUtility.CreateDb((dynamic)Sp.GetRequiredService(typeof(IDbContextFactory <>).MakeGenericType(datasource)));
     }
 }
Ejemplo n.º 3
0
        public void Instance()
        {
            var service = new Service();

            ServicePostRegistry.AddSingleton(typeof(IService), service, null);

            Assert.Same(service, Sp.GetRequiredService <IService>());
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
 // 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"
         });
     });
 }
Ejemplo n.º 10
0
        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()));
            }
        }
Ejemplo n.º 11
0
 public TDbContext GetDbContext <TDbContext>() where TDbContext : DbContext
 {
     return(Sp.GetRequiredService <IDbContextFactory <TDbContext> >().Get());
 }