ASP.NET MVC+EF 项目架构搭建

新建MVC项目UI

ASP.NET MVC+EF 项目架构搭建_第1张图片

然后分别建立类库,Model,IDAL,DALFactory,DAL,IBLL,BLL,Common

ASP.NET MVC+EF 项目架构搭建_第2张图片

Model里面添加EF实体 User生成数据库

ASP.NET MVC+EF 项目架构搭建_第3张图片

ASP.NET MVC+EF 项目架构搭建_第4张图片

IDAL层

IBasedal.cs

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

namespace IDAL
{
   public  interface IBaseDal
    {
        /// 
        /// 
        /// 
        /// 
        /// 
        IQueryable LoadEntities(System.Linq.Expressions.Expression> whereLambda);
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression> wherelambda, System.Linq.Expressions.Expression> orderByLambda, bool isasc);
        bool DeleteEntity(T entity);
        bool EditEntity(T entity);
        T AddEntity(T entity);
    }
}

IUserDal.cs实现IBaseDal接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace IDAL
{
   public  interface IUserDal:IBaseDal
    {
    }
}

IDbSession.cs接口(回话层接口DALFactory)

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

namespace IDAL
{
    public interface  IDbSession
    {
        DbContext db { get; }
        IUserDal userdal { get; set; }
        bool SaveChanges();
    }
}

ASP.NET MVC+EF 项目架构搭建_第5张图片

DALFactory(数据回话层)

DBSession.cs实现IDBSession

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDAL;
using DAL;
namespace DALFactory
{
    public class DBSession : IDbSession
    {
        public System.Data.Entity.DbContext db
        {
            get {
                return DBContextFactory.CReateDbContext();
            }
        }
        private IUserDal _UserDAl;
       public IUserDal userdal { get =>AbstractFactorycs.createIUserDal(); set => _UserDAl=value; }

        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}

DBSessionFactory.cs DBsession的工厂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using IDAL;
namespace DALFactory
{
    public   class DBSessionFactory
    {
        public static IDbSession CreateDBSession()
        {
            //面向接口编程 返回接口
            IDbSession   dbsession =(IDbSession) CallContext.GetData("dbsession");
            if (dbsession == null)
            {
               dbsession = new DBSession();
                CallContext.SetData("dbsession", dbsession);
            }
            return dbsession;
        }
    }
}

AbstractFactory.cs(DAL的抽象工厂 利用反射创建DAL)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Reflection;
using IDAL;
using DAL;
namespace DALFactory
{
    public class AbstractFactorycs
    {
        public static readonly string AssemblyPath = ConfigurationManager.AppSettings["AssemblyPath"];
        public static readonly string NameSpace = ConfigurationManager.AppSettings["NameSpace"];
        public static IUserDal createIUserDal()
        {
            string fullclassname = NameSpace + ".UserDal";
            return (IUserDal)CreateInstace(fullclassname);
        }
        private static object CreateInstace(string classname)
        {
            var assembly = Assembly.Load(AssemblyPath);//这里加载的是程序集名称 如DAL
            return assembly.CreateInstance(classname);//这里需要填写 命名空间.类名
        }
    }
}

ASP.NET MVC+EF 项目架构搭建_第6张图片

DAL层

DBContextFactory.cs (EF Context线程内唯一对象)

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace DAL
{
    public  class DBContextFactory
    {
        public static DbContext CReateDbContext()
        {

            DbContext dbcontext = (DbContext)CallContext.GetData("dbContext");
            if (dbcontext == null)
            {
                dbcontext = new MYMVCOAContainer();
                CallContext.SetData("dbContext", dbcontext);
            }
            return dbcontext;
        }
    }
}

UserDal(具体的dal类,实现IUserDAL接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
using System.Linq.Expressions;
using System.Data.Entity;
namespace DAL
{
    public class UserDal :IUserDal
    {
        MYMVCOAContainer db =(MYMVCOAContainer) DBContextFactory.CReateDbContext();
        public User AddEntity(User entity)
        {
            db.UserSet.Add(entity);
            db.SaveChanges();
            return entity;
        }

        public bool DeleteEntity(User entity)
        {
            db.UserSet.Remove(entity);
           return  db.SaveChanges()>0;
        }

        public bool EditEntity(User entity)
        {
            db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges() > 0;
        }

        public IQueryable LoadEntities(Expression> whereLambda)
        {
            return db.UserSet.Where(whereLambda);
        }

        public IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, Expression> wherelambda, Expression> orderByLambda, bool isasc)
        {
            var temp = db.UserSet.Where(wherelambda);
            totalCount = temp.Count();
            if (isasc)
            {
                temp = temp.OrderBy(orderByLambda).Skip(pageIndex - 1).Skip(pageSize);
            }
            else {
                temp = temp.OrderByDescending(orderByLambda).Skip(pageIndex - 1).Skip(pageSize);
            }
            return temp;
        }
    }
}

ASP.NET MVC+EF 项目架构搭建_第7张图片

IBLL层

IBaseService.cs (业务接口)

using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
     public  interface IBaseService where T:class ,new()
    {
        IDbSession CurrentDBSession { get; }
       IBaseDal CurrentDal { get; set; }
        IQueryable LoadEntities(System.Linq.Expressions.Expression> wherelambda);
        IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression> wherelambda, System.Linq.Expressions.Expression> orderByLambda, bool isasc);
        bool DeleteEntity(T entity);
        bool EditEntity(T entity);
        T AddEntity(T entity);
    }
}

