Autofac的高级使用——Autofac.2.6.3.862

3 篇文章 0 订阅
1 篇文章 0 订阅

1. 使用代码方式进行组件注册【依赖服务类和组件类】

/// <summary>
    /// 管理类
    /// </summary>
    public partial class Mgr
    {
        private static IContainer container = null;

        /// <summary>
        /// 自定义容器和组件注册
        /// </summary>
        /// <returns></returns>
        public static IContainer GetContainer()
        {

            if (container == null)
            {
                var builder = new ContainerBuilder();
                //builder.RegisterType<SqlDatabase>().As<IDatabase>();
                builder.RegisterType<SqlDatabase>().Named<IDatabase>("AutofacDemo.Lib.Sql.SqlDatabase");
                builder.RegisterType<OracleDatabase>().Named<IDatabase>("AutofacDemo.Lib.Oracle.OracleDatabase");

                container = builder.Build();

            }
            return container;
        }
    }


 

2. 使用配置文件进行组件注册【不需要依赖】


2.1. 定义配置文件

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
  </configSections>
  <autofac>
    <components>
      <component name="AutofacDemo.Lib.Oracle.OracleDatabase" type="AutofacDemo.Lib.Oracle.OracleDatabase, AutofacDemo.Lib.Oracle" service="AutofacDemo.Lib.IDatabase, AutofacDemo.Lib"/>
      <component name="AutofacDemo.Lib.Sql.SqlDatabase" type="AutofacDemo.Lib.Sql.SqlDatabase, AutofacDemo.Lib.Sql" service="AutofacDemo.Lib.IDatabase, AutofacDemo.Lib"/>
    </components>
  </autofac>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>


2.2. 读取config配置文件进行组件注册

 

        /// <summary>
        /// 根据配置文件里的服务名生成对象
        /// </summary>
        public static void GetFrom_Config()
        {


            var builder = new ContainerBuilder();

            //从.config配置文件中取得相关的组件注册
            builder.RegisterModule(new ConfigurationSettingsReader("autofac")); 

           

            using (var container = builder.Build())
            {
                string vServiceName = "AutofacDemo.Lib.Oracle.OracleDatabase";//服务名
                //vServiceName = "AutofacDemo.Lib.Sql.SqlDatabase";

                //是否存在服务名
                if (container != null)
                {
                    if (container.IsRegisteredWithName(vServiceName, typeof(IDatabase)))
                    {
                        IDatabase db = container.ResolveNamed<IDatabase>(vServiceName);
                        if (db != null)
                        {
                            db.Select("..........");
                        }
                    }
                }
            }
        }


 

2.3. 是否存在服务名,存在,则根据服务名取得对象

                //是否存在服务名
                if (container.IsRegisteredWithName(vServiceName, typeof(IDatabase)))
                {
                    IDatabase db = container.ResolveNamed<IDatabase>(vServiceName);
                    if (db != null)
                    {
                        db.Select("..........");
                    }
                }

 

 

3. Demo下载

 点此下载

 

 

4. 使用代码方式进行组件注册【不需要依赖】【类似反射的全字符串形式】

/// <summary>
        /// 自定义容器和通过反射进行组件注册
        /// </summary>
        /// <returns></returns>
        public static IContainer GetContainer()
        {

            if (container == null)
            {
                var builder = new ContainerBuilder();


                Type objType = Type.GetType("AutofacDemo.Lib.IDatabase, AutofacDemo.Lib");
                Type objTypeA = Type.GetType("AutofacDemo.Lib.Oracle.OracleDatabase, AutofacDemo.Lib.Oracle");
                Type objTypeB = Type.GetType("AutofacDemo.Lib.Sql.SqlDatabase, AutofacDemo.Lib.Sql");


                builder.RegisterType(objTypeA).Named("AutofacDemo.Lib.Oracle.OracleDatabase", objType);
                builder.RegisterType(objTypeB).Named("AutofacDemo.Lib.Sql.SqlDatabase", objType);


                需要知道接口
                //builder.RegisterType(objTypeA).Named<objType>("AutofacDemo.Lib.Sql.SqlDatabase");
                //builder.RegisterType(objTypeB).Named<objType>("AutofacDemo.Lib.Oracle.OracleDatabase");

                container = builder.Build();

            }
            return container;
        }


 

download

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值