WedeNet2018.Web-UI层:
结构如下:

首先,在Controller中定义BaseController,以便加入统一处理逻辑,如下:

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WedeNet2018.Common; namespace WedeNet2018.Web.Controllers
{
/// <summary>
/// 控制器基类
/// </summary>
public class BaseController : Controller
{
protected ILog log { get; set; } protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
log.Info("---进入BaseController---");
} }
}

在这里我们也声明了log4net变量,可在其派生类中决定使用哪一种配置的log。
以默认控制器为例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WedeNet2018.BussinessLogic;
using PagedList;
using WedeNet2018.Infrastructure;
using System.Text.RegularExpressions;
using WedeNet2018.Common;
using WedeNet2018.Common.Models;
using log4net; namespace WedeNet2018.Web.Controllers
{
public class HomeController : BaseController
{
private OrdersBussinessLogic orderBll;
private ElongOrdersDetailsBussinessLogic elongBll;
private EmployeesBussinessLogic employeeBll; public HomeController(OrdersBussinessLogic BLL, ElongOrdersDetailsBussinessLogic ElongBll, EmployeesBussinessLogic EmployeeBll)
{
log = LoggerHelper.WedeNetLogger;
orderBll = BLL;
elongBll = ElongBll;
employeeBll = EmployeeBll; log.Info("----HomeController初始化完成。----");
} public ActionResult Index(int? page)
{
log.InfoFormat("初始化首页数据,page={0}", page);
int pageSize = 10;
int pageNum = (page ?? 1); var orders = orderBll.GetOrders(46).OrderBy(o => o.Id); return View(orders.ToPagedList(pageNum,pageSize)); } public ActionResult Create()
{
return View();
} [HttpPost]
public ActionResult Create(Orders order)
{
if (string.IsNullOrEmpty(order.OrderSn))
{
ModelState.AddModelError("OrderSn", "OrderSn不能为空");
}
if (order.UserId<0)
{
ModelState.AddModelError("UserId", "UserId必须大于0");
}
if (string.IsNullOrEmpty(order.UserName))
{
ModelState.AddModelError("UserName", "UserName不能为空");
}
if (string.IsNullOrEmpty(order.UserMobile))
{
ModelState.AddModelError("UserMobile", "UserMobile不能为空");
} if (string.IsNullOrEmpty(order.Email))
{
ModelState.AddModelError("Email", "Email不能为空");
}
else
{
if (!ModelState.IsValidField(order.Email) || !new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase).IsMatch(order.Email))
{
ModelState.AddModelError("Email", "Email不是正确的格式");
}
} if (ModelState.IsValid)
{
order.MerchantId = 46;
order.UserMobile = "18210928340";
order.DeliveryTypeId = 0;
order.ServiceCost = 0;
order.DeliveryCost = 0;
order.PayCardType = 0;
order.Enabled = true;
order.IsSettledUp = true;
order.HasRecievePay = true;
order.HaveGetRecievePay = true;
order.Created = DateTime.Now;
order.LastModified = DateTime.Now;
order.PaySn = "";
order.OrderType = 46;
order.Satisfaction = 0;
orderBll.Add(order); //插入ElongOrdersDetails表
ElongOrdersDetails detail = new ElongOrdersDetails();
detail.OrderGuid = Guid.NewGuid();
detail.OrderSn = order.OrderSn;
detail.OrderId = 0;
detail.Status = order.Status;
detail.Created = DateTime.Now;
detail.LastModified = DateTime.Now;
elongBll.Add(detail);
orderBll.Commit(); return RedirectToAction("Index");
} return View();
} public ActionResult Details(int id)
{
Orders order = orderBll.Find(id);
return View(order);
} public ActionResult Edit(int id)
{
Orders order = orderBll.Find(id);
return View(order);
} [HttpPost]
public ActionResult Edit(Orders orderEdit)
{
Orders order = orderBll.Find(orderEdit.Id);
if (string.IsNullOrEmpty(orderEdit.Email))
{
ModelState.AddModelError("Email", "Email不能为空");
}
else
{
if (!ModelState.IsValidField(orderEdit.Email) || !new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase).IsMatch(orderEdit.Email))
{
ModelState.AddModelError("Email", "Email不是正确的格式");
}
} if (ModelState.IsValid) {
order.Email = orderEdit.Email;
order.Status = orderEdit.Status;
order.ServiceCost = orderEdit.ServiceCost;
orderBll.Update(order);
}
return RedirectToAction("Index");
} public ActionResult Delete(int id)
{
orderBll.Delete(id);
return RedirectToAction("Index");
} public JsonResult GetEmployees() {
ResponseBase<List<Employees>> result = new ResponseBase<List<Employees>>();
try
{
var employees = employeeBll.GetAll().ToList();
result.IsSuccess = true;
result.Message = "查询成功!";
result.resultCode = "200";
result.ResultData = employees;
}
catch (Exception ex) {
result.IsSuccess = false;
result.Message = "查询异常!"+ex.Message;
result.resultCode = "500";
} return Json(result);
} }
}

