Created
May 14, 2019 01:56
-
-
Save danielmackay/8573bed28b4bc0cac9b9b86eb8e4310b to your computer and use it in GitHub Desktop.
ASP.NET Core Configuration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Inspired from: https://platform.deloitte.com.au/articles/dependency-injections-on-azure-functions-v2 | |
/// </summary> | |
public class ContainerBuilder : IContainerBuilder | |
{ | |
private readonly IServiceCollection services; | |
public ContainerBuilder() | |
{ | |
services = new ServiceCollection(); | |
} | |
public IContainerBuilder RegisterModule(IModule module = null) | |
{ | |
if (module == null) | |
module = new Module(); | |
module.Load(services); | |
return this; | |
} | |
public IServiceProvider Build() | |
{ | |
var provider = services.BuildServiceProvider(); | |
return provider; | |
} | |
} | |
public interface IContainerBuilder | |
{ | |
IContainerBuilder RegisterModule(IModule module = null); | |
IServiceProvider Build(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Inspired from: https://platform.deloitte.com.au/articles/dependency-injections-on-azure-functions-v2 | |
/// </summary> | |
public class Module : IModule | |
{ | |
public virtual void Load(IServiceCollection services) | |
{ | |
return; | |
} | |
} | |
public interface IModule | |
{ | |
void Load(IServiceCollection services); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LicenceConfiguration : Module | |
{ | |
private IConfiguration Configuration; | |
public override void Load(IServiceCollection services) | |
{ | |
InitConfiguration(); | |
InitLogging(services); | |
InitDatabase(services); | |
InitOptions(services); | |
InitIoC(services); | |
} | |
private void InitConfiguration() | |
{ | |
Configuration = new ConfigurationBuilder() | |
.SetBasePath(Environment.CurrentDirectory) | |
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) | |
.AddEnvironmentVariables() | |
.Build(); | |
} | |
private static void InitLogging(IServiceCollection services) | |
{ | |
services.AddLogging(builder => | |
builder | |
.AddDebug() | |
.AddConsole() | |
); | |
} | |
private void InitDatabase(IServiceCollection services) | |
{ | |
services.AddDbContext<BoostedChartsContext>(options => | |
options.UseSqlServer( | |
Configuration.GetConnectionString("DefaultConnection"))); | |
} | |
private void InitOptions(IServiceCollection services) | |
{ | |
services.AddOptions(); | |
services.Configure<CustomerServiceOptions>(Configuration); | |
} | |
private void InitIoC(IServiceCollection services) | |
{ | |
services.AddScoped<ILicenceService, LicenceService>(); | |
services.AddScoped<ILicenceRepository, LicenceRepository>(); | |
services.AddScoped<IPlanRepository, PlanRepository>(); | |
services.AddScoped<IProductRepository, ProductRepository>(); | |
services.AddScoped<IUnitOfWork, UnitOfWork>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class LicenceFunction | |
{ | |
public static IServiceProvider Container = new ContainerBuilder() | |
.RegisterModule(new LicenceConfiguration()) | |
.Build(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment