Download presentation
Presentation is loading. Please wait.
1
Best practices and architecture
Entity Framework SoftUni Team Best practices and architecture Technical Trainers Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
2
Table of Contents Why separate models/data/client. Repository pattern
Unit of work pattern Service pattern © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
3
Questions sli.do #Entity
4
Why separate models/data/client.
Overall structure is more decoupled. Many clients can use the data and models, without knowing being coupled to things we might not use. Easier to maintain, debug and test Data Client1 Client2 Models Client3
5
Repository pattern
6
What it is and why to use it?
Repository pattern helps us make an abstraction over the DBSets, which can sometimes be an overengineering problem. Gives better decoupling and is easier to maintain. We can easily change the implementation of the provider/framework, without having to go through all it’s usages in our app. Gives us the possibility to write tests, for the things that we use and not everything that comes from the DbSet DbSet is often considered being a Repository patter implementation.
7
Example: Additionally in the implementation of the interface, the context should be passed, or it would be even more adequate to pass only the corresponding DbSet, through the constructor. public interface IRepository<TEntity> where TEntity : class { void Add(TEntity entity); void AddRange(IEnumerable<TEntity> entities); void Remove(TEntity entity); void RemoveRange(IEnumerable<TEntity> entities); IEnumerable<TEntity> GetAllWhere(Expression<Func<TEntity, bool>> where); TEntity GetFirstWhere(Expression<Func<TEntity>> where); }
8
Unit of work pattern
9
What is unit of work and why to use it?
A Unit of Work can be defined as a collection of operations that succeed or fail as a single unit (transactional). It is a place that unites all of our entity sets. Simplifies the passing of the entity set, by just passing the UOW, to the place, where we need it. Makes testing and maintainability easier. DbContext is often referred to being a Unit of work, because it has a transactional nature and holds the DbSets.
10
Example: public interface IUnitOfWork : IDisposable { IRepository<User> UsersRepository { get; } IRepository<Album> AlbumsRepository { get; } IRepository<Picture> PicturesRepository { get; } IRepository<Tag> TagsRepository { get; } IRepository<AlbumRole> AlbumRolesRepository { get; } IRepository<Town> TownsRepository { get; } void Commit(); }
11
What is service pattern and why to use it?
Services handle the business object related to Entities Services are used by Controllers Services handle DTO objects Services communicate with the repositories/unit of work, but usually implements the business logic. Easily maintainable and testable and of course decoupled from the repository.
12
Example public class EmployeesService {
private readonly UnitOfWork unit; public Employee GetEmployeeByName(string name) if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name), "Name should not be null or empty!"); } return this.unit.Employees .FindAll(employee => employee.FirstName == name) .FirstOrDefault();
13
Overview Controller DTO Used by Service Transforms Uses Models Save
Unit of work DB Entity Repo Repo Retrieve Retrieve Root Aggregate Repo Repo
14
Best practices and architecture in EF
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
15
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Databases" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
16
Free Trainings @ Software University
Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software Facebook facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.