Презентация

Best Practices and Architecture - Databases Advanced - Entity Framework - октомври 2016

 

Материали от темата

Съдържание на документа

Entity Framework

SoftUni Team

Technical Trainers

Software University

http://softuni.bg

Best practices and architecture




Table of Contents

Why separate models/data/client. 

Repository pattern

Unit of work pattern 

Service pattern



2


3



sli.do
#Entity

Questions


4

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

Why separate models/data/client. 

Data

Models

Client1

Client2

Client3


5

Repository pattern 


6

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.

What it is and why to use it? 


7

Example: 

 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);

    }

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.


8

Unit of work pattern 


9

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. 

What is unit of work and why to use it? 


10

 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();

    }

Example:


11

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.







What is service pattern and why to use it? 


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();

        }

    }




Models

13

Overview

Repo

Entity

Root Aggregate

DB

Save

Save

Retrieve

Retrieve

Service

DTO

Controller

Uses

Transforms

Used by

Repo

Repo

Repo

Unit of work


Best practices and architecture in EF 

https://softuni.bg/courses/


License

This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license

15

Attribution: this work may contain portions from

"Databases" course by Telerik Academy under CC-BY-NC-SA license


Free Trainings @ Software University

Software University Foundation – softuni.org

Software University – High-Quality Education, Profession and Job for Software Developers

softuni.bg 

	Software University @ Facebook

	facebook.com/SoftwareUniversity

	Software University @ YouTube

	youtube.com/SoftwareUniversity

	Software University Forums – forum.softuni.bg