Entity framework 正确使用实体框架/Linq到SQL的接口
我将在这里展示我的经验不足,但是嘿,就像我想学习的任何开发人员一样 给定以下接口:Entity framework 正确使用实体框架/Linq到SQL的接口,entity-framework,oop,linq-to-sql,interface,Entity Framework,Oop,Linq To Sql,Interface,我将在这里展示我的经验不足,但是嘿,就像我想学习的任何开发人员一样 给定以下接口: public interface IRepository { entityDB Database { get; set; } IQueryable<T> All<T>() where T:class, new(); T Single<T>(Expression<Func<T, bool>> expression) where T
public interface IRepository
{
entityDB Database { get; set; }
IQueryable<T> All<T>() where T:class, new();
T Single<T>(Expression<Func<T, bool>> expression) where T : class, new();
IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new();
}
公共接口IRepository
{
entityDB数据库{get;set;}
IQueryable All(),其中T:class,new();
T Single(表达式),其中T:class,new();
IList Find(表达式),其中T:class,new();
}
我将如何创建一个可用的存储库类,以便将泛型类型T替换为强类型
假设我有一个照片公文包站点,它有一个照片实体和一个CameraSettings实体,那么如何定义包含在我的具体类中的类型(以及L2)?目前,当我实现该类时,它需要以下形式:
public class PhotoRepository : IRepository
{
public override IQueryable<T> All<T>()
{
// code goes here...
}
public override T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
{
// ...and here...
}
public override IList<T> Find<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
{
// ... and let's not forget here.
}
}
公共类光电沉积:IRepository
{
公共覆盖IQueryable All()
{
//代码在这里。。。
}
公共重写T单(System.Linq.Expressions.Expression)
{
//……在这里。。。
}
公共重写IList查找(System.Linq.Expressions.Expression)
{
//…别忘了这里。
}
}
我这样做是正确的,还是应该使用如下界面:
public interface IRepository<T> where T:class
{
entityDB Database { get; set; }
IQueryable<T> All();
T Single<T>;
IList<T> Find();
}
public class PhotoRepository : IRepository<Photo>
{
public IQueryable<Photo> All()
{
// code goes here...
}
public Photo Single(Expression<Func<Photo, bool>> expression)
{
// ...and here...
}
public IList<Photo> Find(Expression<Func<Photo, bool>> expression)
{
// ... and let's not forget here.
}
}
公共接口i假设,其中T:class
{
entityDB数据库{get;set;}
IQueryable All();
T单;
IList Find();
}
我只是想让我的单元测试正常工作,并确信我没有养成坏习惯(或者只是简单的坏习惯)。欣赏正确的方向。如果我对您的理解正确,您希望为您的每个实体创建单独的存储库(或仅为其中一些实体)。在这种情况下,合适的解决方案是使用您发布的界面的第二个版本。像这样:
public interface IRepository<T> where T : class, new()
{
entityDB Database { get; set; }
IQueryable<T> All();
T Single(Expression<Func<T, bool>> expression);
IList<T> Find(Expression<Func<T, bool>> expression);
}
此外,如果您的存储库将与实体框架实体一起运行,则可以更准确地定义IRepository接口:
public interface IRepository<T> where T : EntityObject, new()
public interface i位置,其中T:EntityObject,new()
如果我对您的理解正确,您希望为您的每个实体创建单独的存储库(或仅为其中一些实体)。在这种情况下,合适的解决方案是使用您发布的界面的第二个版本。像这样:
public interface IRepository<T> where T : class, new()
{
entityDB Database { get; set; }
IQueryable<T> All();
T Single(Expression<Func<T, bool>> expression);
IList<T> Find(Expression<Func<T, bool>> expression);
}
此外,如果您的存储库将与实体框架实体一起运行,则可以更准确地定义IRepository接口:
public interface IRepository<T> where T : EntityObject, new()
public interface i位置,其中T:EntityObject,new()
在我的项目中,我创建了自己的IRepository:
public interface IRepository<T>
{
//Retrieves list of items in table
IQueryable<T> List();
IQueryable<T> List(params string[] includes);
//Creates from detached item
void Create(T item);
void Delete(int id);
T Get(int id);
T Get(int id, params string[] includes);
void SaveChanges();
}
公共接口IRepository
{
//检索表中的项目列表
IQueryable列表();
可查询列表(参数字符串[]包括);
//从分离的项创建
无效创建(T项);
无效删除(int-id);
T Get(int-id);
T Get(int-id,参数字符串[]包含);
void SaveChanges();
}
以下是通用实现:
public class Repository<T> : IRepository<T> where T : EntityObject
{
private ObjectContext _ctx;
public Repository(ObjectContext ctx)
{
_ctx = ctx;
}
private static string EntitySetName
{
get
{
return String.Format(@"{0}Set", typeof(T).Name);
}
}
private ObjectQuery<T> ObjectQueryList()
{
var list = _ctx.CreateQuery<T>(EntitySetName);
return list;
}
#region IRepository<T> Members
public IQueryable<T> List()
{
return ObjectQueryList().OrderBy("it.ID").AsQueryable();
}
public IQueryable<T> List(params string[] includes)
{
var list = ObjectQueryList();
foreach(string include in includes)
{
list = list.Include(include);
}
return list;
}
public void Create(T item)
{
_ctx.AddObject(EntitySetName, item);
}
public void Delete(int id)
{
var item = Get(id);
_ctx.DeleteObject(item);
}
public T Get(int id)
{
var list = ObjectQueryList();
return list.Where("ID = @0", id).First();
}
public T Get(int id, params string[] includes)
{
var list = List(includes);
return list.Where("ID = @0", id).First();
}
public void SaveChanges()
{
_ctx.SaveChanges();
}
#endregion
}
公共类存储库:IRepository其中T:EntityObject
{
私有对象上下文_ctx;
公共存储库(ObjectContext ctx)
{
_ctx=ctx;
}
私有静态字符串EntitySetName
{
得到
{
返回String.Format(@“{0}集”,typeof(T).Name);
}
}
私有ObjectQuery ObjectQueryList()
{
var list=_ctx.CreateQuery(EntitySetName);
退货清单;
}
#地区成员
公共可查询列表()
{
返回ObjectQueryList().OrderBy(“it.ID”).AsQueryable();
}
公共可查询列表(参数字符串[]包括)
{
var list=ObjectQueryList();
foreach(包含在包含中的字符串)
{
列表=列表。包括(Include);
}
退货清单;
}
公共作废创建(T项)
{
_ctx.AddObject(EntitySetName,item);
}
公共无效删除(int-id)
{
var item=Get(id);
_ctx.DeleteObject(项目);
}
公共T获取(int id)
{
var list=ObjectQueryList();
return list.Where(“ID=@0”,ID).First();
}
public T Get(int-id,参数字符串[]包含)
{
var列表=列表(包括);
return list.Where(“ID=@0”,ID).First();
}
公共void SaveChanges()
{
_ctx.SaveChanges();
}
#端区
}
如果您想要简单的存储库,只需使用存储库
。如果您想增强它,您可以定义ClassNameRepository:Repository
并编写另一个方法。您不必在每个存储库中定义List()
方法,因为它是在base中完成的。在编写存储库之前,我做了以下假设:
- 数据库中的每个主键字段都称为ID
- 每个EntitySetName都是由类名和
构成的。对于Set
类,它将是User
UserSet
在我的项目中,我创建了自己的IRepository:
public interface IRepository<T>
{
//Retrieves list of items in table
IQueryable<T> List();
IQueryable<T> List(params string[] includes);
//Creates from detached item
void Create(T item);
void Delete(int id);
T Get(int id);
T Get(int id, params string[] includes);
void SaveChanges();
}
公共接口IRepository
{
//检索表中的项目列表
IQueryable列表();
可查询列表(参数字符串[]包括);
//从分离的项创建
无效创建(T项);
无效删除(int-id);
T Get(int-id);
T Get(int-id,参数字符串[]包含);
void SaveChanges();
}
以下是通用实现:
public class Repository<T> : IRepository<T> where T : EntityObject
{
private ObjectContext _ctx;
public Repository(ObjectContext ctx)
{
_ctx = ctx;
}
private static string EntitySetName
{
get
{
return String.Format(@"{0}Set", typeof(T).Name);
}
}
private ObjectQuery<T> ObjectQueryList()
{
var list = _ctx.CreateQuery<T>(EntitySetName);
return list;
}
#region IRepository<T> Members
public IQueryable<T> List()
{
return ObjectQueryList().OrderBy("it.ID").AsQueryable();
}
public IQueryable<T> List(params string[] includes)
{
var list = ObjectQueryList();
foreach(string include in includes)
{
list = list.Include(include);
}
return list;
}
public void Create(T item)
{
_ctx.AddObject(EntitySetName, item);
}
public void Delete(int id)
{
var item = Get(id);
_ctx.DeleteObject(item);
}
public T Get(int id)
{
var list = ObjectQueryList();
return list.Where("ID = @0", id).First();
}
public T Get(int id, params string[] includes)
{
var list = List(includes);
return list.Where("ID = @0", id).First();
}
public void SaveChanges()
{
_ctx.SaveChanges();
}
#endregion
}
公共类存储库:IRepository其中T:EntityObject
{
私有对象上下文_ctx;
公共存储库(ObjectContext ctx)
{
_ctx=ctx;
}
私有静态字符串EntitySetName
{
得到
{
返回String.Format(@“{0}集”,typeof(T).Name);
}
}
私有ObjectQuery ObjectQueryList()
{
var list=_ctx.CreateQuery(EntitySetName);
退货清单;
}
#地区成员
公共可查询列表()
{
返回ObjectQueryList().OrderBy(“it.ID”).AsQueryable();
}
公共可查询列表(参数