Skip to content

Commit

Permalink
refactored SeedData
Browse files Browse the repository at this point in the history
  • Loading branch information
alimon808 committed Sep 20, 2017
1 parent 6dd1aa8 commit 6e25c7f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 149 deletions.
5 changes: 3 additions & 2 deletions ContosoUniversity.Data/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ContosoUniversity.Data.Entities;
using ContosoUniversity.Data.Enums;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
Expand All @@ -8,12 +9,12 @@ namespace ContosoUniversity.Data
{
public static class DbInitializer
{
public static void Initialize(ApplicationContext context, ILoggerFactory loggerFactory)
public static void Initialize(ApplicationContext context, ILoggerFactory loggerFactory, SampleData data)
{
context.Database.EnsureCreated();

var unitOfWork = new UnitOfWork(context);
var seedData = new SeedData(loggerFactory.CreateLogger("SeedData"), unitOfWork);
var seedData = new SeedData(loggerFactory.CreateLogger("SeedData"), unitOfWork, data);
seedData.Initialize();
}
}
Expand Down
17 changes: 17 additions & 0 deletions ContosoUniversity.Data/SampleData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ContosoUniversity.Data.DTO;
using System.Collections.Generic;

namespace ContosoUniversity.Data
{
public class SampleData
{
public List<EnrollmentDTO> Enrollments { get; set; }
public List<CourseDTO> Courses { get; set; }
public List<CourseAssignmentsDTO> CourseAssignments { get; set; }
public List<DepartmentDTO> Departments { get; set; }
public List<StudentDTO> Students { get; set; }
public List<OfficeAssignmentDTO> OfficeAssignments { get; set; }
public List<InstructorDTO> Instructors { get; set; }
public bool SaveToExternalFile { get; set; }
}
}
195 changes: 50 additions & 145 deletions ContosoUniversity.Data/SeedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
using ContosoUniversity.Data.Entities;
using System.Linq;
using ContosoUniversity.Data.Enums;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections;
using ContosoUniversity.Data.DTO;
using Microsoft.Extensions.Configuration;

namespace ContosoUniversity.Data
{
public class SeedData
{
private readonly UnitOfWork _unitOfWork;
private readonly ILogger _logger;
private readonly SampleData _data;

public SeedData(ILogger logger, UnitOfWork unitOfWork)
public SeedData(ILogger logger, UnitOfWork unitOfWork, SampleData data)
{
_unitOfWork = unitOfWork;
_logger = logger;
_data = data;
}

public void Initialize()
Expand All @@ -36,69 +45,18 @@ private void InitializeEnrollments()
{
return;
}

InitializeStudents();
InitializeCourses();

var students = _unitOfWork.StudentRepository.GetAll().ToArray();
var courses = _unitOfWork.CourseRepository.GetAll().ToArray();
var enrollments = new Enrollment[]
var enrollments = _data.Enrollments.Select(e => new Enrollment
{
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alexander").ID,
CourseID = courses.Single(c => c.Title == "Chemistry" ).ID,
Grade = Grade.A
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alexander").ID,
CourseID = courses.Single(c => c.Title == "Microeconomics" ).ID,
Grade = Grade.C
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alexander").ID,
CourseID = courses.Single(c => c.Title == "Macroeconomics" ).ID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alonso").ID,
CourseID = courses.Single(c => c.Title == "Calculus" ).ID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alonso").ID,
CourseID = courses.Single(c => c.Title == "Trigonometry" ).ID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Alonso").ID,
CourseID = courses.Single(c => c.Title == "Composition" ).ID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Anand").ID,
CourseID = courses.Single(c => c.Title == "Chemistry" ).ID
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Anand").ID,
CourseID = courses.Single(c => c.Title == "Microeconomics").ID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Barzdukas").ID,
CourseID = courses.Single(c => c.Title == "Chemistry").ID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Li").ID,
CourseID = courses.Single(c => c.Title == "Composition").ID,
Grade = Grade.B
},
new Enrollment {
StudentID = students.Single(s => s.LastName == "Justice").ID,
CourseID = courses.Single(c => c.Title == "Literature").ID,
Grade = Grade.B
}
};
CourseID = e.CourseID,
StudentID = e.StudentID,
Grade = e.Grade
});

_logger.LogInformation("Seeding enrollment table");
foreach (Enrollment e in enrollments)
Expand All @@ -122,41 +80,11 @@ private void InitializeCourseAssignment()
var instructors = _unitOfWork.InstructorRepository.GetAll().ToArray();
var courses = _unitOfWork.CourseRepository.GetAll().ToArray();

var courseAssignments = new CourseAssignment[]
var courseAssignments = _data.CourseAssignments.Select(ca => new CourseAssignment
{
new CourseAssignment {
CourseID = courses.Single(c => c.Title == "Chemistry" ).ID,
InstructorID = instructors.Single(i => i.LastName == "Kapoor").ID
},
new CourseAssignment {
CourseID = courses.Single(c => c.Title == "Chemistry" ).ID,
InstructorID = instructors.Single(i => i.LastName == "Harui").ID
},
new CourseAssignment {
CourseID = courses.Single(c => c.Title == "Microeconomics" ).ID,
InstructorID = instructors.Single(i => i.LastName == "Zheng").ID
},
new CourseAssignment {
CourseID = courses.Single(c => c.Title == "Macroeconomics" ).ID,
InstructorID = instructors.Single(i => i.LastName == "Zheng").ID
},
new CourseAssignment {
CourseID = courses.Single(c => c.Title == "Calculus" ).ID,
InstructorID = instructors.Single(i => i.LastName == "Fakhouri").ID
},
new CourseAssignment {
CourseID = courses.Single(c => c.Title == "Trigonometry" ).ID,
InstructorID = instructors.Single(i => i.LastName == "Harui").ID
},
new CourseAssignment {
CourseID = courses.Single(c => c.Title == "Composition" ).ID,
InstructorID = instructors.Single(i => i.LastName == "Abercrombie").ID
},
new CourseAssignment {
CourseID = courses.Single(c => c.Title == "Literature" ).ID,
InstructorID = instructors.Single(i => i.LastName == "Abercrombie").ID
}
};
InstructorID = ca.InstructorID,
CourseID = ca.CourseID
});

