建立 空的 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. hihocoder1241 Best Route in a Grid

    题目链接:hihocoder 1241 题意: n*n的格阵,每个方格内有一个数字.蚂蚁从左上角走到右下角,数字是零的方格不能走,只能向右向下走.蚂蚁走的路径上全部方格的的乘积为s,要使s低位0的个数 ...

  2. 【JavaEE企业应用实战学习记录】getConnListener

    Listener:当Web应用在Web容器中运行时,Web应用内部会不断地发生各种事件,如Web应用被启动.Web应用被停止,用户Session开始,用户session结束.用户请求到达等,这些对We ...

  3. 通过Gearman实现MySQL到Redis的数据同步

    对于变化频率非常快的数据来说,如果还选择传统的静态缓存方式(Memocached.File System等)展示数据,可能在缓存的存取上会有很大的开销,并不能很好的满足需要,而Redis这样基于内存的 ...

  4. 十天冲刺---Day9

    站立式会议 站立式会议内容总结: 燃尽图 照片 队员们都回来了,写完之后继续对alpha版本进行迭代. 希望演示的时候能拿得出来.

  5. 【Quartz】Quartz的搭建、应用(单独使用Quartz)

    Quartz在Java构建的系统中,是十分常用的定时任务框架. 本文,记录.介绍Quartz的简单入门的单独搭建(此文入门学习Quartz为主,并非基于Spring托管形式). > 参考的优秀资 ...

  6. iOS不得姐项目--TabBar的重复点击实现当前模块刷新;状态栏点击实现当前模块回滚到最顶部

    一.实现功能:重复点击tabBar,刷新当前TableView,其余不受影响 <1>实现思路: 错误的方法: TabBar成为自己的代理,监听自己的点击--这种方法是不可取的,如果外面设置 ...

  7. native2ascii 国际资源文件编码

    将common_zh_CN.properties 转换为 本地文件 common_zh_CN_src.properties native2ascii -reverse common_zh_CN.pro ...

  8. APP的缓存文件到底应该存在哪?看完这篇文章你应该就自己清楚了

    APP的缓存文件到底应该存在哪?看完这篇文章你应该就自己清楚了 彻底理解android中的内部存储与外部存储 存储在内部还是外部 所有的Android设备均有两个文件存储区域:"intern ...

  9. UNIX命令,统计当前目录(含子目录)下所有后缀为.log的文件中ERROR出现的行数

    shell程序如下所示: # cat xarg.txt #! /usr/bin/ksh for logfile in `find . -name "*.log*"` do echo ...

  10. Maven报错: Missing artifact jdk.tools:jdk.tools:jar:1.7

    pom.xml 有小红叉,报错: Missing artifact jdk.tools:jdk.tools:jar:1.7 解决方法:(缺少一个jar包) http://blog.csdn.net/u ...