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的更多相关文章

  1. 记一次.NET代码重构

    好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使劲赶的时候,神马设计都是浮云 ...

  2. MVC5+EF6 入门完整教程十一:细说MVC中仓储模式的应用

    摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...

  3. NET代码重构

    记一次.NET代码重构   好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使 ...

  4. MVC+EF 理解和实现仓储模式和工作单元模式

    MVC+EF 理解和实现仓储模式和工作单元模式 原文:Understanding Repository and Unit of Work Pattern and Implementing Generi ...

  5. 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 ...

  6. MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用

    摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...

  7. 用c#开发微信 (6) 微渠道 - 推广渠道管理系统 1 基础架构搭建

    我们可以使用微信的“生成带参数二维码接口”和 “用户管理接口”,来实现生成能标识不同推广渠道的二维码,记录分配给不同推广渠道二维码被扫描的信息.这样就可以统计和分析不同推广渠道的推广效果. 本系统使用 ...

  8. 用c#开发微信 (11) 微统计 - 阅读分享统计系统 1 基础架构搭建

    微信平台自带的统计功能太简单,有时我们需要统计有哪些微信个人用户阅读.分享了微信公众号的手机网页,以及微信个人用户访问手机网页的来源:朋友圈分享访问.好友分享消息访问等.本系统实现了手机网页阅读.分享 ...

  9. Fluent NHibernate example

    http://www.codeproject.com/Articles/26466/Dependency-Injection-using-Spring-NET http://stackoverflow ...

随机推荐

  1. Java IO(二)

    字节流 字符流: FileReader FileWriter BufferedReader BufferedWriter 字节流: FileInputStream FileOutputStream B ...

  2. nil和NULL

  3. lintcode:删除链表中指定元素

    题目 删除链表中等于给定值val的所有节点. 样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1-> ...

  4. Fatal error: cannot allocate memory for the buffer pool

    mysql有时候会被系统kill掉,原因是内存不够了,一般都是Ubuntu出现的,因为Ubuntu吃内存,你们又给的不多.. 咋解决呢? 重启服务器是可以的,起码暂时可以了, 可以考虑加内存,或者增加 ...

  5. 图解TCP/IP读书笔记(四)

    第四章.IP协议 IP(Internet Protocol,网际协议),作为整个TCP/IP中至关重要的协议,主要负责将数据包发送给最终的目标计算机.因此,IP能够让世界上任何两台计算机之间进行通信. ...

  6. android 点九PNG技术 适应不同分辨率 完美显示效果

    .9.png是一种非失真性压缩位图图形文件格式.PNG格式是非失真性压缩的,允许使用类似于GIF格式的调色板技术,支持真彩色图像,并具备阿尔法通道(半透明)等特性.现在有很多人使用PNG格式于互联网及 ...

  7. 物联网操作系统Hello China移植mile stone之一:移植基础版本V1.76发布

    Hello China V1.76版发布,这是向ARM系列CPU移植的基础版本.相对V1.75版,该版本主要做了如下的一些调整: 1.  通过宏定义的方式对内核实现了模块化,开发者可以通过开启或关闭预 ...

  8. JVM垃圾回收机制总结(7) :调优方法

    JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪.详细说明参考这里 ...

  9. Android百度地图开发04之POI检索

    POI检索 POI~~~ Point of Interest,翻译过来就是“兴趣点”.我们在使用地图的时候,搜索周边的ktv,饭店,或者宾馆的时候,输入关键字,然后地图展示给我们很多个点, 这些点就是 ...

  10. 【Cocosd2d实例教程二】地图编辑器Tiled的安装使用

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 我们知道cocos2d是一个基于2d效果的游戏引擎,那么如果制作一个2d手机游戏我们需要创建相应的游戏画面,而c ...