_logger.LogInformation("Seeding CourseAssignment table");
foreach (CourseAssignment c in courseAssignments)
Expand All @@ -173,16 +101,15 @@ private void InitializeOfficeAssignment()
{
return;
}

InitializeInstructors();

var instructors = _unitOfWork.InstructorRepository.GetAll().ToArray();
var officeAssignments = new OfficeAssignment[]
var officeAssignments = _data.OfficeAssignments.Select(oa => new OfficeAssignment
{
new OfficeAssignment { InstructorID = instructors.Single( i => i.LastName == "Fakhouri").ID, Location = "Smith 17" },
new OfficeAssignment { InstructorID = instructors.Single( i => i.LastName == "Harui").ID, Location = "Gowan 27" },
new OfficeAssignment { InstructorID = instructors.Single( i => i.LastName == "Kapoor").ID, Location = "Thompson 304" },
};
InstructorID = oa.InstructorID,
Location = oa.Location
});

_logger.LogInformation("Seeding OfficeAssignment table");
foreach (OfficeAssignment o in officeAssignments)
Expand All @@ -201,32 +128,15 @@ private void InitializeCourses()
}

InitializeDeparments();

var departments = _unitOfWork.DepartmentRepository.GetAll().ToArray();
var courses = new Course[]
var courses = _data.Courses.Select(c => new Course
{
new Course {CourseNumber = 1050, Title = "Chemistry", Credits = 3,
DepartmentID = departments.Single( s => s.Name == "Engineering").ID
},
new Course {CourseNumber = 4022, Title = "Microeconomics", Credits = 3,
DepartmentID = departments.Single( s => s.Name == "Economics").ID
},
new Course {CourseNumber = 4041, Title = "Macroeconomics", Credits = 3,
DepartmentID = departments.Single( s => s.Name == "Economics").ID
},
new Course {CourseNumber = 1045, Title = "Calculus", Credits = 4,
DepartmentID = departments.Single( s => s.Name == "Mathematics").ID
},
new Course {CourseNumber = 3141, Title = "Trigonometry", Credits = 4,
DepartmentID = departments.Single( s => s.Name == "Mathematics").ID
},
new Course {CourseNumber = 2021, Title = "Composition", Credits = 3,
DepartmentID = departments.Single( s => s.Name == "English").ID
},
new Course {CourseNumber = 2042, Title = "Literature", Credits = 4,
DepartmentID = departments.Single( s => s.Name == "English").ID
}
};
CourseNumber = c.CourseNumber,
Title = c.Title,
Credits = c.Credits,
DepartmentID = c.DepartmentID
});

