个人项目框架搭建 -- 仓储模式使用

---恢复内容开始---

1、创建仓储模式的相关接口

2、三个文件的代码(命名空间)

IRepository.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace EnterpriseFrame.Core.Data
{
    /// <summary>
    /// 这里T是泛型,(T:class  T是泛型参数。where T : class  是对T的限制,这里的意思是T必须是引用类型,包括任何类、接口、委托或数组类型)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IRepository<T> where T : class
    {

        /// <summary>
        /// Gets a table
        /// </summary>
        IQueryable<T> Table { get; }
        /// <summary>
        /// IRespository插入接口
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool InsertEntity(T entity);


        /// <summary>
        /// IRespository修改接口
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool UpdateEntity(T entity);

        /// <summary>
        /// IRespository删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool DeleteEntity(T entity);

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        T GetEntityById(object Id);

        /// <summary>
        /// 带条件查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        T Get(Expression<Func<T, bool>> where);


        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        IEnumerable<T> GetALLEntity();

        /// <summary>
        /// 这里也可以用IEnumerable类型,带条件查询所有
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        IQueryable<T> GetAllEntityWhere(Expression<Func<T, bool>> where);


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        IList<T> GetPageEntities(int pageIndex, int PageSize);

        /// <summary>
        /// 分页带查询条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        IList<T> GetPageEntities(int pageIndex, int PageSize, Expression<Func<T, bool>> where);



    }
}

EfRepository.cs代码:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseFrame.Core.Data
{
    public class EfRepository<T> : IRepository<T> where T : class
    {

        private readonly IDbContext _context;
        private IDbSet<T> _entities;

        public EfRepository(IDbContext context)
        {
            this._context = context;
        }

        protected virtual IDbSet<T> Entities
        {
            get
            {
                if (_entities == null)
                    _entities = _context.Set<T>();
                return _entities;
            }
        }
        /// <summary>
        /// Gets a table
        /// </summary>
        public virtual IQueryable<T> Table
        {
            get
            {
                return this.Entities;
            }
        }

        /// <summary>
        /// 插入实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertEntity(T entity)
        {
            bool RetStatus = false;
            this.Entities.Add(entity);
            if (Save() > 0)
            {
                RetStatus = true;
            }
            return RetStatus;

        }


        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(T entity)
        {
            // throw new NotImplementedException();
            bool RetStatus = false;
            if (entity != null && Save() > 0)
            {
                RetStatus = true;
            }
            return RetStatus;

        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteEntity(T entity)
        {
            //throw new NotImplementedException();
            bool RetStatus = false;
            if (entity != null)
            {
                this.Entities.Remove(entity);
                if (Save() > 0)
                {
                    RetStatus = true;
                }
            }
            return RetStatus;

        }

        /// <summary>
        /// 对Set<T>根据id 的查询的操作
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public T GetEntityById(object Id)
        {
            return this.Entities.Find(Id);
        }

        /// <summary>
        /// 这里对Set<T>是带条件的操作
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public T Get(Expression<Func<T, bool>> where)
        {
            return this.Entities.Where(where).FirstOrDefault<T>();
        }



        /// <summary>
        /// 查询所有的
        /// </summary>
        /// <returns></returns>
        public IEnumerable<T> GetALLEntity()
        {
            //  throw new NotImplementedException();

            IEnumerable<T> query = this.Entities;

            return query;
        }

        /// <summary>
        /// 查询所有带条件
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> GetAllEntityWhere(Expression<Func<T, bool>> where)
        {
            IQueryable<T> query = this.Entities.Where(where);
            return query;

        }


        /// <summary>
        /// 分页方法
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public IList<T> GetPageEntities(int pageIndex, int PageSize)
        {
            IList<T> List = this.Entities.Skip(pageIndex * PageSize).Take(PageSize).ToList();
            return List;

        }


        /// <summary>
        /// 分页带查询条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<T> GetPageEntities(int pageIndex, int PageSize, Expression<Func<T, bool>> where)
        {
            // throw new NotImplementedException();
            IList<T> List = this.Entities.Where(where).Skip(pageIndex * PageSize).Take(PageSize).ToList();
            return List;

        }



        /// <summary>
        /// Save 保存确认方法
        /// </summary>
        public int Save()
        {
            return this._context.SaveChanges();

        }
    }
}

IDbContext.cs:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseFrame.Core.Data
{
    public interface IDbContext
    {
        /// <summary>
        /// Get DbSet
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <returns>DbSet</returns>
        IDbSet<TEntity> Set<TEntity>() where TEntity : class;

        /// <summary>
        /// Save changes
        /// </summary>
        /// <returns></returns>
        int SaveChanges();

        /// <summary>
        /// Execute stores procedure and load a list of entities at the end
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <param name="commandText">Command text</param>
        /// <param name="parameters">Parameters</param>
        /// <returns>Entities</returns>
        IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters)
            where TEntity : class, new();

        /// <summary>
        /// Creates a raw SQL query that will return elements of the given generic type.  The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type.
        /// </summary>
        /// <typeparam name="TElement">The type of object returned by the query.</typeparam>
        /// <param name="sql">The SQL query string.</param>
        /// <param name="parameters">The parameters to apply to the SQL query string.</param>
        /// <returns>Result</returns>
        IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters);

        /// <summary>
        /// Executes the given DDL/DML command against the database.
        /// </summary>
        /// <param name="sql">The command string</param>
        /// <param name="doNotEnsureTransaction">false - the transaction creation is not ensured; true - the transaction creation is ensured.</param>
        /// <param name="timeout">Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used</param>
        /// <param name="parameters">The parameters to apply to the command string.</param>
        /// <returns>The result returned by the database after executing the command.</returns>
        int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters);
    }
}

