文档章节

步步为营:三层架构+ASP.NET MVC 架构的快速搭建(入门篇)

 木宛城主
发布于 2015/03/02 19:43
字数 1798
阅读 40
收藏 1

上一篇博客中《两天完成一个小型工程报价系统》,许多朋友向我讨源码。其实我之后没发了,确实那种三层架构没什么意思,只是我个人的孤芳自赏,很多的处理都不是很成熟。所以我重新架构了一番,以一个小例子来阐述我的架构模式,当然,这和企业级的架构还是差距很大,不过,还是值得一读。不积硅步,无以至江海,只有从小细节慢慢领悟,步步为营,才能设计出优秀的企业级架构。

 

  重构后的架构图:

解决方案里项目设计:

数据库访问层接口设计:IBaseDao

 

  • Repository模式

这儿使用了Repository模式,抽象出一个公共的接口IBaseDao,IBaseDao接口中定义了一系列契约(CRUD ),不管我的我数据访问用了那种技术,只要我实现了IBaseDao接口,则必须要实现接口定义的契约,所以接口的稳定至关重要,当然这样做的好处是显而易见的,减少了冗余代码。

public interface IBaseDao<T> where T:class
    {
        IList<T> GetAllEntities();
        bool SaveEntity(T entity);
        bool UpdateEntity(T entity);
        bool DeleteEntity(T entity);
        bool DeleteEntityByID(object id);
        T GetEntityByID(object id);
        IList<T> GetPageEntities(int pageIndex, int PageSize);
    }
  • 首先ICustomerDao继承了IBaseDao接口:

  public interface  ICustomerDao<Customer>:IBaseDao

  public interface  ICustomerDao:IBaseDao<Customer>

 public interface ICustomerDao:IBaseDao<Customer>
    {
    }

 

 

  • OracleDao实现ICustomerDao接口:
public class CustomerOracleDao:IDao.ICustomerDao
    {
        public IList<Model.Customer> GetAllEntities()
        {
            return GetAllEntitiesBySqlWhere("");

        }
        public IList<Model.Customer> GetAllEntitiesBySqlWhere(string sqlWhere)
        {

            string sql = string.Format("select * from Customer Where 1=1  {0}",sqlWhere);
            List<Customer> listCustomers = new List<Customer>();
            using (OracleDataReader odr = OracleHelper.ExecuteReader(OracleHelper.ConnectionString, System.Data.CommandType.Text, sql, null))
            {
                while (odr.Read())
                {
                    Model.Customer customer = new Customer();
                    customer.ID = odr.GetInt32(0);
                    customer.Name = odr.IsDBNull(1) ? "" : odr.GetString(1);
                    customer.Phone = odr.IsDBNull(2) ? "" : odr.GetString(2);
                    customer.Remark = odr.IsDBNull(3) ? "" : odr.GetString(3);
                    listCustomers.Add(customer);

                }
            }
            return listCustomers;
        }

        private int GetNewID()
        {
            string sql = "select s_customer.nextval from dual";
            return int.Parse(OracleHelper.ExecuteScalar(OracleHelper.ConnectionString, CommandType.Text, sql, null).ToString());
        }

        public bool SaveEntity(Model.Customer entity)
        {
            entity.ID = GetNewID();
            bool resultValue = false;
            string sql = string.Format(@"insert into Customer(ID,Name,Phone,Remark) values({0},'{1}','{2}','{3}')",entity.ID,entity.Name,entity.Phone,entity.Remark );
            if (OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString, CommandType.Text, sql, null) > 0)
            {
                resultValue = true;
            }
            return resultValue;
           
        }

        public bool UpdateEntity(Model.Customer entity)
        {
            string sql = string.Format("update Customer set Name='{0}',Phone='{1}',Remark='{2}' where ID={3}",entity.Name,entity.Phone,entity.Remark,entity.ID);
            return OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString, CommandType.Text, sql, null) > 0; 
        }

        public bool DeleteEntity(Model.Customer entity)
        {
            return DeleteEntityByID(entity.ID);
        }

        public bool DeleteEntityByID(object id)
        {
            string sql = string.Format("delete from Customer where ID={0} ",id);
            return OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionString, CommandType.Text, sql, null) > 0;
        }

        public Model.Customer GetEntityByID(object id)
        {
            string sqlWhere = string.Format(" and id={0}", id);
            int CID = (int)id;
            List<Model.Customer> list = GetAllEntitiesBySqlWhere(sqlWhere) as List<Customer>;
            return list.SingleOrDefault(c => c.ID == CID);
        }

        public IList<Model.Customer> GetPageEntities(int pageIndex, int PageSize)
        {
            throw new NotImplementedException();
        }
    }
  • EFDao实现ICustomerDao接口:
