c# - 使用 StructureMap 将接口(interface)注入(inject)重载类构造函数

标签 c# asp.net-mvc-4 inversion-of-control ioc-container structuremap

我在 ASP .Net MVC 4 中使用 StructureMap 并且在我的项目基础结构中有以下接口(interface)和实现类:

public interface IUnitOfWork
{
    void Commit();
}

public class UnitOfWork : IUnitOfWork
{
    public void Commit()
    {
        // Track all changes in database
    }
}

我的 HomeController 使用 IUnitOfWork 接口(interface)作为构造函数参数:

public class HomeController
{
    IUnitOfwork unitOfWork;

    public HomeController(IUnitOfwork unitOfWork)
    {
        this.unitOfWork = unitOfWork;
    }
 }

如何将 IUniOfWork 接口(interface)作为参数注入(inject)重载的 HomeController 类?

最佳答案

您必须执行以下步骤:

  1. 删除和卸载 StructureMap 的所有配置和包。
  2. 安装 structurmap.mvc4 from nuget
  3. 将您的 IoC 容器配置为:

    x.For(IUnitOfWork).Use(UnitOfWork)

关于c# - 使用 StructureMap 将接口(interface)注入(inject)重载类构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12938315/

相关文章:

asp.net-mvc-4 - 提交表单时 MVC : System. NullReferenceException 模型

c# - 使用 Autofac 注入(inject)接口(interface)的特定实例

javascript - mvc回发后AngularJS空输入模型

c# - 使用简单注入(inject)器在 C# 中实现域事件处理程序模式

java - 如何使用 @Assisted 将 bean "with additional parameters"从 Guice 迁移到 Spring?

c# - 在 C# 中删除代码重复

c# - 为什么 IPersistenceContext.GetEntry 在 IPersistenceContext.UnproxyAndReassociate 之后返回 null?

c# - ReadOuterXml 正在读取大型 (1 GB) XML 文件的一部分时抛出 OutOfMemoryException

c# - 如何找到解决方案中最长的方法/类?

c# - Visual Studios 使用 ASP 项目启动错误的 URL?