核心:

主要利用MVC的区域功能,实现项目模块独立开发和调试。

目标:

各个模块以独立MVC应用程序存在,即模块可独立开发和调试。

动态注册各个模块路由。

一:新建解决方案目录结构

如图:

二:EasyMvc.Core即为核心库。

核心库三大主力:AreaConfig 、RouteConfig 、FilterConfig

AreaConfig :为区域启动停止以及其他状态时注入的方法,类似与Global.asax里面Application_Start、Application_End方法。

RouteConfig :路由方法,类似与App_Start里面RouteConfig方法。

FilterConfig:区域全局过滤器,针对当前路区域所有控制器的过滤(未实现)。

AreaConfig.cs

    public class AreaConfig
{
public virtual void Area_Start()
{ }
public virtual void Area_End()
{ }
}

RouteConfig.cs

    public class RouteConfig
{
public virtual void RegisterRoutes(AreaRoute routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public virtual void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
} private static Dictionary<string, RouteBase> _routes = new Dictionary<string, RouteBase>();
private static Dictionary<string, RouteConfig> _areas = new Dictionary<string, RouteConfig>(); #region Fields
public string Name { get; set; }
public string Path { get; set; }
public string[] NameSpaces { get; set; }
#endregion #region Route
public string FormatName(string name)
{
if (string.IsNullOrEmpty(name))
throw new RouteException("路由名称为空", Name);
return string.Format("{0}_{1}", Name, name);
}
public string FormatUrl(string url)
{
if (string.IsNullOrEmpty(url))
throw new RouteException("路由地址为空", Name);
return string.Format("{0}/{1}", Path, url);
}
public string[] FormatNameSpaces(string[] namespaces)
{
if (namespaces != null && namespaces.Length > 0)
{
List<string> list = NameSpaces == null ? (new List<string>()) : NameSpaces.ToList();
foreach (var item in namespaces)
{
if (!list.Contains(item))
list.Add(item);
}
NameSpaces = list.ToArray();
}
return null;
}
public void AddRoute(string routeName, RouteBase route)
{
if (!string.IsNullOrEmpty(routeName) && route != null)
_routes.Add(routeName, route);
} public void CheckName(string routeName)
{
if (_routes.Any(op => op.Key == routeName))
throw new RouteException("路由名称已存在", Name, routeName);
}
#endregion #region Area
public void Init()
{
Regist(RouteTable.Routes);
}
private void Regist(RouteCollection routes)
{
if (_areas.ContainsKey(Name))
throw new AreaExcption("区域已存在", Name);
_areas[Name] = this;
AreaRegistrationContext context = new AreaRegistrationContext(Name, routes);
AddNameSpaces(context);
RegisterArea(context);
if (Config.MConfig.IsDebug)
{
RegisterRoutes(routes);
}
}
private void AddNameSpaces(AreaRegistrationContext context)
{
if (NameSpaces != null && NameSpaces.Length > 0)
foreach (string item in NameSpaces)
{
context.Namespaces.Add(item);
}
}
private void RegisterArea(AreaRegistrationContext context)
{
AreaRoute route = new AreaRoute(this, context);
RegisterRoutes(route);
}
#endregion
}

FilterConfig.cs(未实现)

public class FilterConfig { }

三:模块重写三大核心类

App_Satrt下面的几个类,就是重写EasyMvc.Core的三大核心类的了。