//用EF来实现数据访问层接口
    public class CustomerEFDao:ICustomerDao
    {
        //上下文网关
        private Hotel.Model.HotelContainer hotelDB = new Model.HotelContainer();
        /// <summary>
        /// 获取全部用户信息
        /// </summary>
        /// <returns></returns>
        public IList<Insigma.Hotel.Model.Customer> GetAllEntities()
        {
            return hotelDB.Customer.ToList<Customer>();
        }

        public bool SaveEntity(Insigma.Hotel.Model.Customer entity)
        {
            hotelDB.Customer.AddObject(entity);
            bool returnValue = false;
            //返回受影响的行数
            if (hotelDB.SaveChanges()>0)
            {
                returnValue = true;
            }
            return returnValue;
        }
        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(Insigma.Hotel.Model.Customer entity)
        {
            //新的方法来保存
            //hotelDB.Customer.Attach(entity);//附加到表对应集合缓冲中
            //hotelDB.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
            bool resultValue = false;
            //if (hotelDB.SaveChanges()>0)
            //{
            //    resultValue = true;
            //}
            //return resultValue;
            HotelContainer hoteEntities = new HotelContainer();
            var oldCustomer = (from c in hoteEntities.Customer
                               where c.ID == entity.ID
                               select c).SingleOrDefault<Customer>();
            oldCustomer.Name = entity.Name;
            oldCustomer.Phone = entity.Phone;
            oldCustomer.Remark = entity.Remark;
            if (hoteEntities.SaveChanges()>0)
            {
                resultValue = true;
            }
            return resultValue;
        }
        /// <summary>
        /// 删除客户信息(一般是软删除)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteEntity(Insigma.Hotel.Model.Customer entity)
        {
            return DeleteEntityByID(entity.ID);
        }
        /// <summary>
        /// 根据ID删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteEntityByID(object id)
        {
            
            int Id = (int)id;
            bool resultValue = false;
            var deleteCustomer = hotelDB.Customer.Where<Customer>(c => c.ID == Id).SingleOrDefault<Customer>();
            if (deleteCustomer != null)
            {
                hotelDB.Customer.DeleteObject(deleteCustomer);
                if (hotelDB.SaveChanges() > 0)
                {
                    resultValue = true;
                }
            }
            return resultValue;
        }
        /// <summary>
        ////// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Insigma.Hotel.Model.Customer GetEntityByID(object id)
        {
         
            int Id = (int)id;
            return hotelDB.Customer.Where<Customer>(c => c.ID == Id).SingleOrDefault<Customer>();
        }

        public IList<Insigma.Hotel.Model.Customer> GetPageEntities(int pageIndex, int PageSize)
        {
         
            var result = hotelDB.Customer.OrderBy(c =>c.ID).Skip<Customer>((pageIndex - 1) * PageSize).Take<Customer>(PageSize).ToList();
            return result;
        }
    }

 

  • SQLDao实现ICustomerDao接口:
public class CustomerSQLDao:ICustomerDao
    {

        public IList<Insigma.Hotel.Model.Customer> GetAllEntities()
        {
            using (CustomerTableAdapter adapter=new CustomerTableAdapter ())
            {
                var result = (from c in adapter.GetData()
                             select new Customer { ID=c.ID,Name=c.Name,Phone=c.Phone}).ToList<Customer>();
                return result;
            }
        }

        public bool SaveEntity(Insigma.Hotel.Model.Customer entity)
        {
            bool resultValue=false;
            using (CustomerTableAdapter adapter=new CustomerTableAdapter ())
            {
                if (adapter.Insert(entity.Name, entity.Phone, entity.Remark) > 0)
                {
                    resultValue = true;
                }
                return resultValue;
            }
        }

        public bool UpdateEntity(Insigma.Hotel.Model.Customer entity)
        {
            bool resultValue = false;
            using (CustomerTableAdapter adapter=new CustomerTableAdapter ())
            {
                if (adapter.UpdateCustomer(entity.Name,entity.Phone,entity.Remark,entity.ID)>0)
                {
                    resultValue = true;
                }
                return resultValue;
            }
        }

        public bool DeleteEntity(Insigma.Hotel.Model.Customer entity)
        {
            return DeleteEntityByID(entity.ID);
        }

        public bool DeleteEntityByID(object id)
        {
            bool resultValue = false;
            int CID=(int)id;
            using (CustomerTableAdapter adapter=new CustomerTableAdapter ())
            {
                //若取名Delete会变成Delete1
                if (adapter.DeleteCustomerByID(CID)>0)
                {
                    resultValue = true;
                }
                return resultValue;
            }
        }

        public Insigma.Hotel.Model.Customer GetEntityByID(object id)
        {
            using (CustomerTableAdapter adapter=new CustomerTableAdapter ())
            {
                var table=adapter.GetCustomerByID(Convert.ToInt32(id));
                Customer customer = new Customer();
                customer.ID = table[0].ID;
                customer.Name = table[0].Name;
                customer.Phone = table[0].Phone;
                return customer;

            }
        }

        public IList<Insigma.Hotel.Model.Customer> GetPageEntities(int pageIndex, int PageSize)
        {
            using (CustomerTableAdapter adapter=new CustomerTableAdapter ())
            {
                var result = (from c in adapter.GetData()
                              select new Customer { ID = c.ID, Name = c.Name, Phone = c.Phone }).OrderBy(c => c.ID).Skip<Customer>((pageIndex - 1) * PageSize).Take<Customer>(PageSize).ToList<Customer>();
                return result;
            }
        }
    }

