public void ConfigureContainerBuilder(bool writeLogsToTestOutput = false, bool writeLogsToFile = false, bool logDotNettyTraffic = false) { SocketPortHelper.AlterConfigurationToGetUniquePort(ConfigurationRoot); var configurationModule = new ConfigurationModule(ConfigurationRoot); ContainerBuilder.RegisterModule(configurationModule); ContainerBuilder.RegisterModule(new CoreLibProvider()); ContainerBuilder.RegisterInstance(ConfigurationRoot).As <IConfigurationRoot>(); var repoFactory = RepositoryFactory.BuildSharpRepositoryConfiguation(ConfigurationRoot.GetSection("CatalystNodeConfiguration:PersistenceConfiguration")); ContainerBuilder.RegisterSharpRepository(repoFactory); var passwordReader = new TestPasswordReader(); ContainerBuilder.RegisterInstance(passwordReader).As <IPasswordReader>(); var certificateStore = new TestCertificateStore(); ContainerBuilder.RegisterInstance(certificateStore).As <ICertificateStore>(); ContainerBuilder.RegisterInstance(_fileSystem).As <IFileSystem>(); var keyRegistry = TestKeyRegistry.MockKeyRegistry(); ContainerBuilder.RegisterInstance(keyRegistry).As <IKeyRegistry>(); ConfigureLogging(writeLogsToTestOutput, writeLogsToFile, logDotNettyTraffic); }
public static Container Run() { var container = new Container(x => { x.Scan(scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); }); var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json") .Build(); var sectionName = "sharpRepository"; IConfigurationSection sharpRepoSection = config.GetSection(sectionName); if (sharpRepoSection == null) { throw new ConfigurationErrorsException("Section " + sectionName + " is not found."); } var sharpRepoConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(sharpRepoSection); x.ForRepositoriesUseSharpRepository(sharpRepoConfig); }); return(container); }
public Kernel BuildKernel(bool overwrite = false, string overrideNetworkFile = null) { _overwrite = overwrite; _configCopier.RunConfigStartUp(_targetConfigFolder, NetworkType.Devnet, null, _overwrite, overrideNetworkFile); var config = _configurationBuilder.Build(); var configurationModule = new ConfigurationModule(config); ContainerBuilder.RegisterInstance(config); ContainerBuilder.RegisterModule(configurationModule); if (!string.IsNullOrEmpty(_withPersistence)) { var repoFactory = RepositoryFactory.BuildSharpRepositoryConfiguation(config.GetSection(_withPersistence)); ContainerBuilder.RegisterSharpRepository(repoFactory); } Logger = new LoggerConfiguration() .ReadFrom .Configuration(configurationModule.Configuration).WriteTo .File(Path.Combine(_targetConfigFolder, _fileName), rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] ({MachineName}/{ThreadId}) {Message} ({SourceContext}){NewLine}{Exception}") .CreateLogger() .ForContext(MethodBase.GetCurrentMethod().DeclaringType); ContainerBuilder.RegisterLogger(Logger); Log.Logger = Logger; return(this); }
/// <summary> /// Registers in unity container all IRepository and ICompoundKeyRepository resolutions. /// </summary> /// <param name="container"></param> /// <param name="configuration"></param> /// <param name="repositoryName"></param> /// <param name="lifetimeScopeTag">Accepts any MatchingScopeLifetimeTags scope enum tag</param> public static void RegisterSharpRepository(this IUnityContainer container, IConfigurationSection configurationSection, string repositoryName = null) { if (configurationSection == null) throw new ConfigurationErrorsException("Configuration section not found."); var configuration = RepositoryFactory.BuildSharpRepositoryConfiguation(configurationSection); container.RegisterSharpRepository(configuration, repositoryName); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddDbContext <ContactContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")), ServiceLifetime.Transient); // services.AddTransient<DbContext, ContactContext>(); // needed if you don't write dbContextClass on json configuration services.AddTransient <EmailRepository>(r => new EmailRepository(RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository")), "efCore")); }
public void Network_Config_Should_Contain_a_valid_storage_module(string networkConfig) { var networkConfiguration = new ConfigurationBuilder().AddJsonFile(networkConfig).Build(); var configurationSection = networkConfiguration .GetSection("CatalystNodeConfiguration:PersistenceConfiguration"); var persistenceConfiguration = RepositoryFactory.BuildSharpRepositoryConfiguation(configurationSection); persistenceConfiguration.HasRepository.Should().BeTrue(); persistenceConfiguration.DefaultRepository.Should().NotBeNullOrEmpty(); persistenceConfiguration.DefaultRepository.Should().Be("inMemoryNoCaching"); }
public static void BindSharpRepository(this IKernel kernel, IConfigurationSection configurationSection, string repositoryName = null) { if (configurationSection == null) { throw new ConfigurationErrorsException("Configuration section not found."); } var configuration = RepositoryFactory.BuildSharpRepositoryConfiguation(configurationSection); kernel.BindSharpRepository(configuration, repositoryName); }
public static IServiceProvider UseSharpRepository(this IServiceCollection services, IConfigurationSection configurationSection, string repositoryName = null) { if (configurationSection == null) { throw new ConfigurationErrorsException("Configuration section not found."); } var sharpRepoConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(configurationSection); return(services.UseSharpRepository(sharpRepoConfig, repositoryName)); }
/// <summary> /// Configures StructureMap container telling to resolve IRepository and ICompoundKeyRepository with the repository from configuration /// </summary> /// <param name="init"></param> /// <param name="configuration"></param> /// <param name="repositoryName">name of repository implementation in configuration, null tell to use default in configuration</param> /// <param name="lifecycle">StructureMap coping of variables default is Lifecycle.Transient</param> public static void ForRepositoriesUseSharpRepository(this ConfigurationExpression init, IConfigurationSection configurationSection, string repositoryName = null, ILifecycle lifeCycle = null) { if (configurationSection == null) { throw new ConfigurationErrorsException("Configuration section not found."); } var configuration = RepositoryFactory.BuildSharpRepositoryConfiguation(configurationSection); init.ForRepositoriesUseSharpRepository(configuration, repositoryName, lifeCycle); }
/// <summary> /// Registers in autofac container all IRepository and ICompoundKeyRepository resolutions. /// </summary> /// <param name="container"></param> /// <param name="configuration"></param> /// <param name="repositoryName"></param> /// <param name="lifetimeScopeTag">Accepts any MatchingScopeLifetimeTags scope enum tag</param> public static void RegisterSharpRepository(this ContainerBuilder containerBuilder, IConfigurationSection configurationSection, string repositoryName = null, params object[] lifetimeScopeTag) { if (configurationSection == null) { throw new ConfigurationErrorsException("Configuration section not found."); } var configuration = RepositoryFactory.BuildSharpRepositoryConfiguation(configurationSection); containerBuilder.RegisterSharpRepository(configuration, repositoryName, lifetimeScopeTag); }
/// <summary> /// Configures DependencyResolver with configured SharpRepository as implementation of IRepository and ICompoundRepository instances /// </summary> /// <param name="jsonConfigurationFileName"></param> /// <param name="sharpRepositoryConfigurationSectionName"></param> /// <param name="repositoryName">name of repository implementation in configuration, null tell to use default in configuration</param> /// <param name="lifecycle">StructureMap coping of variables default is Lifecycle.Transient</param> public static void ForRepositoriesUseSharpRepository(string jsonConfigurationFileName, string sharpRepositoryConfigurationSectionName, string repoisitoryName = null, ILifecycle lifecycle = null) { var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile(jsonConfigurationFileName) .Build(); var section = config.GetSection(sharpRepositoryConfigurationSectionName); var sharpConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(section); ForRepositoriesUseSharpRepository(sharpConfig, repoisitoryName, lifecycle); }
static SharpRepoConfig() { var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json") .Build(); var sectionName = "sharpRepository"; //var repositoryName = "efCore"; IConfigurationSection sharpRepoSection = config.GetSection(sectionName); sharpRepoConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(sharpRepoSection); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddDbContext <ContactContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")), ServiceLifetime.Transient); // services.AddTransient<DbContext, ContactContext>(); // needed if you don't write dbContextClass on json configuration services.AddTransient <EmailRepository>(r => new EmailRepository(RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository")), "efCore")); // return services.UseSharpRepository(Configuration.GetSection("sharpRepository")); //default InMemory // return services.UseSharpRepository(Configuration.GetSection("sharpRepository"), "mongoDb"); // for Mongo Db return(services.UseSharpRepository(Configuration.GetSection("sharpRepository"), "efCore")); // for Ef Core }
public void Setup() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json") .Build(); var sectionName = "sharpRepository"; IConfigurationSection sharpRepoSection = config.GetSection(sectionName); if (sharpRepoSection == null) { throw new ConfigurationErrorsException("Section " + sectionName + " is not found."); } var sharpRepoConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(sharpRepoSection); sharpRepoConfig.DefaultRepository = "efCoreRepos"; var memoryCache = new MemoryCache(new MemoryCacheOptions()); var dbContext = new TestObjectContextCore(options); // structure map container = new Container(x => { x.Scan(_ => { _.TheCallingAssembly(); _.WithDefaultConventions(); }); x.For <DbContext>() .Use(dbContext); x.For <TestObjectContextCore>() .Use(dbContext); x.For <IMemoryCache>().Use(memoryCache); x.ForRepositoriesUseSharpRepository(sharpRepoConfig); }); RepositoryDependencyResolver.SetDependencyResolver(new StructureMapProvider(container)); }
static void Main(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("repository.json") .Build(); var section = config.GetSection("sharpRepository"); ISharpRepositoryConfiguration sharpConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(section); // setting up services var services = new ServiceCollection(); // add dbcontext to container before configuration to sharprepository services.AddDbContext <MyDbContext>(options => options.UseSqlServer("server=localhost;user id=sa;password=secret;database=employee"), ServiceLifetime.Transient); services.UseSharpRepository(section); // add this lines for repo2 services.AddSingleton(sharpConfig); services.AddTransient <EmployeeRepository>(); var serviceProvider = services.BuildServiceProvider(); // this need dbcontext from sharprepository DI var repo1 = new EmployeeRepository(sharpConfig); repo1.Add(new Employee { Name = "Sven Svensson" }); // this needs EmployeeRepository and ISharpRepositoryConfiguration configured var repo2 = serviceProvider.GetService <EmployeeRepository>(); repo2.Add(new Employee { Name = "Sven Svensson" }); // this is not working yet, there some different how service provider is configured here ASP.NET Core var repo3 = serviceProvider.GetService <IRepository <Employee, int> >(); repo3.Add(new Employee { Name = "Sven Svensson" }); }
public void InMemoryLoadConfigurationRepositoryByName() { var sectionName = "sharpRepository2"; var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json") .Build(); var sharpRepoConfig2 = config.GetSection(sectionName); if (sharpRepoConfig2 == null) { throw new ConfigurationErrorsException("Section " + sectionName + " is not found."); } var sharpConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(sharpRepoConfig2); var repos = RepositoryFactory.GetInstance <Contact, string>(sharpConfig, "inMem"); if (!(repos is InMemoryRepository <Contact, string>)) { throw new Exception("Not EfRepository"); } }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddOData(); services.AddMvc(options => { options.Filters.Add(new NavigationFilter()); }).AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); }); services.Configure <FormOptions>(x => { x.ValueLengthLimit = int.MaxValue; x.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart }); services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddAuthentication(sharedOptions => { sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; }) .AddOpenIdConnect(option => { option.ClientId = Configuration["AzureAD:ClientId"]; option.Authority = String.Format(Configuration["AzureAd:AadInstance"], Configuration["AzureAd:Tenant"]); option.SignedOutRedirectUri = Configuration["AzureAd:PostLogoutRedirectUri"]; option.Events = new OpenIdConnectEvents { OnRemoteFailure = OnAuthenticationFailed, }; }) .AddCookie(); // ABARROS -> ADD NAV CONFIGURATIONS TO THE SERVICE var NAVConfigurations = Configuration.GetSection("NAVConfigurations"); services.Configure <NAVConfigurations>(NAVConfigurations); // ABARROS -> ADD NAV WS CONFIGURATIONS TO THE SERVICE var NAVWSConfigurations = Configuration.GetSection("NAVWSConfigurations"); services.Configure <NAVWSConfigurations>(NAVWSConfigurations); // ABARROS -> ADD NAV CONFIGURATIONS TO THE SERVICE var GeneralConfigurations = Configuration.GetSection("GeneralConfigurations"); services.Configure <GeneralConfigurations>(GeneralConfigurations); // ABARROS -> Activate Session Variables services.AddSession(s => s.IdleTimeout = TimeSpan.FromMinutes(30)); Data.Database.SuchDBContext.ConnectionString = Configuration.GetConnectionString("DefaultConnection"); /*sharpRepository for evolution database - IoC*/ services.AddDbContext <EvolutionWEBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EvolutionConnection")), ServiceLifetime.Transient); services.AddTransient <MaintenanceOrdersRepository>(r => { return(new MaintenanceOrdersRepository(RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository")))); }); services.AddTransient <MaintenanceOrdersLineRepository>(r => new MaintenanceOrdersLineRepository(RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository")))); return(services.UseSharpRepository(Configuration.GetSection("sharpRepository"))); }