ASP.Net MVC框架搭建

 一.框架搭建的初衷:

        1.平时工作就是敲代码,做了N多个项目,每上一个项目都要重新修改一些重复性的代码(这些代码完全可以省略,通过继承,反射等等的方式简化开发量)。

       2.项目成员不固定,水平参差不齐,没有固定的规范,通过纯SQL的方式写数据库操作逻辑,代码量很大,让新的项目成员难于下手,导致各自为营,框架越来越臃肿。

       3.C/S模式界面跟不上时代的步伐,界面显得很Lower,而且实施起来非常费劲。

二.框架涉及的技术点

      大概的技术点:ASP.NET MVC + 抽象工厂反射 + SqlSugar + Redis+ LayUI+ 消息队列MQ  

     ASP.NET:微软框架运行了几十年已经是非常成熟的一套框架了,针对中小型企业的系统应该已经够用了(如果是超高并发系统技术框架可以再调整)。

    SqlSugar:框架是基于.Net的轻量级的ORM框架,经过几年的技术积累也是足够应对大部分的要求,虽然SqlSugar不是微软官方的,但是在性能方面也是足够媲美EF框架,链接是我做测试用的,基本功能都能满足:https://blog.csdn.net/huanxiao8512/article/details/105880567

    LayUI:界面风格还是非常友好的,各种的组件在官网都有Demo展示,而且有自己的社区支持。

    消息队列MQ :MQ对于一些对高并发有需求的项目还是有点好处的,对边缘的业务比如之前我用到的插日志和插记录表完全可以利用消息队列进行对系统分摊压力。

三.详细设计

  1.大概的框架图(简单地画了下有点粗糙。)

  2.详细代码解释

1.controller与业务BLL类的交互

  controller继承了BaseController这个基类,具体代码如下:

namespace Web.YMH.Controllers.Customer
{
    [RoleActionFilter(Message = "Basic/Customer")]
    public class CustomerController : BaseController<T_CustomerInfo>
    {

        private ICustomerService customerService;
        public CustomerController()
        {
            customerService = (ICustomerService)ServiceFactory.CreateObject("Customer.T_Customer_Func");
            baseservice = customerService;
        }

    }
}

  (1.)BaseController类里面写了大量的经常使用的常见的方法(增删改查),直接通过接口调用,让其他所有的controller都不需要再重复写这样类似的重复代码了。

namespace Web.YMH.Controllers
{
    public abstract class BaseController<Tmodel> : Controller 
    {
        protected IBaseService<Tmodel> baseservice;
        protected UserInfo currentUser = Common.Commom.ReadUserInfo();
        public ActionResult PageView(PageData<Tmodel> model)
        {
            ViewData["dividPage"] = model.dividPage;
            ViewData["url"] = model.link;
            return View("_ViewPage");
        }


        [HttpGet]
        public ActionResult GetModel(Tmodel model, string strMsg)
        {
            if (model == null)
            {
                model = (Tmodel)this.TempData["modeladd"];
            }

            string Msg = "";
            baseservice.GetModelByID(ref model, ref Msg);
            ViewData["Msg"] = Msg;
            return View(model);
        }


        [HttpGet]
        public ActionResult ErrorMsg(string strMsg)
        {
            ViewData["Msg"] = strMsg;
            return View("_ViewErrorMsg");
        }

       // [HttpPost]
        public JsonResult Delete(string ID)
        {
            string strMsg = "";
            string[] Ids = ID.TrimEnd(',').Split(',');
            foreach (string id in Ids)
            {
                baseservice.DeleteModelByID(currentUser, Convert.ToInt32(id), ref strMsg);
            }
            return Json(strMsg, JsonRequestBehavior.AllowGet);
        }



        [HttpPost]
        public ActionResult Add(Tmodel modeladd)
        {
            string strMsg = "";
            if (baseservice.SaveModelToDB(currentUser, ref modeladd, ref strMsg))
            {
                return RedirectToAction("GetModelList");
            }
            else
            {
                this.TempData["modeladd"] = modeladd;
                return RedirectToAction("GetModel", new { model = modeladd, strMsg = strMsg });
            }
        }


        public string ErrorController(object obj)
        {
            return "<script>alert('" + obj.ToString() + "')</script>";
        }


    }
}

(2)通过工厂模式进行接口的初始化

//工厂基类 YMH
namespace YMH.Factory
{
  
    public class BaseFactory
    {
        public string asspath { get; set; }

        /// <summary>
        /// 创建一个实体
        /// </summary>
        /// <param name="ClassNamespace"></param>
        /// <param name="paramsStr"></param>
        /// <returns></returns>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            ClassNamespace = AssemblyPath + "." + ClassNamespace;
            object objtype = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);
            return objtype;
        }
    }
}
//Service 层的工厂类 YMH
namespace YMH.Factory
{
    public class ServiceFactory:BaseFactory
    {

        /// <summary>
        /// 创建一个实体
        /// </summary>
        /// <param name="ClassNamespace"></param>
        /// <param name="paramsStr"></param>
        /// <returns></returns>
        public static object CreateObject(string ClassNamespace)
        {
            string AssemblyPath = "BLLWeb";
            return CreateObject(AssemblyPath, ClassNamespace);
        }
    }
}
namespace BLLWeb
{
    public interface IBaseService<T>
    {
        bool GetModelByID(ref T model, ref string strMsg);

        bool DeleteModelByModelSql(UserModel userInfo, T model, ref string strMsg);

        bool DeleteModelByID(UserModel userInfo, int ID, ref string strMsg);

    }
}

这里解释下:上面的两个类和一个基类接口是通用反射的方法创建一个实体类来供前面的controller调用

    下次有空再写!!!时间不一定(今天客户服务器有点问题在重装,所以有点碎片时间。)

猜你喜欢

转载自blog.csdn.net/huanxiao8512/article/details/105659572