MVC Autofac构造函数注入
建立 空的 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构造函数注入的更多相关文章
- ASP.NET MVC Autofac自动注入
依赖注入容器有很多插件,我用过Unity和Autofac,这两个插件给我最明显的感觉就是Autofac很快,非常的快,毕竟是第三方开发的,而Unity相对而言性能比较稳定 下面附上Autofac自动注 ...
- Mvc Autofac构造器注入
新建MVC项目,添加程序集引用 定义接口ILog public interface ILog { string Save(string message); } 类TxtLog实现接口ILog publ ...
- ASP.NET MVC Autofac依赖注入的一点小心得(包含特性注入)
前言 IOC的重要性 大家都清楚..便利也都知道..新的ASP.NET Core也大量使用了这种手法.. 一直憋着没写ASP.NET Core的文章..还是怕误导大家.. 今天这篇也不是讲Core的 ...
- MVC Autofac依赖注入
通过Dll实现全部类的属性注入,该演示实例主要通过多层架构中单一的对象方式来演示,没有采取接口的方式, 新建AutoFacHelper类,如下代码: public class AutoFacHelpe ...
- MVC autofac 属性注入
Global文件 public class MvcApplication : System.Web.HttpApplication { private static IContainer Contai ...
- Autofac 的构造函数注入方式
介绍 该篇文章通过一个简单的 ASP.NET MVC 项目进行介绍如何使用 autofac 及 autofac 的 MVC 模块进行依赖注入.注入方式通过构造函数. 在编写 aufofac 的依赖注入 ...
- ASP.NET MVC IOC依赖注入之Autofac系列(二)- WebForm当中应用
上一章主要介绍了Autofac在MVC当中的具体应用,本章将继续简单的介绍下Autofac在普通的WebForm当中的使用. PS:目前本人还不知道WebForm页面的构造函数要如何注入,以下在Web ...
- ASP.NET MVC IOC依赖注入之Autofac系列(一)- MVC当中应用
话不多说,直入主题看我们的解决方案结构: 分别对上面的工程进行简单的说明: 1.TianYa.DotNetShare.Model:为demo的实体层 2.TianYa.DotNetShare.Repo ...
- Autofac Getting Started(默认的构造函数注入)
https://autofaccn.readthedocs.io/en/latest/getting-started/index.html The basic pattern for integrat ...
随机推荐
- Python 一些总结和比较
数据类型
- android开发------初识Activity
之前我们简单说过,Activity实际上是一个窗体,用来存放我们的程序外观. 我们先来创建一个空的Activity,不加载任何layout.要做的是,定义自己的类,继承android的Activity ...
- 理解 Paxos
Paxos是前段时间刚获得图灵奖的大神Leslie Lamport所提出的,是用来解决分布式系统中的一致性问题的算法.该算法对于分布式系统的重要性,在这里不再赘言.了解过Paxos的朋友应该都知道,要 ...
- Mustache 使用总结
前言: 在分析 jeesite 项目的时候,看到了 Mustache,于是查了下 正文: 1.Mustache 概述 Mustache 是基于 JavaScript 实现的模板引擎,即用来 渲染前台页 ...
- nutch-default.xml文件
Nutch中的所有配置文件都放置在总目录下的conf子文件夹中,最基本的配置文件是conf/nutch-default.xml.这个文件中定义了 Nutch的所有必要设置以及一些默认值,它是不可以被修 ...
- Android Loader使用详解
1.CursorLoader使用Demo public class MainActivity extends Activity implements LoaderManager.LoaderCall ...
- .VDI manual Technical Logistics - Volume 2: Industrial Trucks
VDI manual Technical Logistics - Volume 2: Industrial Trucks Name Publication date: State VDI 2196 B ...
- SPAN的高度问题
FIRST OF ALL,最容易令人忽略而导致头疼的因素.web页面文档类型: 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...
- 【POJ 2406】Power Strings(KMP循环节)
终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; i ...
- iOS中如何选择delegate、通知、KVO(以及三者的区别)
转载自:http://blog.csdn.net/dqjyong/article/details/7685933 在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,contr ...