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

MVC中Model BLL层Model模型互转

时间:2015-02-28 18:14:29      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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模型互转

标签:

原文地址:http://www.cnblogs.com/weiweithe/p/4305781.html

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