Best practices and architecture

Slides:



Advertisements
Similar presentations
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
Advertisements

Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Version Control Systems
Auto Mapping Objects SoftUni Team Database Applications
Introduction to Entity framework
Databases basics Course Introduction SoftUni Team Databases basics
Interface Segregation / Dependency Inversion
C# MVC Frameworks – ASP.NET
Introduction to MVC SoftUni Team Introduction to MVC
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
Reflection SoftUni Team Technical Trainers Java OOP Advanced
C# Database Fundamentals with Microsoft SQL Server
Introduction to Entity Framework
Application Architecture, Redux
ASP.NET Integration Testing
ASP.NET Unit Testing Unit Testing Web API SoftUni Team ASP.NET
Classes, Properties, Constructors, Objects, Namespaces
Mocking tools for easier unit testing
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
C# Databases Advanced with Microsoft SQL Server
OOP Course - Virtual Trip
Spring Filters Spring Interceptors SoftUni Team Spring Interceptors
EF Relations Object Composition
Entity Framework: Code First
Entity Framework DB From Code, OOP Introduction
Data Definition and Data Types
Databases advanced Course Introduction SoftUni Team Databases advanced
Install and configure theme
Balancing Binary Search Trees, Rotations
Entity Framework: Relations
Fast String Manipulation
Functional Programming
The Right Way Control Flow
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
MVC Architecture, Symfony Framework for PHP Web Apps
Transactions in Entity Framework
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Best Practices and Architecture
C# Web Development Basics
How to connect natively?
Testing with Spring Boot
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Exporting and Importing Data
ASP.NET Filters SoftUni Team ASP.NET MVC Introduction
Making big SPA applications
Manual Mapping and AutoMapper Library
Functional Programming
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Exporting and Importing Data
Introduction to TypeScript & Angular
CSS Transitions and Animations
Train the Trainers Course
Iterators and Comparators
Spring Data Advanced Querying
Directives & Forms Capturing User Input SoftUni Team
Version Control Systems
JavaScript Frameworks & AngularJS
/^Hel{2}o\s*World\n$/
CSS Transitions and Animations
Iterators and Generators
Presentation transcript:

Best practices and architecture Entity Framework SoftUni Team Best practices and architecture Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents Why separate models/data/client. Repository pattern Unit of work pattern Service pattern © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Questions sli.do #Entity

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

Repository pattern

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.

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

Unit of work pattern

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.

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

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.

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

Overview Controller DTO Used by Service Transforms Uses Models Save Unit of work DB Entity Repo Repo Retrieve Retrieve Root Aggregate Repo Repo

Best practices and architecture in EF https://softuni.bg/courses/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 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 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.