zoukankan      html  css  js  c++  java
  • SQL Table 自动生成Net底层-控制器Autofac注入

    自动生成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

            /// <summary>
            /// 根据名称获取dll程序集
            /// </summary>
            /// <param name="dllName"></param>
            /// <returns></returns>
            public static List<Assembly> GetAllAssembly(string dllName = "*.Area.dll")
            {            
                List<string> pluginpath = FindPlugin(dllName);
                List<Assembly> list = new List<Assembly>();
                Assembly asm = null;
                foreach (string filename in pluginpath)
                {
                    try
                    {
                        string asmname = Path.GetFileNameWithoutExtension(filename);
                        if (asmname != string.Empty)
                        {
                            asm = Assembly.LoadFrom(filename);
                            list.Add(asm);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                    }
                }
                return list;
            }
    
    
            //查找所有插件的路径
            static List<string> FindPlugin(string dllName)
            {
                List<string> pluginpath = new List<string>();
                try
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory;
                    string dir = Path.Combine(path, "bin");
                    string[] dllList = Directory.GetFiles(dir, dllName);
                    if (dllList != null && dllList.Length > 0)
                    {
                        foreach (var item in dllList)
                        {
                            pluginpath.Add(Path.Combine(dir, item.Substring(dir.Length + 1)));
                        }
                    }
                }
                catch (Exception ex)
                {
    
                }
                return pluginpath;
            }
    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
  • 相关阅读:
    TestLink学习六:TestLink1.9.13工作使用小结
    TestLink学习五:TestLink1.9.13和JIRA6.3.6的集成
    TestLink学习四:TestLink1.9.13使用说明
    TestLink学习三:发送邮件的两种配置方法
    TestLink学习二:Windows搭建TestLink环境
    TestLink学习一:Windows搭建Apache+MySQL+PHP环境
    Python:Ubuntu上使用pip安装opencv-python出现错误
    Python:Ubuntu上出现错误 Could not load dynamic library 'libnvinfer.so.6' / 'libnvinfer_plugin.so.6'
    mybatis-generator二次开发总结
    动态代理
  • 原文地址:https://www.cnblogs.com/plming/p/8046856.html
Copyright © 2011-2022 走看看