實現(xiàn)代碼
成都創(chuàng)新互聯(lián)公司是一家網(wǎng)站建設(shè)、網(wǎng)站制作,提供網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,網(wǎng)站制作,建網(wǎng)站,按需求定制設(shè)計,網(wǎng)站開發(fā)公司,2013年至今是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價值為核心業(yè)務(wù),全程參與項目的網(wǎng)站策劃設(shè)計制作,前端開發(fā),后臺程序制作以及后期項目運(yùn)營并提出專業(yè)建議和思路。
1、新建接口類:IRepository.cs,規(guī)范各個操作類的都有那些方法,方便管理。
using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Text;namespace CMS.Entity.Interfaces { public interface IRepositorywhere T:class { /// /// 添加 /// /// 實體對象 void Add(T entity); ////// 更新 /// /// 實體對象 void Update(T entity); ////// 刪除 /// /// 實體對象 void Delete(T entity); ////// 刪除 /// /// 條件(lambda表達(dá)式) void Delete(Expression> where); /// /// 根據(jù)ID獲取一個對象 /// /// 主鍵ID ///對象 T GetById(long Id); ////// 根據(jù)ID獲取一個對象 /// /// 主鍵ID ///對象 T GetById(string Id); ////// 根據(jù)條件獲取一個對象 /// /// 條件(lambda表達(dá)式) ///對象 T Get(Expression> where); /// /// 獲取所有數(shù)據(jù) /// ///所有數(shù)據(jù) IQueryableGetAll(); /// /// 根據(jù)條件獲取數(shù)據(jù) /// /// 條件(lambda表達(dá)式) ///數(shù)據(jù) IQueryableGetMany(Expression > where); /// /// 根據(jù)條件獲取記錄數(shù) /// /// 條件(lambda表達(dá)式) ///int GetCount(Expression > where); /// /// 關(guān)閉代理 /// void CloseProxy(); ////// 打開代理 /// void OpenProxy(); ////// 是否有指定條件的元素 /// /// 條件(lambda表達(dá)式) ///bool IsHasValue(Expression > where); } }
2、新建倉儲基礎(chǔ)操作類RepositoryBase.cs,注意要一一對應(yīng)實現(xiàn)IRepositroy接口的方法
using System;using System.Collections.Generic;using System.Text;using System.Linq;namespace CMS.Entity.Interfaces { public abstract class BaseRepositorywhere T: class { private BcmfDBContext db;//數(shù)據(jù)庫上下文 public BaseRepository(BcmfDBContext _db) { db = _db; } public virtual void Save() { db.SaveChanges(); } public virtual void Add(T entity) { db.Set ().Add(entity); } public virtual void CloseProxy() { db.Database.CommitTransaction(); } public virtual void Delete(T entity) { db.Set ().Remove(entity); } public virtual void Delete(System.Linq.Expressions.Expression > where) { var dataList = db.Set ().Where(where).AsEnumerable(); db.Set ().RemoveRange(dataList); } public virtual T Get(System.Linq.Expressions.Expression > where) { return db.Set ().FirstOrDefault(where); } public virtual System.Linq.IQueryable GetAll() { return db.Set (); } public virtual T GetById(long Id) { return db.Set ().Find(Id); } public virtual T GetById(string Id) { return db.Set ().Find(Id); } public virtual int GetCount(System.Linq.Expressions.Expression > where) { return db.Set ().Count(where); } public virtual System.Linq.IQueryable GetMany(System.Linq.Expressions.Expression > where) { return db.Set ().Where(where); } public virtual bool IsHasValue(System.Linq.Expressions.Expression > where) { return db.Set ().Any(where); } public virtual void OpenProxy() { db.Database.BeginTransaction(); } public virtual void Update(T entity) { db.Set ().Attach(entity); db.Entry (entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } } }
3、新建倉儲類TUserRepository與TOperateLogRepository,TUserRepository用于操作用戶表,TOperateLogRepository用于操作用戶記錄表,對應(yīng)的User類與OperateLog類根據(jù)項目需求自行創(chuàng)建
using System;using System.Collections.Generic;using System.Text;using CMS.Entity.Repository;using CMS.Entity.Entity;using Microsoft.EntityFrameworkCore;using System.Linq;using CMS.Entity.Interfaces;namespace CMS.Entity.Repository { public class TUserRepository :BaseRepository,IUserRepository { public TUserRepository(BcmfDBContext db) : base(db) { } } public interface IUserRepository : IRepository { } }
using System;using System.Collections.Generic;using System.Text;using CMS.Entity.Repository;using CMS.Entity.Entity;using Microsoft.EntityFrameworkCore;using System.Linq;using CMS.Entity.Interfaces;namespace CMS.Entity.Repository { public class TOperateLogRepository : BaseRepository, IOperateLogRepository { public TOperateLogRepository(BcmfDBContext db) : base(db) { } } public interface IOperateLogRepository : IRepository { } }
4、分別在UserController與OperateLogController控制器中的構(gòu)造函數(shù)注入倉儲類
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;using CMS.Entity;using CMS.Entity.Entity;using Newtonsoft.Json;using CMS.WebApi.Core;using Microsoft.EntityFrameworkCore.Query;using CMS.Entity.Repository;namespace CMS.WebApi.Controllers { ////// 用戶中心 /// //[Produces("application/json")] [Route("api/[controller]")] [ApiController] public class UsersController : ControllerBase { private readonly IUserRepository userRepository; public UsersController(IUserRepository _userRepository) { userRepository = _userRepository; } ////// 獲取用戶列表 /// ///[HttpGet] public string Get() { var userList= userRepository.GetAll().ToList(); return JsonConvert.SerializeObject(userList); } } }
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;using CMS.Entity.Repository;using Newtonsoft.Json;namespace CMS.WebApi.Controllers { [Route("api/[controller]")] [ApiController] public class OperateLogController : ControllerBase { private readonly IOperateLogRepository operateLogRepository; public OperateLogController(IOperateLogRepository _operateLogRepository) { operateLogRepository = _operateLogRepository; } [HttpGet] public string Get() { var result = operateLogRepository.GetMany(m=>m.ActionLogId<100); return JsonConvert.SerializeObject(result); } } }
5、完成上述操作后,運(yùn)行獲取數(shù)據(jù)時會系統(tǒng)報錯,那是由于還沒將倉儲類注入到服務(wù)中,接下來就實現(xiàn)用AutoFac注入倉儲類到項目服務(wù)中
添加引用Autofac,Auto.Configuration,Autofac.Extensions.DependencyInjection到項目中
這里貼出Nuget程序控制臺命令:
Install-Package Autofac -Version 4.9.2
Install-Package Autofac.Configuration -Version 4.1.0
Install-Package Autofac.Extensions.DependencyInjection -Version 4.4.0
6、打開項目Startup.cs,找到ConfigureServices方法,將void改為IServiceProvider返回值,如下
//先引用命名空間 using Autofac; using Autofac.Extensions.DependencyInjection; builder = builder.RegisterAssemblyTypes(=> t.Name.EndsWith( container =
7、重新生成發(fā)布項目,完成