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. 540A: Combination Lock

    题目链接:http://codeforces.com/problemset/problem/540/A 题意: 输入的两个长度一样的数,求对应位置的某位数到下一个数需要最小的步长,每次只能先前或先后走 ...

  2. Junit单元测试学习笔记三

    一.     高级 Fixture 上一篇文章中我们介绍了两个 Fixture 标注,分别是 @Before 和 @After ,我们来看看他们是否适合完成如下功能:有一个类是负责对大文件(超过 50 ...

  3. gcc 优化选项 -O1 -O2 -O3 -Os 优先级

    http://hi.baidu.com/xiaole10368/item/7cea9b1369cc240db88a1a5c 少优化->多优化: O0 -->> O1 -->&g ...

  4. PageLayoutControl的基本操作

    整理了下对PageLayoutControl的基本功能操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...

  5. Jenkins Master/Slave架构

    原文:http://www.cnblogs.com/itech/archive/2011/11/11/2245849.html 一 Jenkins Master/Slave架构 Master/Slav ...

  6. 感知机(python实现)

    感知机(perceptron)是二分类的线性分类模型,输入为实例的特征向量,输出为实例的类别(取+1和-1).感知机对应于输入空间中将实例划分为两类的分离超平面.感知机旨在求出该超平面,为求得超平面导 ...

  7. python实现全角半角的相互转换

    缘起 在自然语言处理过程中,全角.半角的的不一致会导致信息抽取不一致,因此需要统一. 转换说明 全角半角转换说明 有规律(不含空格): 全角字符unicode编码从65281~65374 (十六进制 ...

  8. Control character in cookie value, consider BASE64 encoding your value

    这是因为你给Cookie设置了中文的value,比如Cookie c = new Cookie("user", "张三");

  9. 神经网络:卷积神经网络CNN

    一.前言 这篇卷积神经网络是前面介绍的多层神经网络的进一步深入,它将深度学习的思想引入到了神经网络当中,通过卷积运算来由浅入深的提取图像的不同层次的特征,而利用神经网络的训练过程让整个网络自动调节卷积 ...

  10. wget https://github.com/xxx/yyy/archive/${commit_hash}.zip

    wget https://github.com/xxx/yyy/archive/${commit_hash}.zip