这样我们就设计好了数据访问层实现方式,一共有三层方法来实现对数据库的访问,但是不管是OracleDao,还是EFDao,SQLDao实现了IBaseDao接口。这就是Repository模式。

业务逻辑层接口设计:IBLL

 

public interface ICustomerService
    {
        //Controller来调用业务逻辑
        IList<Customer> GetAllCustomers();
        bool SaveCustomer(Customer customer);
        bool UpdateCustomer(Customer customer);
        bool DeleteCustomer(int id);
        IList<Customer> GetPageCustomers(int pageIndex, int pageSize);
        Customer GetCustomerByID(int id);
    }
  • 这儿我们更希望通过配置文件的方式手动的去选择需要的数据访问层技术。所以这儿可以运用抽象工厂+反射的技术,当然还有IOC依赖注入
public class CustomerService:ICustomerService
    {
        public CustomerService()
        {
            //最好通过抽象工厂和反射 IOC依赖注入
            //this.customerDao = new EFDao.CustomerEFDao();
            this.customerDao = DaoFactory.GetCustomerDao();
        }
        /// <summary>
        /// 数据表数据访问层接口(好处:接口是稳定)
        /// </summary>
        private ICustomerDao customerDao;

        public ICustomerDao CustomerDao
        {
            get { return customerDao; }
            set { customerDao = value; }
        }
        public IList<Model.Customer> GetAllCustomers()
        {
            return CustomerDao.GetAllEntities();
        }

        public bool SaveCustomer(Model.Customer customer)
        {
            return CustomerDao.SaveEntity(customer);
        }

        public bool UpdateCustomer(Model.Customer customer)
        {
            return CustomerDao.UpdateEntity(customer);
        }

        public bool DeleteCustomer(int id)
        {
            return CustomerDao.DeleteEntityByID(id);
        }

        public IList<Model.Customer> GetPageCustomers(int pageIndex, int pageSize)
        {
            return CustomerDao.GetPageEntities(pageIndex, pageSize);
        }

        public Model.Customer GetCustomerByID(int id)
        {
            return CustomerDao.GetEntityByID(id);
        }
    }

 

  • 抽象共厂的实现,因为反射出实例很耗费性能,所以运用了缓存来减轻负担:
public class DaoCache
    {
        public DaoCache()
        {

        }
        public static object GetDao(string key)
        {
            System.Web.Caching.Cache daoCache = HttpRuntime.Cache;
            return daoCache.Get(key);
        }
        public static void InsertDaoCache(string key, object value)
        {
            if (GetDao(key) == null)
            {
                System.Web.Caching.Cache daoCache = HttpRuntime.Cache;
                daoCache.Insert(key, value);
            }
        }
    }
  • 抽象工厂反射实例:
public class DaoFactory
    {
       public static readonly string DaoPath=System.Configuration.ConfigurationManager.AppSettings["DaoPath"];
        public static readonly string DaoHZ=System.Configuration.ConfigurationManager.AppSettings["HZ"];
        public static object CreateDao(string assemblyPath, string objType)
        {
            var cacheDao = DaoCache.GetDao(objType);
            if (cacheDao==null)
            {
                cacheDao = Assembly.Load(assemblyPath).CreateInstance(objType);
                DaoCache.InsertDaoCache(objType, cacheDao);
            }
            return cacheDao;
        }
        public static ICustomerDao GetCustomerDao()
        {
            return CreateDao(DaoPath, string.Format("{0}.Customer{1}", DaoPath, DaoHZ)) as ICustomerDao;
        }
    }

这样,最简单的业务逻辑层完成了,当然这儿很复杂,还需要用到Facade模式。

表示层(MVC)

 

  • 表示层当然是指挥家Controller去访问业务逻辑层,把数据装配到Model中,交给View来显示。
