ASP.NET MVC 中的 Microsoft Unity






4.97/5 (16投票s)
在 ASP.NET MVC 中使用 Microsoft Unity 应用依赖注入和控制反转
引言
解耦类的一种方法是使用 Unity。您可以在 此处 了解更多关于 Unity 的信息。在本文中,我将尝试描述 Unity 的用法。为此,您应该按照以下步骤操作。
使用代码
- 在 Visual Studio 中创建一个新项目(推荐使用 Visual Studio 2012)
- 在“文件”菜单中,单击“新建” ->“项目”。
- 在“新建项目”窗口中,选择“Visual C#” ->“Web”,然后选择“ASP.NET MVC 4 Web 应用程序”。图 a-1。
- 命名项目(例如
TestUnity
)。 - 单击“确定”按钮。
- 在“新建 ASP.NET MVC 4 项目”窗口中,选择“Internet 应用程序”和“Razor 视图引擎”(创建单元测试项目是可选的)。图 a-2。
- 单击“确定”按钮。
- 安装 Microsoft Unity
- 在解决方案资源管理器窗口中,右键单击“引用”文件夹。
- 单击“管理 NuGet 程序包”(不要忘记您需要连接到互联网)。
- 在“管理 NuGet 程序包”窗口的左侧面板中,选择“联机”。
- 在“管理 NuGet 程序包”窗口的右侧面板的搜索框中,键入 unity3。
- 在搜索结果列表中,选择“Unity bootstrapper for ASP.NET MVC”,然后单击“安装”(如果需要,单击“接受”按钮)。片刻之后,Unity 将开始安装。图 b-1。
- 单击“关闭”按钮关闭“管理 NuGet 程序包”窗口。
- 在 Models 文件夹中创建一个名为
Person
的类,如下所示- 右键单击 Models 文件夹,然后选择“添加” ->“类”
- 将类命名为 Person.cs
- 将以下代码键入到
Person
类中
- 将
WSCDBProvider
类创建到 Models 文件夹中,如下所示- 右键单击 Models 文件夹,然后选择“添加” ->“类”
- 将类命名为 WSCDBProvider.cs
- 将以下代码键入到 WSCDBProvider.cs 类中
- 在 Models 文件夹中创建一个名为
IPersonRepository
的接口- 右键单击 Models 文件夹,然后选择“添加” ->“类”
- 在项目列表中,选择“接口”并将其命名为 IPersonRepository.cs
- 单击“添加”按钮
- 将以下代码键入到
IPersonRepository
接口中
- 在 Models 文件夹中创建一个名为
PersonRepository
的类,如下所示- 右键单击 Models 文件夹,然后选择“添加” ->“类”
- 将类命名为 PersonRepository.cs
- 将以下代码键入到类中
- 创建 Person 控制器
- 右键单击 Controller 文件夹,然后选择“添加” ->“控制器”
- 在“添加控制器”窗口中,将控制器命名为
PersonController
- 对于“Scaffolding 选项”,选择“具有空读/写操作的 MVC 控制器”
- 单击“添加”按钮。图 g-1。
- 如下所示更改控制器代码
- 现在您应该使用 Unity 将存储库注入到控制器中。
- 从 App_Start 文件夹中打开 UnityConfig.cs 类
- 在
RegisterTypes
方法中键入注入命令,如下所示
- 现在您的项目已准备好创建视图、运行和使用。
图 a-1:在 Microsoft Visual Studio 2012 中创建一个新项目。
图 a-2:选择 Internet 应用程序和 Razor 视图引擎。
在步骤 A 之后,您的项目已经创建完成,可以进行编程。现在您应该如下安装 Unity(步骤 B)。
图 b-1:安装 Unity bootstrapper for ASP.NET MVC。
Unity 将向 App_Start 文件夹中添加两个类 (UnityConfig.cs, UnityMvcActivator.cs)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace TestUnity.Models
{
public class Person
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int Age { get; set; }
[Display(Name = "Contact Information")]
public string ContactInfo { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace TestUnity.Models
{
public class WSCDBProvider : DbContext
{
public DbSet<person> Persons { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestUnity.Models;
namespace TestUnity.Models
{
public interface IPersonRepository : IDisposable
{
IEnumerable<person> GetAll();
void InsertorUpdate(Person contact);
Person Find(int id);
bool Delete(int id);
void Save();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
namespace TestUnity.Models
{
public class PersonRepository : IPersonRepository
{
private WSCDBProvider db = new WSCDBProvider();
public IEnumerable<person> GetAll()
{
return db.Persons.ToList();
}
public Person Find(int id)
{
return db.Persons.Find(id);
}
public bool Delete(int id)
{
try
{
Person person = Find(id);
db.Persons.Remove(person);
Save();
return true;
}
catch (Exception)
{
return false;
}
}
public void InsertorUpdate(Person person)
{
if (person.Id == default(int))
{
// New entity
db.Persons.Add(person);
}
else
{
// Existing entity
db.Entry(person).State = EntityState.Modified;
}
}
public void Save()
{
db.SaveChanges();
}
public void Dispose()
{
db.Dispose();
}
}
}
图 g-1:添加控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestUnity.Models;
namespace TestUnity.Controllers
{
public class PersonController : Controller
{
private readonly IPersonRepository repository;
public PersonController(IPersonRepository repository)
{
this.repository = repository;
}
public ActionResult Index()
{
var persons = repository.GetAll();
return View(persons);
}
public ActionResult Details(int id)
{
var person = repository.Find(id);
return View(person);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Person person)
{
try
{
repository.InsertorUpdate(person);
repository.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Edit(int id)
{
var person = repository.Find(id);
return View(person);
}
[HttpPost]
public ActionResult Edit(int id, Person model)
{
try
{
var person = repository.Find(id);
repository.InsertorUpdate(person);
repository.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Delete(int id)
{
var person = repository.Find(id);
return View(person);
}
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
bool ret = repository.Delete(id);
if(ret)
return RedirectToAction("Index");
return View();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
repository.Dispose();
}
base.Dispose(disposing);
}
}
}
container.RegisterType<IPersonRepository, PersonRepository>();
图 h-1:UnityConfig.cs 类