Asp.Net MVC+EF+三层架构搭建

三层架构与MVC的关系

三层架构是一个分层式的软件体系架构设计,分为:表现层(UI)、业务逻辑层(BLL)、数据访问层(DAL)。分层的目的是为了实现“高内聚,低耦合”的思想,有利于系统后期的维护、更新或者移植。

MVC是一个设计模式,分为::MVC 即Model(模型),View(视图),Controller(控制)。

MVC与三层架构不是一个等级的。

我个人认为MVC其实就是把三层中的UI层又细分成了三层而已

开发环境

vs2013+mvc5+ef6+sql2008

解决方案设计

  • Common.Utility:共用类库
  • Mymvc.BLL:逻辑层,引用DAL、Model
  • Mymvc.DAL:数据访问层,应用Model
  • Mymvc.Model:实体层,对应数据库
  • Mymvc.Web:MVC网站,引用Common.Utility、BLL、Model

算是比较简单的三层+MVC,先看下框架图

Common.Utility:共用类库

因为用到了缓存,所以发下缓存类代码

CacheHelper.cs

using System;
using System.Web;
using System.Collections;

namespace Common.Utility
{
    /// <summary>
    /// Cache辅助类
    /// </summary>
    public class CacheHelper
    {
        /// <summary>
        /// 获取数据缓存
        /// </summary>
        /// <param name="CacheKey"></param>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        public static void RemoveCache(string CacheKey)
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            _cache.Remove(CacheKey);
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public static void RemoveCache()
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                _cache.Remove(CacheEnum.Key.ToString());
            }
        }
    }
}
View Code


Mymvc.Model:实体层

model层使用dbfirst模式生成

后边几步比较简单就不上图了

namespace Mymvc.Model
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class dbModel : DbContext
    {
        public dbModel()
            : base("name=dbModel")
        {
        }

        public virtual DbSet<admin> admin { get; set; }
        public virtual DbSet<news> news { get; set; }
        public virtual DbSet<newsClass> newsClass { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }
}
View Code

生成后把app.config里的数据库链接字符串复制到mvc网站web.config中

Mymvc.DAL:数据访问层

 BaseDAL

using Mymvc.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Mymvc.DAL
{
    /// <summary>
    /// DAL基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public partial class BaseDAL<T> where T : class, new()
    {
        public dbModel dbContext = ContextFactory.GetCurrentContext();
        public T Add(T entity)
        {
            dbContext.Set<T>().Add(entity);
            dbContext.SaveChanges();
            return entity;
        }
        public int AddRange(IEnumerable<T> entities)
        {
            dbContext.Set<T>().AddRange(entities);
            return dbContext.SaveChanges();
        }
        public bool Delete(object id)
        {
            Type t = typeof(T);
            string tableName = t.Name;
            string sql = string.Format("delete from from {0} where id=@ID", tableName);
            SqlParameter[] paras = new SqlParameter[]
            { 
               new SqlParameter("@ID",id)
            };
            return dbContext.Database.ExecuteSqlCommand(sql, paras) > 0;
        }
        public bool Delete(T entity)
        {
            dbContext.Set<T>().Attach(entity);
            dbContext.Entry<T>(entity).State = System.Data.Entity.EntityState.Deleted;
            return dbContext.SaveChanges() > 0;
        }
        public bool DeleteList(string idList)
        {
            Type t = typeof(T);
            string tableName = t.Name;
            string sql = string.Format("delete from from {0} where id in(@IDList)", tableName);
            SqlParameter[] paras = new SqlParameter[]
            { 
               new SqlParameter("@IDList",idList)
            };
            return dbContext.Database.ExecuteSqlCommand(sql, paras) > 0;
        }
        public bool Update(T entity)
        {
            dbContext.Set<T>().Attach(entity);
            dbContext.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified;
            return dbContext.SaveChanges() > 0;
        }
        public int Count()
        {
            return dbContext.Set<T>().Count();
        }
        public int Count(Expression<Func<T, bool>> predicate)
        {
            return dbContext.Set<T>().Count(predicate);
        }
        public bool Exist(Expression<Func<T, bool>> anyLambda)
        {
            return dbContext.Set<T>().Any(anyLambda);
        }
        public T Find(object id)
        {
            T _entity = dbContext.Set<T>().Find(id);
            return _entity;
        }
        public T Find(Expression<Func<T, bool>> whereLambda)
        {
            T _entity = dbContext.Set<T>().FirstOrDefault<T>(whereLambda);
            return _entity;
        }

        public T GetModelByCache(object id)
        {
            Type t = typeof(T);
            string tableName = t.Name;
            string CacheKey = tableName + "Model-" + id;
            object objModel = Common.Utility.CacheHelper.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = Find(id);
                    if (objModel != null)
                    {
                        int ModelCache = 5;//5分钟
                        Common.Utility.CacheHelper.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (T)objModel;
        }
        /// <summary>
        /// 查找对象列表:bll.FindList(x => x.ID > 25, true, x => x.ID);
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="whereLamdba"></param>
        /// <param name="isAsc"></param>
        /// <param name="orderLamdba"></param>
        /// <returns></returns>
        public IQueryable<T> FindList<S>(Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba)
        {
            var _list = dbContext.Set<T>().Where<T>(whereLamdba);
            if (isAsc) _list = _list.OrderBy<T, S>(orderLamdba);
            else _list = _list.OrderByDescending<T, S>(orderLamdba);
            return _list;
        }
        /// <summary>
        /// 数据分页:bll.FindPageList(pageIndex, pageSize, out totalItem, x => x.ID > 1, false, x => x.ID);
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <param name="whereLamdba"></param>
        /// <param name="isAsc"></param>
        /// <param name="orderLamdba"></param>
        /// <returns></returns>
        public IQueryable<T> FindPageList<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba)
        {
            var _list = dbContext.Set<T>().Where<T>(whereLamdba);
            totalRecord = _list.Count();
            if (isAsc) _list = _list.OrderBy<T, S>(orderLamdba).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            else _list = _list.OrderByDescending<T, S>(orderLamdba).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            return _list;
        }
    }
}
View Code

