码迷,mamicode.com
首页 > 数据库 > 详细

SQL Table 自动生成Net底层-控制器Autofac注入

时间:2017-12-16 17:10:17      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:tab   register   tostring   exe   new   系统   技术   and   data   

自动生成BaseController注入业务接口

技术分享图片
        public static string DataTableToAutoface(DataTable dt,string nameSpace)
        {
            StringBuilder sb = new StringBuilder();

            StringBuilder sbContent = new StringBuilder();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                sbContent.AppendFormat(@"
        public I{0} I{0}
        {{
            get {{ return DependencyResolver.Current.GetService<I{0}>() as I{0}; }}
        }}", dt.Rows[i]["name"] + "Service");
            }

            sb.AppendFormat(@"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Autofac;
using {0}.Mapping;
using {0}.IService;

namespace {0}.UIBase.Controllers
{{
    /// <summary>
    /// 系统控制器层注入入口(统一生成请不要修改文件)
    /// </summary>
    public partial class BaseController : Controller
    {{
        {1}
    }}
}}", nameSpace, sbContent);
            return sb.ToString();

        }
View Code

Global.asax

技术分享图片
public static 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 }
            );
        }

        protected void Application_Start()
        {
       
            var builder = new ContainerBuilder();
            builder.RegisterModule(new ConfigurationSettingsReader("命名"));
            Assembly[] asm = PluginHelper.GetAllAssembly().ToArray();

            //Assembly.LoadFrom(Path.GetFileNameWithoutExtension("SharpSvn.dll"));
            builder.RegisterAssemblyTypes(asm);
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();
            builder.RegisterFilterProvider();//注册Filter        
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }
View Code

PluginHelper

技术分享图片View Code

配置文件

技术分享图片
<configSections>
    <section name="module" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
  </configSections>
  <lq-module>
    <modules>
      <module type="命名空间.Service,命名空间"></module>
      <module type="命名空间.Repository,命名空间"></module>
    </modules>
  </lq-module>
</configuration>

web.config
  <命名>
    <files>
      <file name="Config/Module.config" section="module" />
    </files>
  </命名>
View Code

 

SQL Table 自动生成Net底层-控制器Autofac注入

标签:tab   register   tostring   exe   new   系统   技术   and   data   

原文地址:http://www.cnblogs.com/plming/p/8046856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!