3、在EF CodeFirst中使用

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseFrame.Entity
{
    using EnterpriseFrame.Core.Data;

    public partial class EnterpriseContext : DbContext, IDbContext
    {
        public EnterpriseContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }
        #region Entity
        public virtual DbSet<Admin> Admins { get; set; }
        public virtual DbSet<ArticleInfo> ArticleInfoes { get; set; }
        public virtual DbSet<ArticleRelation> ArticleRelations { get; set; }
        public virtual DbSet<ArticleType> ArticleTypes { get; set; }
        public virtual DbSet<FriendsLink> FriendsLinks { get; set; }
        public virtual DbSet<Permission> Permissions { get; set; }
        public virtual DbSet<Role_Permission> Role_Permission { get; set; }
        public virtual DbSet<Role> Roles { get; set; }
        public virtual DbSet<SiteALlConfig> SiteALlConfigs { get; set; }
        public virtual DbSet<SiteMessage> SiteMessages { get; set; }
        #endregion

        #region Utilities

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //dynamically load all configuration
            //System.Type configType = typeof(LanguageMap);   //any of your configuration classes here
            //var typesToRegister = Assembly.GetAssembly(configType).GetTypes()

            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
            .Where(type => !String.IsNullOrEmpty(type.Namespace))
            .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
            //...or do it manually below. For example,
            //modelBuilder.Configurations.Add(new LanguageMap());

            modelBuilder.Entity<ArticleInfo>()
                .Property(e => e.ArtContent)
                .IsUnicode(false);


            modelBuilder.Entity<SiteMessage>()
                .Property(e => e.MsgContent)
                .IsUnicode(false);

            modelBuilder.Entity<SiteMessage>()
                .Property(e => e.MsgReply)
                .IsUnicode(false);

            base.OnModelCreating(modelBuilder);
        }
        #endregion

        #region Methods

        /// <summary>
        /// Create database script
        /// </summary>
        /// <returns>SQL to generate database</returns>
        public string CreateDatabaseScript()
        {
            return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
        }

        /// <summary>
        /// Get DbSet
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <returns>DbSet</returns>
        public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
        {
            return base.Set<TEntity>();
        }

        /// <summary>
        /// Execute stores procedure and load a list of entities at the end
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <param name="commandText">Command text</param>
        /// <param name="parameters">Parameters</param>
        /// <returns>Entities</returns>
        public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : class, new()
        {
            //add parameters to command
            if (parameters != null && parameters.Length > 0)
            {
                for (int i = 0; i <= parameters.Length - 1; i++)
                {
                    var p = parameters[i] as DbParameter;
                    if (p == null)
                        throw new Exception("Not support parameter type");

                    commandText += i == 0 ? " " : ", ";

                    commandText += "@" + p.ParameterName;
                    if (p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Output)
                    {
                        //output parameter
                        commandText += " output";
                    }
                }
            }

            var result = this.Database.SqlQuery<TEntity>(commandText, parameters).ToList();

            //performance hack applied as described here - http://www.nopcommerce.com/boards/t/25483/fix-very-important-speed-improvement.aspx
            bool acd = this.Configuration.AutoDetectChangesEnabled;
            try
            {
                this.Configuration.AutoDetectChangesEnabled = false;

                throw new System.NotImplementedException();//未实现
                //for (int i = 0; i < result.Count; i++)
                //    result[i] = AttachEntityToContext(result[i]);
            }
            finally
            {
                this.Configuration.AutoDetectChangesEnabled = acd;
            }

            return result;
        }

        /// <summary>
        /// Creates a raw SQL query that will return elements of the given generic type.  The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type.
        /// </summary>
        /// <typeparam name="TElement">The type of object returned by the query.</typeparam>
        /// <param name="sql">The SQL query string.</param>
        /// <param name="parameters">The parameters to apply to the SQL query string.</param>
        /// <returns>Result</returns>
        public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
        {
            return this.Database.SqlQuery<TElement>(sql, parameters);
        }

        /// <summary>
        /// Executes the given DDL/DML command against the database.
        /// </summary>
        /// <param name="sql">The command string</param>
        /// <param name="doNotEnsureTransaction">false - the transaction creation is not ensured; true - the transaction creation is ensured.</param>
        /// <param name="timeout">Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used</param>
        /// <param name="parameters">The parameters to apply to the command string.</param>
        /// <returns>The result returned by the database after executing the command.</returns>
        public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
        {
            int? previousTimeout = null;
            if (timeout.HasValue)
            {
                //store previous timeout
                previousTimeout = ((IObjectContextAdapter)this).ObjectContext.CommandTimeout;
                ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = timeout;
            }

            var transactionalBehavior = doNotEnsureTransaction
                ? TransactionalBehavior.DoNotEnsureTransaction
                : TransactionalBehavior.EnsureTransaction;
            var result = this.Database.ExecuteSqlCommand(transactionalBehavior, sql, parameters);

            if (timeout.HasValue)
            {
                //Set previous timeout back
                ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = previousTimeout;
            }

            //return result
            return result;
        }

        #endregion
    }
}

