首页 > 代码库 > EasyMvc--让MVC区域开发更Easy(提供源码下载)

EasyMvc--让MVC区域开发更Easy(提供源码下载)

核心:

主要利用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://files.cnblogs.com/files/deeround/EasyMvc.rar

 

EasyMvc--让MVC区域开发更Easy(提供源码下载)