/// <summary> /// Service Initialization /// </summary> /// <param name="empty">Creates a context without any departments</param> /// <returns></returns> private EmployeesService CreateService(bool empty = false, [CallerMemberName] string databaseName = "") { DbContextOptions <VogContext> contextOptions = new DbContextOptionsBuilder <VogContext>().UseInMemoryDatabase(databaseName: databaseName).Options; VogContext context = new VogContext(contextOptions); if (!empty) { context.Employees.Add(new Employee { Id = 1, DepartmentId = 1, FirstName = "Dan", LastName = "Loden", JobTitle = "Nature Inspector", MailingAddress = "4652 Bates Brothers Road Hilliard, OH 43026" }); context.Employees.Add(new Employee { Id = 2, DepartmentId = 1, FirstName = "Jennifer", LastName = "Evans", JobTitle = "Range Ecologist", MailingAddress = "4278 Walnut Avenue Newark, NJ 07102" }); context.Employees.Add(new Employee { Id = 3, DepartmentId = 2, FirstName = "Teresa", LastName = "Hill", JobTitle = "Construction Equipment Technician", MailingAddress = "3789 Cook Hill Road Stamford, CT 06995" }); context.Employees.Add(new Employee { Id = 4, DepartmentId = 2, FirstName = "Shane", LastName = "Headrick", JobTitle = "Construction Equipment Operator", MailingAddress = "1020 Yoho Valley Road Golden, BC V0A 1H0" }); context.SaveChanges(); } EmployeesRepository repository = new EmployeesRepository(context); return(new EmployeesService(repository)); }
/// <summary> /// Controller Initialization /// </summary> /// <param name="emptyEmployees">Creates a context without any departments</param> /// <returns></returns> private EmployeesController CreateController(bool emptyDepartments = false, bool emptyEmployees = false, [CallerMemberName] string databaseName = "") { IHost host = Host.CreateDefaultBuilder().Build(); ILogger <EmployeesController> logger = host.Services.GetRequiredService <ILogger <EmployeesController> >(); DbContextOptions <VogContext> contextOptions = new DbContextOptionsBuilder <VogContext>().UseInMemoryDatabase(databaseName: databaseName).Options; VogContext context = new VogContext(contextOptions); if (!emptyDepartments) { context.Departments.Add(new Department { Id = 1, Name = "Ecology Department", Address = "1865 Reserve St Campbellford, ON K0L 1L0" }); context.Departments.Add(new Department { Id = 2, Name = "Construction Department", Address = "2635 Hammarskjold Dr Burnaby, BC V5B 3C9" }); context.SaveChanges(); } if (!emptyEmployees) { context.Employees.Add(new Employee { Id = 1, DepartmentId = 1, FirstName = "Dan", LastName = "Loden", JobTitle = "Nature Inspector", MailingAddress = "4652 Bates Brothers Road Hilliard, OH 43026" }); context.Employees.Add(new Employee { Id = 2, DepartmentId = 1, FirstName = "Jennifer", LastName = "Evans", JobTitle = "Range Ecologist", MailingAddress = "4278 Walnut Avenue Newark, NJ 07102" }); context.Employees.Add(new Employee { Id = 3, DepartmentId = 2, FirstName = "Teresa", LastName = "Hill", JobTitle = "Construction Equipment Technician", MailingAddress = "3789 Cook Hill Road Stamford, CT 06995" }); context.Employees.Add(new Employee { Id = 4, DepartmentId = 2, FirstName = "Shane", LastName = "Headrick", JobTitle = "Construction Equipment Operator", MailingAddress = "1020 Yoho Valley Road Golden, BC V0A 1H0" }); context.SaveChanges(); } DepartmentsRepository departmentsRepository = new DepartmentsRepository(context); DepartmentsService departmentsService = new DepartmentsService(departmentsRepository); EmployeesRepository employeesRepository = new EmployeesRepository(context); EmployeesService employeesService = new EmployeesService(employeesRepository); return(new EmployeesController(logger, employeesService, departmentsService)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { #if DEBUG VogContext.InitializeContext(); services.AddDbContext <VogContext>(opt => opt.UseInMemoryDatabase("Vog"), ServiceLifetime.Singleton); #else services.AddDbContext <VogContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("SqlServerConnString")), ServiceLifetime.Singleton); #endif services.AddSingleton <IGenericRepository <Employee>, EmployeesRepository>(); services.AddSingleton <IGenericRepository <Department>, DepartmentsRepository>(); services.AddTransient <IEmployeesService, EmployeesService>(); services.AddTransient <IDepartmentsService, DepartmentsService>(); services.AddControllers(); services.AddSwaggerGen(); }
/// <summary> /// Service Initialization /// </summary> /// <param name="empty">Creates a context without any departments</param> /// <returns></returns> private DepartmentsService CreateService(bool empty = false, [CallerMemberName] string databaseName = "") { DbContextOptions <VogContext> contextOptions = new DbContextOptionsBuilder <VogContext>().UseInMemoryDatabase(databaseName: databaseName).Options; VogContext context = new VogContext(contextOptions); if (!empty) { context.Departments.Add(new Department { Id = 1, Name = "Ecology Department", Address = "1865 Reserve St Campbellford, ON K0L 1L0" }); context.Departments.Add(new Department { Id = 2, Name = "Construction Department", Address = "2635 Hammarskjold Dr Burnaby, BC V5B 3C9" }); context.SaveChanges(); } DepartmentsRepository repository = new DepartmentsRepository(context); return(new DepartmentsService(repository)); }