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. 工具类注入需要的service

    /** * 从redis获取信息 * @author yy * */ @Component//关键一:添加此注解才能被spring扫描到 public class CacheUtil { privat ...

  2. [MyBatis]查询SQL得到一个数字

    Mapper文件中写: <select id="findExpiredCount" resultType="integer" parameterType= ...

  3. Android 美团Robust热更新 使用入门

    Android热更新方案Robust 相信很多人都认识了解过 热修复.热更新.热补丁(对于这个技术也没有特别标准的一种叫法,下面我统一叫热更新),之后的一年里,各种热更新方案如雨后春笋般出现,比较耳熟 ...

  4. webpy简单入门---1

    1. 2. 3. 4.

  5. java1.7新特性:try-with-resources

    转载:https://blog.csdn.net/fanxiaobin577328725/article/details/53067163 首先看代码: import org.junit.Test; ...

  6. goroutine 调度算法

    自从开始使用 Go 语言,到现在也有一年多了,虽不算精通,但也算小有理解.在这里简单记录一下我的心得(其实是学习别人的心得) goroutine,Go 语言中 cpu 运行的最小单元,与 lua 携程 ...

  7. React Native小知识点记录

    1>查看 RN 的所有历史版本: npm view react-native versions -json 2>查看 RN 的当前版本: npm view react-native ver ...

  8. 爬虫——简单处理js中嵌入的json数据

    看了群里一个人提问道https://www.amazon.com/,商品分类那里无法用xpath拿得到列表.遂对其研究. 通过抓包工具可以得知,原始数据存在于js代码中,我的方式是手动解析js,从里面 ...

  9. 架构模式: 客户端 UI 构建

    架构模式: 客户端 UI 构建 上下文 您已应用微服务架构模式.服务由业务能力/面向子域的团队开发,这些团队也负责用户体验.一些UI屏幕/页面显示来自多个服务的数据.例如,考虑亚马逊风格的产品详细信息 ...

  10. APP安全_Android渗透环境

    Android渗透 移动APP大多通过WEB API服务的方式与服务端进行交互,这种模式把移动安全和web安全绑在一起.常见的web漏洞在移动APP中也存在,比如SQL注入,文件上传,中间件/serv ...