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. NTP服务及时间同步(CentOS6.x)

    博客分类: linux   今有一小型项目,完全自主弄,原来以为很简单的NTP服务,我给折腾了2个多小时才整撑头(以前都是运维搞,没太注意,所以这技术的东西,在简单都需要亲尝啊),这里记录为以后别再浪 ...

  2. 【poj1006-biorhythms】中国剩余定理

    http://poj.org/problem?id=1006 题意:中国剩余定理的裸题. 题目可转化为求最小的x满足以下条件: x%23=a;x%28=b;x%33=c; 关于中国剩余定理可看我昨天的 ...

  3. Useful for Android the development engineer from Github

    Original:http://sysmagazine.com/posts/216591/ Many plowing on open space Github, I found assemblage ...

  4. sql 数据库换行

    制表符 CHAR(9)  换行符 CHAR(10)  回车 CHAR(13) 

  5. SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-001-Spring对原始JDBC的封装

    1.spring扩展的jdbc异常 2.Template的运行机制 Spring separates the fixed and variable parts of the data-access p ...

  6. photoshop菜单显示不全的解决方法

    photoshop菜单显示不全? 解决方法,选择菜单 编辑->菜单,下拉菜单选择photoshop默认值

  7. vb.net 写入文件同步锁

    <SoapHeader("oHeader")> _ <WebMethod()> _ <ScriptMethod(ResponseFormat:=Res ...

  8. Linux驱动修炼之道-RTC子系统框架与源码分析【转】

    转自:http://helloyesyes.iteye.com/blog/1072433 努力成为linux kernel hacker的人李万鹏原创作品,为梦而战.转载请标明出处 http://bl ...

  9. The GPG keys listed not correct

    The GPG keys listed for the "Extra Packages for Enterprise Linux 5 - x86_64" repository ar ...

  10. XTU -1231 人生成就 (dp + 记录最优解的个数)

    http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1231 直接递推. 在保存最大值的时候同时保存有多少条到达最大值的路径,注意 ...