AreaConfig.cs
    public class AreaConfig : EasyMvc.Core.AreaConfig
{
public override void Area_Start()
{ }
public override void Area_End()
{ }
}
RouteConfig.cs
    public class RouteConfig : EasyMvc.Core.RouteConfig
{
public override void RegisterRoutes(EasyMvc.Core.Routes.AreaRoute routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public override void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

FilterConfig.cs

public class FilterConfig : EasyMvc.Core.FilterConfig { }

四:模块配置

主项目和各个模块都可配置Module.config

<?xml version="1.0" encoding="utf-8"?>
<Modules>
<IsDebug>false</IsDebug>
<Module>
<Provider>ModuleOne</Provider>
<AreaType>ModuleOne.AreaConfig</AreaType>
<RouteType>ModuleOne.RouteConfig</RouteType>
<FilterType />
<Name>ModuleOne</Name>
<Path>A</Path>
<NameSpaces>
<string>ModuleOne.Controllers</string>
</NameSpaces>
</Module>
<Module>
<Provider>ModuleTwo</Provider>
<AreaType>ModuleTwo.AreaConfig</AreaType>
<RouteType>ModuleTwo.RouteConfig</RouteType>
<FilterType />
<Name>ModuleTwo</Name>
<Path>B</Path>
<NameSpaces>
<string>ModuleTwo.Controllers</string>
</NameSpaces>
</Module>
<Module>
<Provider>MvcApplication1</Provider>
<AreaType>MvcApplication1.AreaConfig</AreaType>
<RouteType>MvcApplication1.RouteConfig</RouteType>
<FilterType />
<Name>Test</Name>
<Path>C</Path>
<NameSpaces>
<string>MvcApplication1.Controllers</string>
</NameSpaces>
</Module>
</Modules>

EasyMvc.Core AreaApplication会根据配置动态初始化执行区域方法以及各个区域的路由注册等工作。

另外AreaViewEngines会根据配置动态设置视图查找路径。

最后效果:

更多东西请查看源码,源码下面提供下载。

原文地址:http://www.cnblogs.com/deeround/p/6706683.html

源码地址:http://files.cnblogs.com/files/deeround/EasyMvc.rar

MVC模块化开发方案的更多相关文章

  1. net Mvc模块化开发

    Asp.net Mvc模块化开发之“部分版本部分模块更新(上线)” 项目开发从来就不是一个简单的问题.更难的问题是维护其他人开发的项目,并且要修改bug.如果原系统有重大问题还需要重构. 怎么重构系统 ...

  2. Asp.net Mvc模块化开发之分区扩展框架

    对于一个企业级项目开发,模块化是非常重要的. 默认Mvc框架的AreaRegistration对模块化开发真的支持很好吗?真的有很多复杂系统在使用默认的分区开发的吗?我相信大部分asp.net的技术团 ...

  3. Asp.net Mvc模块化开发系列(目录)

    模块化开发是非常重要的,模块化开发是个系统性问题,为此我觉得有必须要写一个系列的文章才能基本说的清楚 那又为什么要写一个目录呢? 其一.是对我昨天承诺写一个系列新的文章的回应 其二.是先写出一个大纲, ...

  4. Mvc 模块化开发

    在Mvc中,标准的模块化开发方式是使用Areas,每一个Area都可以注册自己的路由,使用自己的控件器与视图.但是在具体使用上它有如下两个限制 1.必须把视图文件放到主项目的Areas文件夹下才能生效 ...

  5. 全面解析ASP.NET MVC模块化架构方案

    什么叫架构?揭开架构神秘的面纱,无非就是:分层+模块化.任意复杂的架构,你也会发现架构师也就做了这两件事. 本文将会全面的介绍我们团队在模块化设计方面取得的经验.之所以加了“全面”二字,是因为本文的内 ...

  6. ASP.NET MVC模块化开发——动态挂载外部项目

    最近在开发一个MVC框架,开发过程中考虑到以后开发依托于框架的项目,为了框架的维护更新升级,代码肯定要和具体的业务工程分割开来,所以需要解决业务工程挂载在框架工程的问题,MVC与传统的ASP.NET不 ...

  7. Asp.net Mvc模块化开发之“开启模块开发、调试的简单愉快之旅”

    整个世界林林种种,把所有的事情都划分为对立的两个面. 每个人都渴望的财富划分为富有和贫穷,身高被划分为高和矮,身材被划分为胖和瘦,等等. 我们总是感叹,有钱人的生活我不懂;有钱人又何尝能懂我们每天起早 ...

  8. Asp.net Mvc模块化开发之“部分版本部分模块更新(上线)”

    项目开发从来就不是一个简单的问题.更难的问题是维护其他人开发的项目,并且要修改bug.如果原系统有重大问题还需要重构. 怎么重构系统不是本文探讨的问题,但是重构后如何上线部署和本文关系密切.这个大家可 ...

  9. MVC模块化架构

    全面解析ASP.NET MVC模块化架构方案 什么叫架构?揭开架构神秘的面纱,无非就是:分层+模块化.任意复杂的架构,你也会发现架构师也就做了这两件事. 本文将会全面的介绍我们团队在模块化设计方面取得 ...

随机推荐

  1. Protobuf一例

    Developer Guide  |  Protocol Buffers  |  Google Developershttps://developers.google.com/protocol-buf ...

  2. nginx的安装应用

    Nginx的安装 # yum install gcc pcre-devel zlib-devel –y #./configure –prefix=/usr/local/nginx #make & ...

  3. 微信小程序的界面下拉刷新

    小程序的下拉刷新的值设置:需要设置app.json的window中 "navigationBarTextStyle":true  

  4. fatal error: matio.h: No such file or directory

    http://blog.csdn.net/zziahgf/article/details/72900948 $ sudo apt-get install libmatio-dev 或源码安装: # 下 ...

  5. 【题解】N皇后

    题目描述 相信大家都听过经典的“八皇后”问题吧?这个游戏要求在一个8×8的棋盘上放置8个皇后,使8个皇后互相不攻击(攻击的含义是有两个皇后在同一行或同一列或同一对角线上). 桐桐对这个游戏很感兴趣,也 ...

  6. 使用JQuery进行DOM操作

    获得内容 - text().html() 以及 val() 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元 ...

  7. 聪聪和可可 HYSBZ - 1415(概率 + spfa + 记忆化dp)

    Input 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号. 接下来E行,每 ...

  8. HNOI2017大佬

    贼难的一道题 虽然算法都不难,但组合起来就是想不到 首先,最简单的一步,对所有大佬,嘲讽你减的自信值和你做水题回复自信值都是不变的,写个\(dp\),设\(dp[i][j]\)表示第\(i\)天自信值 ...

  9. [HNOI2005]狡猾的商人 ,神奇做法——贪心

    洛谷P2294 [HNOI2005]狡猾的商人 ,神奇做法--贪心 看到大牛都是写的差分约束或带权并查集,本蒟蒻都不太会(还是用差分约束过了的QAQ),但是想出一种贪心的策略,运用神奇的优先队列实现. ...

  10. python 和 scikit-learn 实现垃圾邮件过滤

    文本挖掘(Text Mining,从文字中获取信息)是一个比较宽泛的概念,这一技术在如今每天都有海量文本数据生成的时代越来越受到关注.目前,在机器学习模型的帮助下,包括情绪分析,文件分类,话题分类,文 ...