MVC中Model BLL層Model模型互轉

來源:互聯網
上載者:User

標籤:

MVC中Model BLL層Model模型互轉一. 模型通常可以做2種:充血模型和失血模型,一般做法是模型就是模型,不具備方法來操作,只具有屬性,這種叫做失血模型(可能不準確);具備對模型一定的簡單操作方法,不只是有屬性的模型叫做充血模型,如下:
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MvcApplication1.Models{    /// <summary>    /// 表現層實體集合    /// </summary>    public class UserCompanyViewModels    {        public UserViewModel User { set; get; }        public List<CompanyViewModel> Companys { set; get; }        public UserCompanyViewModels()        {            User = new UserViewModel();            Companys = new List<CompanyViewModel>();        }        /// <summary>        /// Bo To Vo        /// </summary>        /// <param name="user">業務層實體物件</param>        /// <param name="companys">業務層實體物件集合</param>        /// <returns>表現層實體</returns>        public UserCompanyViewModels ToVo(UserModel user, List<CompanyModel> companys)        {            UserCompanyViewModels models = new UserCompanyViewModels();            if (user != null)            {                models.User.UserID = user.UserID;                models.User.UserName = user.UserName;            }            if (companys != null && companys.Count > 0)            {                foreach (var item in companys)                {                    CompanyViewModel company = new CompanyViewModel();                    company.CompanyID = item.CompanyID;                    company.CompanyName = item.CompanyName;                    models.Companys.Add(company);                }            }            return models;        }    }    /// <summary>    /// 表現層實體    /// </summary>    public class UserViewModel    {        public int UserID { set; get; }        public string UserName { set; get; }    }    /// <summary>    /// 表現層實體    /// </summary>    public class CompanyViewModel    {        public int CompanyID { set; get; }        public string CompanyName { set; get; }    }    /// <summary>    /// 業務層實體    /// </summary>    public class UserModel    {        public int UserID { set; get; }        public string UserName { set; get; }        public string UserGander { set; get; }        public string UserAddress { set; get; }        /// <summary>        /// Vo To Bo        /// </summary>        /// <param name="models">表現層實體物件</param>        /// <returns>業務層實體</returns>        public UserModel ToBo(UserCompanyViewModels models)        {            UserModel user = new UserModel();            if (models != null&&models.User!=null)            {                user.UserID = models.User.UserID;                user.UserName = models.User.UserName;            }            return user;        }    }    /// <summary>    /// 業務層實體    /// </summary>    public class CompanyModel    {        public int CompanyID { set; get; }        public string CompanyName { set; get; }        public string CompanyAddress { set; get; }        /// <summary>        /// Vo To Bo        /// </summary>        /// <param name="models">表現層實體物件</param>        /// <returns>業務層實體</returns>        public List<CompanyModel> ToBo(UserCompanyViewModels models)        {            List<CompanyModel> companys = new List<CompanyModel>();            if (models != null&&models.Companys!=null && models.Companys.Count > 0)            {                foreach (var item in models.Companys)                {                    CompanyModel company = new CompanyModel();                    company.CompanyID = item.CompanyID;                    company.CompanyName = item.CompanyName;                    companys.Add(company);                }            }                return companys;        }    }}

在這個模型中具備了MVC中Model和BLL層model的轉換方法,可能有有人要問為什麼,我想說的是如果加入了這種模型轉換方法,在同一controller不同action要使用的時候,就可以輕鬆轉換整頁模式模型和邏輯層模型的轉換,不用重複寫代碼,簡單的封裝思想。


二. 在Controller中使用,如下:

                [HttpPost]        public ActionResult Create(FormCollection collection,UserCompanyViewModels models)        {            //如果控制項有ID的話,可以通過控制項Names屬性來取值,如            var a = collection["User.UserID"];            try            {                // TODO: Add insert logic here                UserModel user = new UserModel();                user = user.ToBo(models);                                //然後調用BLL層執行資料庫User表操作Save,Update等方法                  CompanyModel company = new CompanyModel();                List<CompanyModel> companys = company.ToBo(models);                //然後調用BLL層資料庫Company表操作Save,Update等方法                  return View("Create", models);            }            catch            {                return View("Create");            }        }

這裡可以看到,因為View視圖可能使用到的是一個複雜的model,這個model是業務層幾個model的組合,甚至是集合List類型,所以在這裡可以輕鬆轉換。

MVC中Model BLL層Model模型互轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.