csharp EF的通用存储库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp EF的通用存储库相关的知识,希望对你有一定的参考价值。

using OrangeMSE.Business;
using OrangeMSE.Business.Data;
using OrangeMSE.Business.Helpers;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;

public abstract class Repository<T> : IDisposable, IRepository<T> where T : class
{
    internal DbContext context { get; private set; }
    protected DbSet<T> objectSet;


    internal Repository(bool ProxyCreationEnabled = true)
        : this(new Entities())
    {
        ((IObjectContextAdapter)context).ObjectContext.ContextOptions.ProxyCreationEnabled = ProxyCreationEnabled;
    }
    internal Repository(DbContext _context)
    {
        context = _context;
        objectSet = context.Set<T>();
    }

    public virtual IQueryable<T> GetAll()
    {
        return objectSet;
    }

    public virtual IQueryable<T> GetAll(Expression<Func<T, bool>> predicate)
    {
        return predicate == null ? objectSet : objectSet.Where<T>(predicate);
    }

    public virtual T Get(Expression<Func<T, bool>> predicate)
    {
        return objectSet.Where<T>(predicate).FirstOrDefault();
    }

    public virtual K Add<K>(T entity, Expression<Func<T, object>> property)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        objectSet.Add(entity);
        context.SaveChanges();

        var propertyName = GeneralExtensions.GetPropertyName<T, object>(property);
        return (K)entity.GetType().GetProperty(propertyName).GetValue(entity, null);
    }

    public virtual T Add(T entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        objectSet.Add(entity);
        context.SaveChanges();

        return entity;
    }

    public virtual void AddRange(List<T> entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        objectSet.AddRange(entity);
        context.SaveChanges();
    }

    public virtual void Update(T entity)
    {
        objectSet.Attach(entity);
        context.Entry<T>(entity).State = EntityState.Modified;
        context.SaveChanges();
    }

    public virtual void Update(T entity, params Expression<Func<T, object>>[] modifiedProperties)
    {
        objectSet.Attach(entity);
        context.Configuration.ValidateOnSaveEnabled = false;

        foreach (var property in modifiedProperties)
        {
            string propertyName = GeneralExtensions.GetPropertyName<T, object>(property);
            context.Entry<T>(entity).Property(propertyName).IsModified = true;
        }

        context.SaveChanges();
        context.Configuration.ValidateOnSaveEnabled = true;
    }

    public virtual void Delete(Expression<Func<T, bool>> predicate)
    {
        IQueryable<T> records = from x in objectSet.Where<T>(predicate) select x;
        foreach (T record in records)
        {
            objectSet.Remove(record);
        }

        context.SaveChanges();
    }

    public void Dispose()
    {
        context.Dispose();
    }
}

以上是关于csharp EF的通用存储库的主要内容,如果未能解决你的问题,请参考以下文章

csharp 存储库模式的通用实现

MVC3 EF 工作单元 + 通用存储库 + Ninject

具有通用存储库和依赖注入和 SoC 的 EF6 Code First

csharp 模拟EF 6的代码库

如何使用通用存储库实现 ADO.NET?

存储库模式 - 如何正确处理 JOIN 和复杂查询?