注意到了,我在构造函数中进行了一些初始化,有日志的,有需要用到的BussinessLogic处理类,这些BussinessLogic处理类是依赖NInject注入实例化的,而且这三个处理类是属于不同的UnitOfWork和dbcontext(OrdersBussinessLogic和ElongOrdersDetailsBussinessLogic属于IWedeUnitOfWorks,而EmployeesBussinessLogic属于IXF0816UnitOfWorks)。

这里要说一下日志,在这个控制器中我把基类的日志初始化为WedeNetLogger,其实要分类打印的日志可以有很多,如:

根据需要对日志进行分类,并且在控制器的构造函数中根据情况进行初始化。

Ninject映射

要配置NInject,就需要先实现一个工厂,如:

using Ninject;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WedeNet2018.BussinessLogic;
using WedeNet2018.Infrastructure;
using WedeNet2018.Infrastructure.Components; namespace WedeNet2018.Web.Controllers
{
/// <summary>
/// Ninject工厂类
/// </summary>
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel; public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
} private void AddBindings()
{
ninjectKernel.Bind<AbsWedeDBContex>().To<WedeDBContex>();
ninjectKernel.Bind<IWedeUnitOfWorks>().To<WedeUnitOfWorks<AbsWedeDBContex>>().InSingletonScope();
ninjectKernel.Bind<OrdersBussinessLogic>().ToSelf();
ninjectKernel.Bind<ElongOrdersDetailsBussinessLogic>().ToSelf(); ninjectKernel.Bind<AbsXF0816DBContex>().To<XF0816DBContex>();
ninjectKernel.Bind<IXF0816UnitOfWorks>().To<XF0816UnitOfWorks<AbsXF0816DBContex>>().InSingletonScope();
ninjectKernel.Bind<EmployeesBussinessLogic>().ToSelf();
} protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
}
}
}

我在工厂类的AddBindings()方法里进行了各种映射关系配置。

实现了工厂以后,还需要在Global.asax中注册这个工厂类,如:

然后,我们就可以使用NInject了。

MVC4的Bundles

UI项目里往往需要加载很多js和css,一些通用的可以放在Bundles里让MVC帮忙,如:

bundles.Add(new ScriptBundle("~/bundles/jss").Include(
"~/Scripts/jquery-{version}.js",
"~/Bootstrap/js/bootstrap.js",
"~/js/common.js",
"~/js/layer/layer.js"
));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/site.css",
"~/Bootstrap/css/bootstrap.css",
"~/Bootstrap/css/bootstrap-grid.css",
"~/Bootstrap/css/bootstrap-reboot.css",
"~/js/layer/skin/layer.css",
"~/js/layer/skin/default/layer.css"
));

然后,就可以在视图模板中使用了,如:

UI展示层主要说明的就这些,另外Bootstrap、layer、jQuery、H5+CSS3内容不介绍了。