文笔有限,就直接贴代码了。记录下自己开发需要到的干货。希望不会误导路过的各位,文中若有误,还望路过的道友指出。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏ASP.NET MVC5 后台权限管理系统

ASP.NET MVC5+EF6+EasyUI 后台管理系统(19)-权限管理系统-用户登录

我们之前做了验证码,登录界面,却没有登录实际的代码,我们这次先把用户登录先完成了,要不权限是讲不下去了 把我们之前的表更新到EF中去 ? 登录在Account控...

267100
来自专栏码字搬砖

httpclient post发送json数组并解决json乱码问题

77020
来自专栏Java成神之路

UML_学习_00_资源贴

10530
来自专栏魂祭心

原 Expression Trees学习

29550
来自专栏大内老A

我的WCF之旅(4):WCF中的序列化[下篇]

XMLSerializer 提到XMLSerializer,我想绝大多数人都知道这是asmx采用的Serializer。首先我们还是来看一个例子,通过比较Ma...

223100
来自专栏一枝花算不算浪漫

[读书笔记]C#学习笔记六: C#3.0Lambda表达式及Linq解析

402110
来自专栏哲学驱动设计

lambda表达式-编译测试

我不会IL,也不想学IL,所以为了测试Lambda表达式编译的原理,只能使用调试进行试验来认识: 第一种情况,当Lambda表达式没有用到函数外的变量时: cl...

22470
来自专栏Java成神之路

求助-钉钉开发异步消息失败

24840
来自专栏菩提树下的杨过

Linq之ToDictionary<TSource, TKey, TElement>的写法

以前一直用 var query = xxx.Select(c=>new {c.X,c.Y}); 来取表中的某二列字段,今天有个应用需要转成Dictionary<...

232100
来自专栏WindCoder

spring JPA相关命名规则

36310

扫码关注云+社区

领取腾讯云代金券