Autofac依赖注入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Autofac.Configuration.Core;
 
namespace AutoFacIOC
{
    class Program
    {
        static void Main(string[] args)
        {
            //直接指定实例类型
            var builder = new ContainerBuilder();
            builder.RegisterType();
            builder.RegisterType().As();
            using (var container = builder.Build())
            {
                var manager = container.Resolve();
                manager.Search("SELECT * FORM USER");
            }
 
 
            Type objType = Type.GetType("AutoFacIOC.IRepository, AutoFacIOC");
            Type objTypeSql = Type.GetType("AutoFacIOC.SqlRepository, AutoFacIOC");
            Type objTypeRedis = Type.GetType("AutoFacIOC.RedisRepository, AutoFacIOC");
            //通过配置文件实现对象的创建
            var builder2 = new ContainerBuilder();
            builder2.RegisterType();
 
 
            //builder.RegisterType().As();
 
            builder2.RegisterType(objTypeSql).As(objType);
 
            //builder.RegisterType().Named("AutofacDemo.Lib.Oracle.OracleDatabase");
            //builder2.RegisterModule(new ConfigurationSettingsReader("autofac"));
            using (var container = builder2.Build())
            {
                var manager = container.Resolve();
                manager.Search("SELECT * FORM USER");
            }
 
            //通过配置文件,配合Register方法来创建对象
            var builder3 = new ContainerBuilder();
            builder3.RegisterType();
            //builder3.RegisterModule(new ConfigurationSettingsReader("autofac"));
            //builder3.RegisterType().Named("AutoFacIOC.SqlRepository");
            //builder3.Register(c => new DBBase(c.Resolve()));
            //builder3.RegisterType(objTypeRedis).Named("AutofacDemo.Lib.Sql.SqlDatabase", objType);
 
            //builder3.RegisterType(objType).Named("AutoFacIOC.DBBase", objType);
            builder3.RegisterType(objTypeRedis).As(objType);
            using (var container = builder3.Build())
            {
                var manager = container.Resolve();
                manager.Search("SELECT * FORM USER");
            }
 
            Console.ReadKey();
        }
    }
}
 
posted @ 2022-06-28 10:22  devgis  阅读(200)  评论(0编辑  收藏  举报