建立 空的 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. C# Image Resizer

    This program is used to resize images. using System; using System.Windows.Forms; using System.Drawin ...

  2. Putty SSH简单使用

    本地的puttygen生出的秘钥,公钥传到服务器上连接会报错 Server refused our key. 一般我们建议都在服务器上生成秘钥,把私钥下载下来.加载到putty认证中 01.在服务器上 ...

  3. stack 栈的实现

    今天晚上去「南哪」听了场AI的讲座,除了话筒真心不给力之外,算是对微软这方面的进展有了更多了解,毕竟是半宣传性质的活动吧. 光听这些是没用的,眼下还是打好基础,多尝试学点新技术,拓宽能力和视野比较重要 ...

  4. PHP -- 上传文件接口编写 及 iOS -- 端上传图片AF实现

    PHP 上传文件接口: //保存图片 $json_result ['status'] = 0; $path = 'upfile'; $json_result ['status'] = 0; $json ...

  5. hello Cookie

    Cookie 是什么? Cookie在浏览器中的表现为请求头域和响应头域的字段,也就是伴随着请求和响应的一组键值对的文本.Cookie来源于服务器,第一次请求无Cookie参数,增加Cookie通过服 ...

  6. struts2 spring3.2 hibernate4.1 框架搭建 整合

    ssh是企业开发中常遇到的框架组合,现将框架的搭建过程记录下来,以便以后查看.我的搭建过程是,首先struts,然后spring,最后hibernate.struts2的最新版本为2.3.8,我下载的 ...

  7. PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)

    通过curl_setopt()函数可以方便快捷的抓取网页(采集很方便),curl_setopt 是php的一个扩展库 使用条件:需要在php.ini 中配置开启.(PHP 4 >= 4.0.2) ...

  8. 【POJ 1279】Art Gallery

    http://poj.org/problem?id=1279 裸的半平面交的模板,按极角排序后维护一个双端队列,不要忘了最后要去除冗余,即最后一条边(或者更多的边)一定在双端队列里,但它不一定构成半平 ...

  9. ps制作gif图片

    本文自学内容来自这里 PS版本是CS6: 制作效果 步骤 1.下载素材 2.打开ps,添加素材 文件->打开->选择所有需要的素材全部打开(如图,已将需要的3个素材全部打开) 3.将素材放 ...

  10. Jquery-下拉列表设置默认选择

    $('#select option:eq(2)').attr('selected','selected');