IUserService.cs用户业务接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace IBLL
{
     public  interface IUserService:IBaseService
    {
       
    }
}

BLL层

BaseService.cs(业务基类 实现IBaseService接口)

using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DALFactory;
namespace BLL
{
    //接口的成员不用声明为 public,private ,protected
    //接口的实现需要设置为public
    public abstract  class BaseService where T:class ,new()
    {
       public IDbSession CurrentDBSession
        {
            get
            {
                return DBSessionFactory.CreateDBSession();
            }
        }
        public IBaseDal CurrentDal { get; set; }
        public abstract void SetCurrentDal();//这个抽象方法用于(子类)实现, 当前Service是哪一类,需要 哪个DAL。(CurrentDal)(多态)
        public BaseService()
        {
            SetCurrentDal();
        }
        public IQueryable LoadEntities(System.Linq.Expressions.Expression> wherelambda)
        {
            //return  CurrentDBSession.userdal.LoadEntities(wherelambda);
            return CurrentDal.LoadEntities(wherelambda);
        }
       public   IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression> wherelambda, System.Linq.Expressions.Expression> orderByLambda, bool isasc)
        {
          return  this.CurrentDal.LoadPageEntities(pageIndex, pageSize,out totalCount, wherelambda, orderByLambda, isasc);
        }
       public  bool DeleteEntity(T entity)
        {
          this.CurrentDal.DeleteEntity(entity);
            return CurrentDBSession.SaveChanges();
                
        }
        public bool EditEntity(T entity)
        {
             CurrentDal.EditEntity(entity);
            return CurrentDBSession.SaveChanges();
        }
       public T AddEntity(T entity)
        {
             CurrentDal.AddEntity(entity);
             CurrentDBSession.SaveChanges();
            return entity;
        }

    }
}

UserService.cs(继承BaseService,实现IBaseService)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IBLL;
namespace BLL
{
    //BaseService实现了 IUserService
    //UserService继承了BaseService,实现了IUserSerivce
   public class UserService : BaseService,IUserService
    {
        public override void SetCurrentDal()
        {
            this.CurrentDal = this.CurrentDBSession.userdal;//多态
        }
        //public void add(User u)
        //{
        //    this.CurrentDal.AddEntity(u);
        //    this.CurrentDBSession.SaveChanges();
        //}
    }
}

到此项目搭建基本完成。

由于让业务层和UI层解耦,还需要使用Spring.Net进行依赖注入和控制反转。

这里需要注意Expressions是表达式 Func 是Lambda.

IQuerable有延时加载。一般DAL返回IQuerable给业务层。业务层再使用IEmuerable.



你可能感兴趣的:(.NET,FrameWork)