毒逆天 2016-07-21 625浏览量
技能大全:http://www.cnblogs.com/dunitian/p/4822808.html#skill
完整Demo:https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/3.常用技能/8.AutoFac/1.AutoFac
先看效果
IBLL
IBLL
核心代码:
代码附件:
public ActionResult Index() { ITestBLL testBLL = Container.Resolve<ITestBLL>(); ViewBag.Name = testBLL.GetName(); return View(); } ———————————————————————————————— public interface ITestBLL { string GetName(); } ———————————————————————————————— public class TestBLL : ITestBLL { public string GetName() { return "我为NET狂-官方群① 238575862"; } } ———————————————————————————————— /// <summary> /// Autofac IOC类 /// </summary> public class Container { /// <summary> /// IOC 容器 /// </summary> public static IContainer container = null; public static T Resolve<T>() { try { if (container == null) { Initialise(); } } catch (Exception ex) { throw new Exception("IOC实例化出错!" + ex.Message); } return container.Resolve<T>(); } /// <summary> /// 初始化 /// </summary> public static void Initialise() { var builder = new ContainerBuilder(); //格式:builder.RegisterType<xxxx>().As<Ixxxx>().InstancePerLifetimeScope(); builder.RegisterType<TestBLL>().As<ITestBLL>().InstancePerLifetimeScope(); container = builder.Build(); } }
扩展:http://blog.csdn.net/dhx20022889/article/details/9061483
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
云原生时代,是开发者最好的时代