自己改写的asp.net MVC EF Respoistory 仓储模式

时间:2022-06-13 08:45:28

之前改写网上收集的Respoistory 模式感觉很多地方都是不可取的,这里经过这段时间的充电重新改写一版,当然注释已加,还有不懂的,可以留言我。

首先还是拿出IRespoistory的接口层代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace Respository
{
/// <summary>
/// 这里T是泛型,(T:class T是泛型参数。where T : class 是对T的限制,这里的意思是T必须是引用类型,包括任何类、接口、委托或数组类型)
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IRespository<T>where T:class
{
/// <summary>
/// IRespository保存接口
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
bool SaveEntity(T entity); /// <summary>
/// IRespository修改接口
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
bool UpdateEntity(T entity); /// <summary>
/// IRespository删除
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
bool DeleteEntity(T entity); /// <summary>
/// 根据id查询
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
T GetEntityById(object Id); /// <summary>
/// 带条件查询
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
T Get(Expression<Func<T, bool>> where); /// <summary>
/// 查询所有
/// </summary>
/// <returns></returns>
IEnumerable<T> GetALLEntity(); /// <summary>
/// 这里也可以用IEnumerable类型,带条件查询所有
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
IQueryable<T> GetAllEntityWhere(Expression<Func<T, bool>> where); /// <summary>
/// 分页
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="PageSize"></param>
/// <returns></returns>
IList<T> GetPageEntities(int pageIndex, int PageSize); /// <summary>
/// 分页带查询条件
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="PageSize"></param>
/// <param name="where"></param>
/// <returns></returns>
IList<T> GetPageEntities(int pageIndex, int PageSize, Expression<Func<T, bool>> where); }
} 然后是Respoistory 对应接口的实现 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
using System.Linq.Expressions;
namespace Respository
{
public class Respository<T>:IRespository<T>where T:class
{
//这里首先把DbContext (数据库的上下文给带出来)
public DbContext context;
public Respository(DbContext context)
{
this.context = context;
} /// <summary>
/// Res保存的方法
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool SaveEntity(T entity)
{
bool RetStatus = false;
context.Set<T>().Add(entity);
if (Save() > )
{
RetStatus = true;
}
return RetStatus; } /// <summary>
/// Res修改的方法
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool UpdateEntity(T entity)
{
// throw new NotImplementedException();
bool RetStatus = false;
if (context.Entry(entity).State == System.Data.Entity.EntityState.Modified)
{
if (Save()> )
{
RetStatus = true;
}
}
return RetStatus; } /// <summary>
/// Res删除的方法
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool DeleteEntity(T entity)
{
//throw new NotImplementedException();
bool RetStatus=false;
if (context.Entry(entity).State == System.Data.Entity.EntityState.Deleted)
{
context.Set<T>().Remove(entity);
if (Save() > )
{
RetStatus = true;
}
}
return RetStatus; } /// <summary>
/// 对Set<T>根据id 的查询的操作
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public T GetEntityById(object Id)
{
return context.Set<T>().Find(Id);
} /// <summary>
/// 这里对Set<T>是带条件的操作
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public T Get(Expression<Func<T, bool>> where)
{
return context.Set<T>().Where(where).FirstOrDefault<T>();
} /// <summary>
/// 查询所有的
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetALLEntity()
{
// throw new NotImplementedException(); IEnumerable<T> query = context.Set<T>(); return query;
} /// <summary>
/// 查询所有带条件
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public IQueryable<T> GetAllEntityWhere(Expression<Func<T, bool>> where)
{
IQueryable<T> query = context.Set<T>().Where(where);
return query; } /// <summary>
/// 分页方法
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="PageSize"></param>
/// <returns></returns>
public IList<T> GetPageEntities(int pageIndex, int PageSize)
{
IList<T> List = context.Set<T>().Skip(pageIndex * PageSize).Take(PageSize).ToList();
return List ; } /// <summary>
/// 分页带查询条件
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="PageSize"></param>
/// <param name="where"></param>
/// <returns></returns>
public IList<T> GetPageEntities(int pageIndex, int PageSize, Expression<Func<T, bool>> where)
{
// throw new NotImplementedException();
IList<T> List = context.Set<T>().Where(where).Skip(pageIndex * PageSize).Take(PageSize).ToList();
return List; } /// <summary>
/// Save 保存确认方法
/// </summary>
public int Save()
{
return context.SaveChanges(); }
}
} 这里我也把实际的调用写出来吧,希望对看到的同学有所帮助 我这里是用edmx 生成的一个简单的用户表的model 实列和Dbcontext //------------------------------------------------------------------------------
// <auto-generated>
// 此代码是根据模板生成的。
//
// 手动更改此文件可能会导致应用程序中发生异常行为。
// 如果重新生成代码,则将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------ namespace SSOModel.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure; public partial class yiliuyangEntities : DbContext
{
public yiliuyangEntities()
: base("name=Entities")
{
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
} public DbSet<yi_userinfo> yi_userinfo { get; set; }
}
} //------------------------------------------------------------------------------
// <auto-generated>
// 此代码是根据模板生成的。
//
// 手动更改此文件可能会导致应用程序中发生异常行为。
// 如果重新生成代码,则将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------ namespace SSOModel.Models
{
using System;
using System.Collections.Generic; public partial class userinfo
{
public int userid { get; set; }
public string username { get; set; }
public string userpwd { get; set; }
public string yusercode { get; set; }
public Nullable<int> usersex { get; set; }
public string userqq { get; set; }
public string usertel { get; set; }
}
} 然后是EF_Userinfo 方法调用层 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using Respository;
using SSOModel.Models;
namespace SSOMain.Models
{
public class EF_Userinfo
{
Respository<yi_userinfo> EF_Res = new Respository<userinfo>(new Entities()); //这里我只写了一个简单的读取方法,是get userinfo 的所有信息
public IList<yi_userinfo> GetUserInfo()
{
return EF_Res.GetALLEntity().ToList(); } }
} 最后是HomeController using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SSOModel;
using Respository;
using SSOMain.Models;
namespace SSOMain.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ EF_Userinfo Ef = new EF_Userinfo(); public ActionResult Index()
{
var list = Ef.GetUserInfo(); return View(list.ToList());
} }
} 页面我也贴出来的 index.cshtml @model IEnumerable<SSOModel.Models.userinfo>
@{
ViewBag.Title = "Index";
} <h2>Index</h2>
<table> <tr>
<th>
名字
</th>
<th>
密码
</th>
</tr> @foreach (var item in Model)
{
<tr>
<td>
@item.username </td>
<td>
@item.userpwd
</td> </tr> } </table>
      Respoistory 的目的是使数据操作集中在它的数据持久层里面,代码重用。个人技术有限只能理解到这里,希望对大家有所帮助。