EF Unit Of Work
v3.0.0
| 나뭇가지 | 짓다 | 전개 |
|---|---|---|
| 주인 | ![]() | |
| 개발하다 | ![]() | N/A |
엔티티 프레임 워크 코어 3의 저장소 및 작업 패턴 구현.
Nuget 패키지를 설치하십시오. 논리 계층에 대한 추상화를 사용하고 다른 하나는 인프라 계층 또는 모 놀리 식 프로젝트와 함께 사용합니다.
dotnet add package QD.EntityFrameworkCore.UnitOfWork
dotnet add package QD.EntityFrameworkCore.UnitOfWork.Abstractions public class AppDbContext : DbContext , IDbContext
{
public DbSet < Product > Products { get ; set ; }
public AppDbContext ( DbContextOptions < AppDbContext > options ) : base ( options )
{
}
.. .
.. .
} public void ConfigureServices ( IServiceCollection services )
{
services . AddDbContext < AppDbContext > ( builder =>
{
.. .
} ) ;
#region Register UnitOfWorks
// Register a IUnitOfWork<AppDbContext>
_services . AddUnitOfWork < AppDbContext > ( ) ;
// Register a IUnitOfWork<AppDbContext> and IUnitOfWork
_services . AddUnitOfWork < AppDbContext > ( onlyGeneric : false ) ;
#endregion
// Optional, register custom repositories
_services . AddRepository < Product , ProductRepository > ( ) ;
_services . AddReadOnlyRepository < Product , ProductReadOnlyRepository > ( ) ;
} public class FancyService : IFancyService
{
private readonly IUnitOfWork < AppDbContext > _unitOfWork ;
public FancyService ( IUnitOfWork < AppDbContext > unitOfWork )
{
_unitOfWork = unitOfWork ;
}
public void DoFancyThing ( )
{
var productsRepository = _unitOfWork . GetRepository < Product > ( ) ;
var products = productsRepository . GetAll ( ) ;
.. .
// Use inserts, updates, deletes.
.. .
_unitOfWork . SaveChanges ( ) ;
}
} public void FancyFunction ( )
{
// These are some examples
// Array
IPagedCollection < Product > page1 = _productsRepository . GetPagedArray ( 20 , 0 , product => product . Price > 500 ) ;
IPagedCollection < Product > page2 = await _productsRepository . GetPagedArrayAsync (
pageSize : 10 ,
orderBy : query => query . OrderBy ( product => product . Price ) . ThenBy ( product => product . Name )
) ;
// Indexers
var a = page1 [ 1 ] . Price ;
var b = ( ( PagedArray < Product > ) page1 ) [ 2 ] . Price ;
.. .
// List
IPagedCollection < Product > page1 = _productsRepository . GetPagedList ( 20 , 0 , product => product . Price > 500 ) ;
IPagedCollection < Product > page2 = await _productsRepository . GetPagedListAsync (
pageSize : 10 ,
orderBy : query => query . OrderBy ( product => product . Price ) . ThenBy ( product => product . Name )
) ;
// Indexers
var a = page1 [ 1 ] . Price ;
var b = ( ( PagedList < Product > ) page1 ) [ 2 ] . Price ;
.. .
// Dictionary
IPagedCollection < KeyValuePair < Guid , Product > > page1 = _productsRepository . GetPagedDictionary ( product => product . Id , 20 , 0 , product => product . Price > 500 ) ;
IPagedCollection < KeyValuePair < Guid , Product > > page2 = await _productsRepository . GetPagedDictionaryAsync (
keySelector : product => product . Id ,
pageSize : 10 ,
orderBy : query => query . OrderBy ( product => product . Price ) . ThenBy ( product => product . Name )
) ;
// Indexers
var a = page1 [ 1 ] . Value . Price ;
var b = ( ( PagedDictionary < Guid , Product > ) page1 ) [ Guid . Parse ( "5926d548-c0b3-4c40-b37f-e89be7741024" ) ] . Price ;
}이전 방법은 동일합니다
GetAll ( predicate , orderBy ) . ToPagedArray ( pageSize , pageNumber ) ;
GetAll ( predicate , orderBy ) . ToPagedListAsync ( pageSize , pageNumber ) ;
GetAll ( predicate , orderBy ) . ToPagedDictionary ( keySelector , pageSize , pageNumber ) ;