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. android模拟器(genymotion)+appium+python 框架执行基本原理(目前公司自己写的)

    android模拟器(genymotion)+appium+python 框架执行的基本过程: 1.Push.initDate(openid)方法     //业务数据初始化 1.1   v5db.p ...

  2. iphone 异常捕获处理

    iphone 异常捕获处理 1 void UncaughtExceptionHandler(NSException *exception) { 2 NSArray *arr = [exception ...

  3. Hadoop基础教程之分布式环境搭建

    前面,我们已经在单机上把Hadoop运行起来了,但我们知道Hadoop支持分布式的,而它的优点就是在分布上突出的,所以我们得搭个环境模拟一下. 在这里,我们采用这样的策略来模拟环境,我们使用3台ubu ...

  4. CodeBlocks ubuntu常见问题及小技巧

    1.安装完后没有快捷键的设置: http://blog.csdn.net/sjwbcb/article/details/4797785 感谢这哥,原来是包没有装全. codeblocks用ubuntu ...

  5. Java Map排序

    Map排序的方式有很多种,这里记录下自己总结的两种比较常用的方式:按键排序(sort by key), 按值排序(sort by value). 1.按键排序 jdk内置的java.util包下的Tr ...

  6. Redis的Set操作

    集合的性质: 唯一性,无序性,确定性 注: 在string和link的命令中,可以通过range 来访问string中的某几个字符或某几个元素 但,因为集合的无序性,无法通过下标或范围来访问部分元素. ...

  7. intelliJ IDEA中项目以jar包的形式导出

    在上一篇中把intelliJ IDEA安装并配置完事后,我们就可以写scala程序了.编写完scala程序后我们要把程序导出,上传到服务器上,在spark集群下运行,下面就讲一下包的导出过程以及包在服 ...

  8. lua简化cocos2dx的Action动画序列

    情景 今天写代码时,又要写一个很常见的动画,就是变大变小模拟那个弹性的赶脚,很常用但写起来挺麻烦,封装一下用起来就简单多了. 当然我也知道有缓动动画(EaseAction)可以实现反弹效果,但这不是重 ...

  9. PHP中对数据库操作的封装

    在动态网面设计中很多都要涉及到对数据库的操作,但是有时跟据需要而改用其它后台数据库,就需要大量修改程序.这是一件枯燥.费时而且容易出错的功作.其实我们可以用PHP中的类来实现对数据库操作的封装,从而使 ...

  10. Python应用与实践【转】

    转自:http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html 目录 1.      Python是什么? 1.1.      Pyt ...