GenericRepository
public interface IGenericRepository<TEntity> where TEntity : class
{
IQueryable<TEntity> GetAll();
IQueryable<TEntity> FindBy(Expression<Func<TEntity , bool>> predicate);
void Add(TEntity entity);
void Delete(TEntity entity);
void Edit(TEntity entity);
void Save();
}
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
private readonly DbContext _context;
private readonly IDbSet<TEntity> _entities; public GenericRepository(DbContext context)
{
this._context = context;
_entities = _context.Set<TEntity>();
} public virtual IQueryable<TEntity> GetAll()
{
IQueryable<TEntity> query = _entities;
return query;
} public IQueryable<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate)
{
IQueryable<TEntity> query = _entities.Where(predicate);
return query;
} public virtual void Add(TEntity entity)
{
_entities.Add(entity);
} public virtual void Delete(TEntity entity)
{
_entities.Remove(entity);
} public virtual void Edit(TEntity entity)
{
// entity.State = EntityState.Modified;
} public virtual void Save()
{
//_entities.SaveChanges();
}
}
public class MyContext : DbContext
{
public MyContext(){} public MyContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); //取消级联删除设置 modelBuilder.Entity<Area>().ToTable("Area");
base.OnModelCreating(modelBuilder);
} public DbSet<Area> Area { get; set; }
}
public class AreaRepository : IAreaRepository
{
private GenericRepository<Area> internalGenericRepository; public AreaRepository(GenericRepository<Area> internalGenericRepository)
{
this.internalGenericRepository = internalGenericRepository;
} public List<Area> GetAllProvince()
{
var query = from o in this.internalGenericRepository.GetAll()
//where o.AreaTypeID == AreaTypeEnum.Province
select o; return query.ToList();
}
}
public class AreaService : IAreaService
{
private readonly IAreaRepository _AreaRepository; public AreaService(IAreaRepository areaRepository)
{
this._AreaRepository = areaRepository;
} public List<DictionaryEntry> ProvinceList {
get {
var query = from o in this._AreaRepository.GetAllProvince()
select new DictionaryEntry(o.Name, o.Id); return query.ToList();
}
}
}
IUnityContainer container = new UnityContainer();
container.RegisterInstance<DbContext>(new MyContext("ConnectionString")); //注册实例
container.RegisterType<IAreaService, AreaService>();
container.RegisterType<IAreaRepository, AreaRepository>();
container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>)); var IAreaService = container.Resolve<IAreaService>(); //实例化接口
Console.WriteLine(IAreaService.ProvinceList.Count());
GenericRepository的更多相关文章
- 记一次.NET代码重构
好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使劲赶的时候,神马设计都是浮云 ...
- MVC5+EF6 入门完整教程十一:细说MVC中仓储模式的应用
摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...
- NET代码重构
记一次.NET代码重构 好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使 ...
- MVC+EF 理解和实现仓储模式和工作单元模式
MVC+EF 理解和实现仓储模式和工作单元模式 原文:Understanding Repository and Unit of Work Pattern and Implementing Generi ...
- EF How to use context.Set and context.Entry, which ships with EF4.1 ?
How to use context.Set and context.Entry, which ships with EF4.1 ? Hello, I am trying to implement a ...
- MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用
摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...
- 用c#开发微信 (6) 微渠道 - 推广渠道管理系统 1 基础架构搭建
我们可以使用微信的“生成带参数二维码接口”和 “用户管理接口”,来实现生成能标识不同推广渠道的二维码,记录分配给不同推广渠道二维码被扫描的信息.这样就可以统计和分析不同推广渠道的推广效果. 本系统使用 ...
- 用c#开发微信 (11) 微统计 - 阅读分享统计系统 1 基础架构搭建
微信平台自带的统计功能太简单,有时我们需要统计有哪些微信个人用户阅读.分享了微信公众号的手机网页,以及微信个人用户访问手机网页的来源:朋友圈分享访问.好友分享消息访问等.本系统实现了手机网页阅读.分享 ...
- Fluent NHibernate example
http://www.codeproject.com/Articles/26466/Dependency-Injection-using-Spring-NET http://stackoverflow ...
随机推荐
- HDU 4169 树形DP
Wealthy Family Problem Description While studying the history of royal families, you want to know ho ...
- node入门开发遇到的问题
最近在看node入门这本书,https://cnodejs.org/getstart 里面是跟随作者完成一个小的demo,书中不免会有遗漏的,下面是我在实现里面最后一个例子时遇到的问题,希望能够帮助其 ...
- MongoDB (七) MongoDB 数据类型
MongoDB支持许多数据类型的列表下面给出: String : 这是最常用的数据类型来存储数据.在MongoDB中的字符串必须是有效的UTF-8. Integer : 这种类型是用来存储一个数值.整 ...
- phpeclipse常用快捷键
phpeclipse常用快捷键
- React编写文本评论框
一.需求分析 二.代码 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset= ...
- 解决Android开发中,ActiveAndroid和Gson同时使用,对象序列化失败的问题
ActiveAndroid是安卓开发常用的ORM框架. Gson则是Google提供的轻量级序列化框架,非常适合Android开发使用. 但这两者同时使用,会产生序列化失败的问题.你通常会收到如下信息 ...
- vmware 虚拟机 桥接 设置静态 IP
最简单的方式: 图形界面下,进入设置IP的地方,设置一个静态IP. 然后再转入命令行继续工作.. 不行就 重启
- Java WEB安全问题及解决方案
1.弱口令漏洞 解决方案:最好使用至少6位的数字.字母及特殊字符组合作为密码.数据库不要存储明文密码,应存储MD5加密后的密文,由于目前普通的MD5加密已经可以被破解,最好可以多重MD5加密. ...
- hibernate的三种状态(儿)
第五讲:hibernate的三种状态 瞬时:bean对象与session,与数据库无关.在session对象的save方法保存之前. 持久状态(托管):bean对象与session有关,数据库中有对应 ...
- PHP的模板引擎这点事儿
什么是模板引擎? 为什么要使用它? 为什么要assign一个变量给模板? https://dbforch.wordpress.com/2010/06/26/the-logic-behind-templ ...