當(dāng)前位置:首頁 > IT技術(shù) > Windows編程 > 正文

《ASP.ENT Core 與 RESTful API 開發(fā)實(shí)戰(zhàn)》-- (第5章)-- 讀書筆記(中)
2021-10-22 16:54:12


第 5 章 使用 Entity Framework Core

5.3 重構(gòu)倉儲(chǔ)類

創(chuàng)建一個(gè)通用倉儲(chǔ)接口

namespace Library.API.Services
{
public interface IRepositoryBase<T>
{
Task<IEnumerable<T>> GetAllAsync();
Task<IEnumerable<T>> GetByConditionAsync(Expression<Func<T, bool>> expression);
void Create(T entity);
void Update(T entity);
void Delete(T entity);
Task<bool> SaveAsync();
}
}


繼續(xù)創(chuàng)建一個(gè)接口

namespace Library.API.Services
{
public interface IRepositoryBase2<T,TId>
{
Task<T> GetByIdAsync(TId id);
Task<bool> IsExistAsync(TId id);
}
}


添加 RepositoryBase 類并實(shí)現(xiàn)上面兩個(gè)接口

namespace Library.API.Services
{
public class RepositoryBase<T, TId> : IRepositoryBase<T>, IRepositoryBase2<T, TId> where T : class
{
public DbContext DbContext { get; set; }

public RepositoryBase(DbContext dbContext)
{
DbContext = dbContext;
}

public Task<IEnumerable<T>> GetAllAsync()
{
return Task.FromResult(DbContext.Set<T>().AsEnumerable());
}

public Task<IEnumerable<T>> GetByConditionAsync(Expression<Func<T, bool>> expression)
{
return Task.FromResult(DbContext.Set<T>().Where(expression).AsEnumerable());
}

public void Create(T entity)
{
DbContext.Set<T>().Add(entity);
}

public void Update(T entity)
{
DbContext.Set<T>().Update(entity);
}

public void Delete(T entity)
{
DbContext.Set<T>().Remove(entity);
}

public async Task<bool> SaveAsync()
{
return await DbContext.SaveChangesAsync() > 0;
}

public async Task<T> GetByIdAsync(TId id)
{
return await DbContext.Set<T>().FindAsync(id);
}

public async Task<bool> IsExistAsync(TId id)
{
return await DbContext.Set<T>().FindAsync(id) != null;
}
}
}


這里需要注意的是,EF Core 對(duì)于查詢的執(zhí)行采用延遲執(zhí)行的方法,只有遇到了實(shí)際需要結(jié)果的操作,查詢才會(huì)執(zhí)行,這些操作包括以下幾種類型:

  • 對(duì)結(jié)果使用 for 或 foreach 循環(huán)
  • 使用了 ToList()、ToArray() 和 ToDictionary() 等方法
  • 使用了 Single()、Count()、Average、First() 和 Max() 等方法

創(chuàng)建其他倉儲(chǔ)接口

public interface IAuthorRepository : IRepositoryBase<Author>, IRepositoryBase2<Author, Guid>


創(chuàng)建實(shí)現(xiàn)類

namespace Library.API.Services
{
public class AuthorRepository : RepositoryBase<Author, Guid>, IAuthorRepository
{
public AuthorRepository(DbContext dbContext) : base(dbContext)
{
}
}
}


以同樣的方式創(chuàng)建 IBookRepository 與 BookRepository

接著創(chuàng)建倉儲(chǔ)包裝器 IRepositoryWrapper 及其實(shí)現(xiàn)

namespace Library.API.Services
{
public interface IRepositoryWrapper
{
IBookRepository Book { get; }
IAuthorRepository Author { get; }
}
}

namespace Library.API.Services
{
public class RepositoryWrapper : IRepositoryWrapper
{
public LibraryDbContext LibraryDbContext { get; }

private IAuthorRepository _authorRepository = null;
private IBookRepository _bookRepository = null;

public RepositoryWrapper(LibraryDbContext libraryDbContext)
{
LibraryDbContext = libraryDbContext;
}

public IAuthorRepository Author => _authorRepository ?? new AuthorRepository(LibraryDbContext);
public IBookRepository Book => _bookRepository ?? new BookRepository(LibraryDbContext);
}
}


包裝器提供了所有倉儲(chǔ)接口的統(tǒng)一訪問方式,從而避免了單獨(dú)訪問每個(gè)倉儲(chǔ)接口

接下來要將包裝器放到容器中,在 ConfigureServices 注入

services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();


5.4 重構(gòu) Controller 和 Action

在重構(gòu)之前,引入對(duì)象映射庫 AutoMapper

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection


在 ConfigureServices 注入

services.AddAutoMapper(typeof(Startup));


為了 AutoMapper 正確執(zhí)行對(duì)象映射,需要?jiǎng)?chuàng)建一個(gè) Profile 類的派生類,用以說明映射的對(duì)象以及映射規(guī)則

namespace Library.API.Helpers
{
public class LibraryMappingProfile : Profile
{
public LibraryMappingProfile()
{
CreateMap<Author, AuthorDto>()
.ForMember(dest => dest.Age, config =>
config.MapFrom(src => src.BirthData.GetCurrentAge()));
CreateMap<Book, BookDto>();
CreateMap<AuthorForCreationDto, Author>();
CreateMap<BookForCreationDto, Book>();
CreateMap<BookForUpdateDto, Book>();
}
}
}


CreateMap 方法的兩個(gè)泛型參數(shù)分別指明對(duì)象映射中的源和目標(biāo),當(dāng)從數(shù)據(jù)庫中獲取數(shù)據(jù)時(shí),實(shí)體類為源,而 DTO 為目標(biāo);當(dāng)處理請(qǐng)求時(shí)相反

當(dāng)程序運(yùn)行時(shí),執(zhí)行 AddAutoMapper 方法時(shí)會(huì)掃描指定程序集中 Profile 類的派生類,并根據(jù)掃描結(jié)果生成映射規(guī)則

《ASP.ENT Core 與 RESTful API 開發(fā)實(shí)戰(zhàn)》-- (第5章)-- 讀書筆記(中)_javascript


本文摘自 :https://blog.51cto.com/u

開通會(huì)員,享受整站包年服務(wù)立即開通 >