码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-08-23 19:33:08      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:attach   sql   man   show   size   sch   string   cli   表现层   

三层架构与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设置        

 

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

标签:attach   sql   man   show   size   sch   string   cli   表现层   

原文地址:https://www.cnblogs.com/webapi/p/9525118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!