Содержание
- 2. Repository and Unit of Work Design Patterns with Entity Framework
- 3. Патерн Repository Патерн Unit of Work
- 5. Repository Pattern Посередник між BLL та DAL(Data Source) рівнями Це рівень, який роздвляє Дані та Рівень
- 6. Рівень абстракції між Business Logic рівнемта Data Access рівнем. Ізолює програму від змін джерела даних. Полегшує
- 7. EF DbContext implementer public class SchoolContext : DbContext { public DbSet Courses {get;set;} public DbSet Departments
- 8. IGenericRepository public interface IGenericRepository where TEntity : class { IEnumerable Get( Expression > filter = null,
- 9. GenericRepository constructor public class GenericRepository : IGenericRepository where TEntity : class { internal SchoolContext context; internal
- 10. public virtual IEnumerable Get( Expression > filter = null, Func , IOrderedQueryable > orderBy = null,
- 11. public virtual TEntity GetByID(object id) { return dbSet.Find(id); } public virtual void Insert(TEntity entity) { dbSet.Add(entity);
- 12. public virtual void Update(TEntity entityToUpdate) { dbSet.Attach(entityToUpdate); context.Entry(entityToUpdate).State = EntityState.Modified; } Імплементація CRUD методів
- 13. Unit of Work (UoW) Патерни об’єктно-реляційної поведінки: • Unit of Work • Identity Map • Lazy
- 16. A possible project structure
- 17. IUnitOfWork interface public interface IUnitOfWork { GenericRepository DepartmentRepository {get;} GenericRepository CourseRepository {get;} GenericRepository PersonRepository{get;} GenericRepository StudentRepository{get;}
- 18. UnitofWork.cs public class UnitOfWork : IUnitOfWork, IDisposable { private SchoolContext context = new SchoolContext(); private GenericRepository
- 19. GenericRepository Getters public GenericRepository DepartmentRepository { get { if (this.departmentRepository == null) { this.departmentRepository = new
- 20. Persist DbContext changes and clean up resources public void Save() { context.SaveChanges(); } private bool disposed
- 21. How does the architectural wiring come live in the controller ? public class CourseController { private
- 22. Sample Edit, Get(…) calls public ActionResult Edit( [Bind(Include = "CourseID,Title,Credits,DepartmentID")] Course course) { try { if
- 23. private void PopulateDepartmentsDropDownList(object selectedDepartment = null) { var departmentsQuery = unitOfWork.DepartmentRepository.Get( orderBy: q => q.OrderBy(d =>
- 24. References http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/advanced-entity-framework-scenarios-for-an-mvc-web-application#repo
- 26. Скачать презентацию