public class CustomerController : Controller
    {
       // private HotelContainer db = new HotelContainer();
        private Insigma.Hotel.IBLL.ICustomerService customerService =new BLL.CustomerService();
        //
        // GET: /Customer/

        public ViewResult Index()
        {
            return View(customerService.GetAllCustomers());
        }

        //
        // GET: /Customer/Details/5

        public ViewResult Details(int id)
        {
            Customer customer = customerService.GetCustomerByID(id);
            return View(customer);
        }

        //
        // GET: /Customer/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                customerService.SaveCustomer(customer);
                return RedirectToAction("Index");  
            }

            return View(customer);
        }
        
        //
        // GET: /Customer/Edit/5
 
        public ActionResult Edit(int id)
        {
            Customer customer = customerService.GetCustomerByID(id);
            return View(customer);
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(Customer customer)
        {
            if (ModelState.IsValid)
            {
                customerService.UpdateCustomer(customer);
                return RedirectToAction("Index");
            }
            return View(customer);
        }

        //
        // GET: /Customer/Delete/5
 
        public ActionResult Delete(int id)
        {
            Customer customer = customerService.GetCustomerByID(id);
            return View(customer);
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            customerService.DeleteCustomer(id);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
      
        }
    }
  • 现在只需要修改配置文件就可以灵活的选择OracleDao、EFDao、SQLDao,而我们的项目丝毫不会有任何改动:

     

    <add key="DaoPath" value="Insigma.Hotel.EFDao" />
        <add key="HZ" value="EFDao" />

     

     

     

总结:

这是对稍早那篇文章的总结,接口引入稳定了开发。反射的引用让程序员更加关注业务层,提高了开发效率。

 

牛人的架构设计图:来自刘冬.NET

 

 

 

 

 

© 著作权归作者所有

共有 人打赏支持
粉丝 2
博文 222
码字总数 199010
作品 0
黄浦
mvc与三层结构终极区别

注:本文章内所有内容都来自互联网,本人主要是起了一个收集的作用 http://www.cnblogs.com/zhhh/archive/2011/06/10/2077519.html 又看到有人在问三层架构和MVC的关系,感觉这种问题有点教条...

长平狐
2012/11/12
10.4K
0
自学MVC看这里——全网最全ASP.NET MVC 教程汇总

MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC。小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MVC技术的学习者提供一个整合学习入口。本文从Why,W...

葡萄城控件技术团队
2016/08/18
204
0
实现基于ASP.NET MVC快速开发平台

最近一个月,自己利用业余时间开发一个基于ASP.NET MVC快速开发平台。现在勉强算个原型,但是感觉不是那么回事,把实现的过程写出来,欢迎大家拍砖。 开发思想:利用Asp.Net MVC的思想,实现自...

王二狗子11
01/01
0
0
ASP.NET MVC 5 基本构成

MVC模式简介: MVC模式两种理解:一种是表现模式,另外一种是架构模式。它将应用程序分成三个主要组件即:视图(View)控件器(Controller)模型(Model) M: Model主要是存储或者是处理数据的组...

swjian1997
2017/02/15
0
0
.NET系列文章——近一年文章分类整理,方便各位博友们查询学习

由于博主今后一段时间可能会很忙(准备出书:《.NET框架设计—模式、配置、工具》,外加换了新工作),所以博客会很少更新; 在最近一年左右时间里,博主各种.NET技术类型的文章都写过,根据...

王清培
2014/03/02
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

maven坐标和依赖

一、maven坐标详解 <groupId>com.fgt.club</groupId><artifactId>club-common-service-facade</artifactId><version>3.0.0</version><packaging>jar</packaging> maven的坐标元素说......

老韭菜
今天
1
0
springmvc-servlet.xml配置表功能解释

问:<?xml version="1.0" encoding="UTF-8" ?> 答: xml version="1.0"表示是此xml文件的版本是1.0 encoding="UTF-8"表示此文件的编码方式是UTF-8 问:<!DOCTYPE beans PUBLIC "-//SPRING//......

隐士族隐逸
今天
1
0
基于TP5的微信的公众号获取登录用户信息

之前讲过微信的公众号自动登录的菜单配置,这次记录一下在TP5项目中获取自动登录的用户信息并存到数据库的操作 基本的流程为:微信设置自动登录的菜单—>访问的URL指定的函数里获取用户信息—...

月夜中徘徊
今天
0
0
youTrack

package jetbrains.teamsys.license.runtime; 计算lis package jetbrains.ring.license.reader; 验证lis 安装后先不要生成lis,要把相关文件进行替换 ring-license-checker-1.0.41.jar char......

max佩恩
今天
1
0
12.17 Nginx负载均衡

Nginx负载均衡 下面的dig看到可以返回2个IP,就是解析出来的IP,这样我们可以做负载均衡。 dig www.qq.com 1.vim /usr/local/nginx/conf/vhost/fuzai.conf 2.添加如下配置 upstream qq //定义...

芬野de博客
今天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部