核心:
主要利用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