adminDAL,继承 BaseDAL

using Mymvc.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mymvc.DAL
{
    public partial class adminDAL : BaseDAL<admin>
    {
        //扩展方法添加在这里

        //public IEnumerable<admin> AddRange(IEnumerable<admin> entities)
        //{
        //    dbContext.Set<admin>().AddRange(entities);
        //    dbContext.SaveChanges();
        //    return entities;
        //}
    }
}
View Code

Mymvc.BLL:逻辑层

BaseBLL

using Mymvc.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Mymvc.BLL
{
    /// <summary>
    /// BLL基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract partial class BaseBLL<T> where T : class, new()
    {
        protected BaseDAL<T> Dal = new BaseDAL<T>();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T Add(T entity) { return Dal.Add(entity); }

        /// <summary>
        /// 添加集合
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public int AddRange(IEnumerable<T> entities) { return Dal.AddRange(entities); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity) { return Dal.Delete(entity); }

        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(object id) { return Dal.Delete(id); }

        /// <summary>
        /// 根据ID批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(object id) { return Dal.Delete(id); }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity) { return Dal.Update(entity); }

        /// <summary>
        /// 统计
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return Dal.Count();
        }

        /// <summary>
        /// 统计
        /// </summary>
        /// <param name="anyLambda"></param>
        /// <returns></returns>
        public int Count(Expression<Func<T, bool>> anyLambda)
        {
            return Dal.Count(anyLambda);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="anyLambda"></param>
        /// <returns></returns>
        public bool Exist(Expression<Func<T, bool>> anyLambda)
        {
            return Dal.Exist(anyLambda);
        }

        /// <summary>
        /// 根据ID查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T Find(object id)
        {
            return Dal.Find(id);
        }

        /// <summary>
        /// 根据lambda查找
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public T Find(Expression<Func<T, bool>> whereLambda)
        {
            return Dal.Find(whereLambda);
        }

        /// <summary>
        /// 从缓存中得到一个对象实体,缓存5分钟
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetModelByCache(object id)
        {
            return Dal.GetModelByCache(id);
        }

        /// <summary>
        /// 查找集合,使用方法:bll.FindList(x => x.ID > 10, true, x => x.ID);
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="whereLamdba"></param>
        /// <param name="isAsc"></param>
        /// <param name="orderLamdba"></param>
        /// <returns></returns>
        public IQueryable<T> FindList<S>(Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba)
        {
            return Dal.FindList(whereLamdba, isAsc, orderLamdba);
        }

        /// <summary>
        /// 分页,使用方法:bll.FindPageList(pageIndex, pageSize, out totalItem, x => x.ID > 1, false, x => x.ID);
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <param name="whereLamdba"></param>
        /// <param name="isAsc"></param>
        /// <param name="orderLamdba"></param>
        /// <returns></returns>
        public IQueryable<T> FindPageList<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba)
        {
            return Dal.FindPageList(pageIndex, pageSize, out totalRecord, whereLamdba, isAsc, orderLamdba);
        }
    }
}
View Code

adminBLL,继承 BaseBLL

using Mymvc.Model;
using Mymvc.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Mymvc.BLL
{
    public partial class adminBLL : BaseBLL<admin>
    {
        //扩展方法添加在这里

        //protected adminDAL mydal=new adminDAL();

        //public IEnumerable<admin> AddRange(IEnumerable<admin> entities) 
        //{
        //    return mydal.AddRange(entities); 
        //}
    }
}
View Code

Mymvc.Web:MVC5

引用Common.Utility、BLL、Model

简单用法

            adminBLL bll = new adminBLL();
            admin model = new admin();

            //Add
            model.username = "admin";
            model.password = "123";
            bll.Add(model);

            //Delete
            model = bll.Find(10);
            bll.Delete(model);//删除对象

            bll.Delete(10);//根据ID删除对象

            //update
            bll.Update(model);

            //find
            model = bll.Find(10);//找一个对象
            model = bll.GetModelByCache(10);//从缓存中找一个对象,默认缓存5分钟,在dal设置        

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值