_logger.LogInformation("Seeding Course table");
foreach (Course c in courses)
Expand All @@ -244,17 +154,12 @@ private void InitializeStudents()
return;
}

var students = new Student[]
var students = _data.Students.Select(s => new Student
{
new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},
new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")},
new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")}
};
FirstMidName = s.FirstMidName,
LastName = s.LastName,
EnrollmentDate = s.EnrollmentDate
});

_logger.LogInformation("Seeding student table");
foreach (Student s in students)
Expand All @@ -263,6 +168,7 @@ private void InitializeStudents()
}

_unitOfWork.Commit();

}

private void InitializeDeparments()
Expand All @@ -275,19 +181,20 @@ private void InitializeDeparments()
InitializeInstructors();

var instructors = _unitOfWork.InstructorRepository.GetAll().ToArray();
var departments = new Department[]
var departments = _data.Departments.Select(d => new Department
{
new Department { Name = "English", Budget = 350000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single( i => i.LastName == "Abercrombie").ID },
new Department { Name = "Mathematics", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single( i => i.LastName == "Fakhouri").ID },
new Department { Name = "Engineering", Budget = 350000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single( i => i.LastName == "Harui").ID },
new Department { Name = "Economics", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single( i => i.LastName == "Kapoor").ID }
};
Name = d.Name,
Budget = d.Budget,
StartDate = d.StartDate,
InstructorID = d.InstructorID
});

_logger.LogInformation("Seeding department table");
foreach (Department d in departments)
{
_unitOfWork.DepartmentRepository.Add(d);
}

_unitOfWork.Commit();
}

Expand All @@ -298,14 +205,12 @@ private void InitializeInstructors()
return;
}

var instructors = new Instructor[]
var instructors = _data.Instructors.Select(i => new Instructor
{
new Instructor { FirstMidName = "Kim", LastName = "Abercrombie", HireDate = DateTime.Parse("1995-03-11") },
new Instructor { FirstMidName = "Fadi", LastName = "Fakhouri", HireDate = DateTime.Parse("2002-07-06") },
new Instructor { FirstMidName = "Roger", LastName = "Harui", HireDate = DateTime.Parse("1998-07-01") },
new Instructor { FirstMidName = "Candace", LastName = "Kapoor", HireDate = DateTime.Parse("2001-01-15") },
new Instructor { FirstMidName = "Roger", LastName = "Zheng", HireDate = DateTime.Parse("2004-02-12") }
};
FirstMidName = i.FirstMidName,
LastName = i.LastName,
HireDate = i.HireDate
});

_logger.LogInformation("Seeding instructor table");
foreach (Instructor i in instructors)
Expand Down
7 changes: 5 additions & 2 deletions ContosoUniversity.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Startup(IHostingEnvironment env)
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();

if (env.IsDevelopment())
Expand Down Expand Up @@ -78,6 +78,7 @@ public void ConfigureServices(IServiceCollection services)
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(1);
options.Lockout.MaxFailedAccessAttempts = 3;
});

services.Configure<AuthMessageSenderOptions>(Configuration);
services.Configure<SMSOptions>(Configuration);
}
Expand All @@ -90,8 +91,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

if (env.IsDevelopment())
{
var sampleData = new SampleData();
Configuration.GetSection("SampleData").Bind(sampleData);
app.UseDeveloperExceptionPage();
DbInitializer.Initialize(context, loggerFactory);
DbInitializer.Initialize(context, loggerFactory, sampleData);
}
else
{
Expand Down

0 comments on commit 6e25c7f

Please sign in to comment.