0

我有一个 mvc 项目。我在成员资格提供程序中使用实体访问层中的 Autofac 和 StructureMap。

我可以在单个 mvc 项目中同时使用 Autofac 和结构映射吗?

谢谢

4

1 回答 1

0

我想您在项目中使用 ASP.NET MVC。然后,您只需要实现 ASP.NET MVC IDependencyResolver 接口,该接口仅包含两个方法,如下所示:

public interface IDependencyResolver
{
    object GetService(Type serviceType);
    IEnumerable<object> GetServices(Type serviceType);
}

您的实现应该在其构造函数中使用两个参数:Autofac 依赖解析器和 StructureMap 依赖解析器。它可能看起来像这样:

public class CombinedDependencyResolver : IDependencyResolver
{
    private readonly AutofacDependencyResolver _autofacDependencyResolver;
    private readonly StructureMapDependencyResolver _structureMapDependencyResolver;

    public CombinedDependencyResolver(AutofacDependencyResolver autofacDependencyResolver, StructureMapDependencyResolver structureMapDependencyResolver)
    {
        _autofacDependencyResolver = autofacDependencyResolver;
        _structureMapDependencyResolver = structureMapDependencyResolver;
    }

    public object GetService(Type serviceType)
    {
        // your logic here
        _structureMapDependencyResolver.GetService(serviceType);
        _autofacDependencyResolver.GetService(serviceType);
        return new object();
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        // your logic here
        return null;
    }
}

然后根据您的逻辑实现 IDependencyResolver 的方法,并从 AutofacDependencyResolver 或 StructureMapDependencyResolver 解决依赖关系。

于 2013-10-24T09:59:56.917 回答