搭建自己的框架WedeNet(四)的更多相关文章

  1. 搭建自己的框架WedeNet(五)

    WedeNet2018.WedeWcfServices-WCF服务层:结构如下: 就是定义了服务契约接口和服务类,以OrderServices为例,如下: using System; using Sy ...

  2. 搭建自己的框架WedeNet(一)

    框架用到的技术: EF.UnitOfWork+Repository.Ninject.log4net.WCF.MVC.T4.windows服务.AOP前端技术:Bootstrap.layer.jQuer ...

  3. 搭建自己的框架WedeNet(三)

    WedeNet2018.BussinessLogic-业务逻辑层:结构如下: 基类: using System; using System.Collections.Generic; using Sys ...

  4. 搭建自己的框架WedeNet(二)

    WedeNet2018.Infrastructure-基础设施层:结构如下: Tools结构如下: 考虑到系统可能会有多个数据上下文(暂时以两个为例),所以根据需要定义两个T4模板用来生成对应的ent ...

  5. 搭建App主流框架_纯代码搭建(OC)

    转载自:http://my.oschina.net/hejunbinlan/blog/529778?fromerr=EmSuX7PR 搭建主流框架界面 源码地址在文章末尾 达成效果 效果图 注:本文部 ...

  6. 10分钟搭建 App 主流框架

    搭建主流框架界面 0.达成效果 我们玩iPhone应用的时候,有没发现大部分的应用都是上图差不多的结构,下面的TabBar控制器可以切换子控制器,上面又有Navigation导航条 我们本文主要是搭建 ...

  7. express + mongodb 搭建一个简易网站 (四)

    express + mongodb 搭建一个简易网站 (四) 目前网站整体页面都已经能全部展示了,但是,整个网站还有两个块需要做完才能算完整,一个连接数据库,目前网站上的数据都是抓取的本地假数据,所以 ...

  8. 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架

    转自:https://blog.csdn.net/eson_15/article/details/51312490 前面两节,我们整合了SSH并且抽取了service和action部分的接口,可以说基 ...

  9. 十分钟搭建App主流框架

    搭建主流框架界面 0.达成效果 Snip20150904_5.png 我们玩iPhone应用的时候,有没发现大部分的应用都是上图差不多的结构,下面的TabBar控制器可以切换子控制器,上面又有Navi ...

随机推荐

  1. 常用css模板

    段落超出显示省略号(可单行多行) .p-content{ overflow:hidden; text-overflow:ellipsis; display:-webkit-box; -webkit-b ...

  2. nginx状态码

    200:服务器成功返回网页 403:服务器拒绝请求.404:请求的网页不存在 499:客户端主动断开了连接.500:服务器遇到错误,无法完成请求.502:服务器作为网关或代理,从上游服务器收到无效响应 ...

  3. linux系统及命令学习

    1,基本概念 Shell(命令行):是一个程序,接受键盘输入的命令,并将命令传递给操作系统进行执行. Bash:Bourne Again Shell, 是大多数linux系统分之中所带的一种shell ...

  4. easyUI之window窗口

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  5. DeviceUtils

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> import androi ...

  6. .netcore centos配置systemctl自动启动

    systemd分两种服务系统和用户服务 对应存储位路径为系统(/usr/lib/systemd/system).用户(/etc/systemd/user/) [Unit] Description=ap ...

  7. 几句简单的python代码完成周公解梦功能

    <周公解梦>是靠人的梦来卜吉凶的一本于民间流传的解梦书籍,共有七类梦境的解述.这是非常传统的中国文化体系的一部分,但是如何用代码来获取并搜索周公解梦的数据呢?一般情况下,要通过爬虫获取数据 ...

  8. C2B电商三种主要模式的分析_数据分析师

    C2B电商三种主要模式的分析_数据分析师 在过去的一年中电商领域血雨腥风,尤其是天猫.京东.苏宁.当当.易讯等B2C电商打得不亦乐乎.而随着B2C领域竞争进入白热化阶段,C2B模式也在天猫" ...

  9. linux编译gpu_flow

    因为需要做双流,论文里面推荐到这个GPU版本的TVL1算法,于是开始编译. 一.下载源码 git clone https://github.com/feichtenhofer/gpu_flow.git ...

  10. BN和L2 NORM的区别

    bn是拉平各个feature的差异,而l2 norm是拉平各个样本的差异,本来各个样本的模长千变万化,按照距离的概念,差别是很大的,但是l2 norm后,距离就变得有一个上界了,显然样本间差异变小了. ...