IOC框架-autoFac、Spring.NET

autoFac:最流行的依赖注入和IOC框架,轻量且高性能,对项目代码几乎无任何侵入性

Spring.NET:依赖注入、面向方面编程(AOP)、数据访问抽象、以及ASP.NET集成。


autoFac:

   1     /// <summary>
        /// 属性注入
        /// </summary>
        public IPeople people { get; set; }

       public ActionResult Index()
        {
            var data = people.GetAll();
            return View();
        }

 2 

   public class AutofacConfig

    {

        public static void Register()
         {
            //实例化控制器
            var builder = new ContainerBuilder();
           //注册类型(映射实现类)
            builder.RegisterType<People>().As<IPeople>();
            //注册到控制器
            builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            var container = builder.Build();
            //下面就是使用MVC的扩展 更改了MVC中的注入方式.
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
         }

        }

3  global文件中加入:

    AutofacConfig.Register();


Spring.NET:

 1 

        public IHello Hello333 { get; set; }
        public string name2 { get; set; }
        public ActionResult Index()
        {
            ViewBag.Message = Hello333.SayHelloWorld()+ name2;
            return View();
        }

2

    Global 继承修改SpringMvcApplication

    public class MvcApplication : SpringMvcApplication
      {
       }

3 webConfig配置

  <configuration>
  <configSections>

<!--必须放在configSections的第一个位置-->

    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc5" />
    </sectionGroup>

  </configSections>

  <spring>
    <context>
      <resource uri="/Configs/Spring.xml" />
    </context>
  </spring>


  Spring.xml:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
 <!--这里放容器里面的所有节点-->
  <description>An  example that demonstrates simple IoC features.</description>
  <!--//type属性值必须是包含程序集名称在内的类型全名    "命名空间,程序集"-->
  <object id="Hello" type="SpringDemo.demo.Hello, SpringDemo"/>
   <!--对象默认配置是单例的,Controller不是单例的,所以这里把singleton设置为false -->
  <object type="SpringDemo.Controllers.HomeController, SpringDemo" singleton="false">
      <property name="Hello333" ref="Hello"/>
      <property name="name2" value="张三"/>
  </object>

</objects>



  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值