建立 空的 MVC4项目

首先引用 NuGet 里 autofac 和 autofac .integration. mvc

然后 建立Model

public class Person

{

public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

public string Address { get; set; }

}

public interface IPersonRepository

{

IEnumerable GetAll();

Person Get(int id);

Person Add(Person item);

bool Update(Person item);

bool Delete(int id);

}

public class PersonRepository : IPersonRepository

{

List person = new List();

public PersonRepository()

{

Add(new Person { Id = 1, Name = "joye.net1", Age = 18, Address = "中国上海" });

Add(new Person { Id = 2, Name = "joye.net2", Age = 18, Address = "中国上海" });

Add(new Person { Id = 3, Name = "joye.net3", Age = 18, Address = "中国上海" });

}

public IEnumerable GetAll()

{

return person;

}

public Person Get(int id)

{

return person.Find(p => p.Id == id);

}

public Person Add(Person item)

{

if (item == null) { throw new ArgumentNullException("item"); }

person.Add(item);

return item;

}

public bool Update(Person item)

{

if (item == null) { throw new ArgumentNullException("item"); }

int index = person.FindIndex(p => p.Id == item.Id);

if (index == -1)

{

return false;

}

person.RemoveAt(index);

person.Add(item); return true;

}

public bool Delete(int id)

{

person.RemoveAll(p => p.Id == id);

return true;

}

}

再新建 HomeController 与相应的 Index.cshtml 在HomeController 声明 属性及构造函数

public class HomeController : Controller

{

public IPersonRepository _personRepository { get; set; }

public HomeController(IPersonRepository personRepository)

{

_personRepository = personRepository;

}

public ActionResult Index()

{

var item = _personRepository.Get(1);

return View();

}

}

最后在global.asax.cs 的Application_Start() 里实现注入

using Autofac;
using Autofac.Integration.Mvc;

public class MvcApplication : System.Web.HttpApplication

{

protected void Application_Start()

{

var builder = new ContainerBuilder();

builder.RegisterType<PersonRepository>().As<IPersonRepository>(); //注册接口服务

builder.RegisterControllers(Assembly.GetExecutingAssembly());//构造函数注入

//builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();//属性注入

var container = builder.Build();//构建

System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));//开始注入

WebApiConfig.Register(GlobalConfiguration.Configuration);

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

RouteConfig.RegisterRoutes(RouteTable.Routes);

BundleConfig.RegisterBundles(BundleTable.Bundles);

}

}

MVC Autofac构造函数注入的更多相关文章

  1. ASP.NET MVC Autofac自动注入

    依赖注入容器有很多插件,我用过Unity和Autofac,这两个插件给我最明显的感觉就是Autofac很快,非常的快,毕竟是第三方开发的,而Unity相对而言性能比较稳定 下面附上Autofac自动注 ...

  2. Mvc Autofac构造器注入

    新建MVC项目,添加程序集引用 定义接口ILog public interface ILog { string Save(string message); } 类TxtLog实现接口ILog publ ...

  3. ASP.NET MVC Autofac依赖注入的一点小心得(包含特性注入)

    前言 IOC的重要性 大家都清楚..便利也都知道..新的ASP.NET Core也大量使用了这种手法.. 一直憋着没写ASP.NET Core的文章..还是怕误导大家.. 今天这篇也不是讲Core的 ...

  4. MVC Autofac依赖注入

    通过Dll实现全部类的属性注入,该演示实例主要通过多层架构中单一的对象方式来演示,没有采取接口的方式, 新建AutoFacHelper类,如下代码: public class AutoFacHelpe ...

  5. MVC autofac 属性注入

    Global文件 public class MvcApplication : System.Web.HttpApplication { private static IContainer Contai ...

  6. Autofac 的构造函数注入方式

    介绍 该篇文章通过一个简单的 ASP.NET MVC 项目进行介绍如何使用 autofac 及 autofac 的 MVC 模块进行依赖注入.注入方式通过构造函数. 在编写 aufofac 的依赖注入 ...

  7. ASP.NET MVC IOC依赖注入之Autofac系列(二)- WebForm当中应用

    上一章主要介绍了Autofac在MVC当中的具体应用,本章将继续简单的介绍下Autofac在普通的WebForm当中的使用. PS:目前本人还不知道WebForm页面的构造函数要如何注入,以下在Web ...

  8. ASP.NET MVC IOC依赖注入之Autofac系列(一)- MVC当中应用

    话不多说,直入主题看我们的解决方案结构: 分别对上面的工程进行简单的说明: 1.TianYa.DotNetShare.Model:为demo的实体层 2.TianYa.DotNetShare.Repo ...

  9. Autofac Getting Started(默认的构造函数注入)

    https://autofaccn.readthedocs.io/en/latest/getting-started/index.html The basic pattern for integrat ...

随机推荐

  1. Code.R团队展示

    团队成员: 031302620马凛凛(队长) 031302619吕昆明 031302319汪毓顺 031302404陈俊达 团队名称: Code.R 团队项目: 基于web的教师报课系统 团队成员风采 ...

  2. 天气预报API获取

    1.citycode: http://mobile.weather.com.cn/js/citylist.xml http://files.cnblogs.com/files/ys-wuhan/cit ...

  3. Extjs 使用图标字体来美化按钮)

    1. 使用Font Awesome,下载地址http://www.bootcss.com/p/font-awesome/#icons-new 2. 把font和css目录放到 Ext的app目录下面 ...

  4. 初步认识ajax(个人整理)

    通过使用ajax可以实现页面的部分动态化 ajax可以发送一个请求去服务端,而服务端则发送回一小段数据给客户端,这样就可以避免加载整个页面,因为很多时候页面只需要刷新某一部分的数据,而其他大部分体就不 ...

  5. [转]xml文件中的转义字符

    原文地址:http://www.cnblogs.com/zhxhdean/archive/2012/02/08/2342498.html 如果在XML文档中使用类似"<" 的 ...

  6. 远程登录服务器执行cmd的Python脚本

    import paramiko,os,sys ip = raw_input("input ip address :>>>") password = raw_inp ...

  7. org.apache.commons.lang3.ArrayUtils 学习笔记

    package com.nihaorz.model; /** * @作者 王睿 * @时间 2016-5-17 上午10:05:17 * */ public class Person { privat ...

  8. ECIF OCRM ACRM关系

    ECIF  :Enterprise Customer Information Facility 企业客户信息工厂: CRM:Customer Relationship Management 客户关系管 ...

  9. win10前面板耳机没声音

    首先去装Relteck的驱动,windows64位的下载地址是: http://12244.wpc.azureedge.net/8012244/drivers/rtdrivers/pc/audio/0 ...

  10. 【bzoj3624】Apio2008—免费道路

    http://www.lydsy.com/JudgeOnline/problem.php?id=3624 (题目链接) 题意 给出一张无向图,其中有0类边和1类边.问能否构成正好有K条0类边的生成树, ...