我如何使用AutoMapper与Ninject.Web.Mvc? [英] How do I use AutoMapper with Ninject.Web.Mvc?

查看:132
本文介绍了我如何使用AutoMapper与Ninject.Web.Mvc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置



我有一个 AutoMapperConfiguration 静态类设置了AutoMapper映射:

 静态类AutoMapperConfiguration()
{
内部静态无效SetupMappings()
{
映射。 CreateMap<长,类别> .ConvertUsing< IdToEntityConverter<分类和GT;>();
}
}



其中, IdToEntityConverter< T> 是一个自定义的 ITypeConverter ,看起来像这样:

 类IdToEntityConverter< T> :ITypeConverter<长,T>其中T:实体
{
私人只读IRepository _repo;

公共IdToEntityConverter(IRepository回购)
{
_repo =回购;
}

公共牛逼转换(ResolutionContext上下文)
{
返回_repo.GetSingle< T>(context.SourceValue);
}
}



IdToEntityConverter 需要一个 IRepository 在其构造,以便击中了数据库的ID转换回实际的实体。注意它并没有一个默认构造函数。



在我的ASP.NET的的Global.asax ,这是我对 OnApplicationStarted() CreateKernel()

 保护覆盖无效OnApplicationStarted()
{
//这是由MVC
AreaRegistration.RegisterAllAreas需要()的东西;
的RegisterRoutes(RouteTable.Routes);

//我们设置的东西
AutoMapperConfiguration.SetupMappings();
}

保护覆盖的iKernel CreateKernel()
{
变种内核=新StandardKernel();
kernel.Bind&所述; IRepository方式>()到< NHibRepository>();

返回内核;
}



所以 OnApplicationCreated()将调用 AutoMapperConfiguration.SetupMappings()来建立映射和 CreateKernel()将绑定<实例code> NHibRepository 到 IRepository 接口。



问题



每当我运行此代码,并试图让AutoMapper一个类别ID转换回类别的实体,我得到一个 AutoMapperMappingException 的说存在于 IdToEntityConverter 没有默认构造函数。



尝试




  1. 添加了一个默认的构造 IdToEntityConverter 。现在,我收到了的NullReferenceException ,这表明,我认为注射是行不通的。


  2. 制造私人 _repo 字段变成公共财产,并添加了 [注入] 属性。仍然得到的NullReferenceException


  3. 新增了 [注入] 的构造属性,它接受一个 IRepository 。仍然得到的NullReferenceException


  4. 思考,也许Ninject无法拦截 OnApplicationStarted AutoMapperConfiguration.SetupMappings()()调用,我把它搬到我知道正确注射,我的控制器之一,像这样的东西

     公共类RepositoryController:控制器
    {
    静态RepositoryController()
    {
    AutoMapperConfiguration.SetupMappings();
    }
    }



    仍然得到的NullReferenceException




问题



我的问题是,我如何才能Ninject注入的 IRepository IdToEntityConverter


< DIV CLASS =h2_lin>解决方案

您必须给AutoMapper访问DI容器。我们使用StructureMap,但我想下面应该与任何DI工作。



我们使用这个(在我们的引导程序的任务之一)...



 私人的IContainer _container; // Structuremap容器

Mapper.Initialize(图= GT;
{
map.ConstructServicesUsing(_container.GetInstance);
map.AddProfile< MyMapperProfile>() ;
}


Setup

I have an AutoMapperConfiguration static class that sets up the AutoMapper mappings:

static class AutoMapperConfiguration()
{
    internal static void SetupMappings()
    {
        Mapper.CreateMap<long, Category>.ConvertUsing<IdToEntityConverter<Category>>();
    }
}

where IdToEntityConverter<T> is a custom ITypeConverter that looks like this:

class IdToEntityConverter<T> : ITypeConverter<long, T> where T : Entity
{
    private readonly IRepository _repo;

    public IdToEntityConverter(IRepository repo)
    {
        _repo = repo;
    }

    public T Convert(ResolutionContext context)
    {
        return _repo.GetSingle<T>(context.SourceValue);
    }
}

IdToEntityConverter takes an IRepository in its constructor in order to convert an ID back to the actual entity by hitting up the database. Notice how it doesn't have a default constructor.

In my ASP.NET's Global.asax, this is what I have for OnApplicationStarted() and CreateKernel():

protected override void OnApplicationStarted()
{
    // stuff that's required by MVC
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);

    // our setup stuff
    AutoMapperConfiguration.SetupMappings();
}

protected override IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    kernel.Bind<IRepository>().To<NHibRepository>();

    return kernel;
}

So OnApplicationCreated() will call AutoMapperConfiguration.SetupMappings() to set up the mappings and CreateKernel() will bind an instance of NHibRepository to the IRepository interface.

Problem

Whenever I run this code and try to get AutoMapper to convert a category ID back to a category entity, I get an AutoMapperMappingException that says no default constructor exists on IdToEntityConverter.

Attempts

  1. Added a default constructor to IdToEntityConverter. Now I get a NullReferenceException, which indicates to me that the injection isn't working.

  2. Made the private _repo field into a public property and added the [Inject] attribute. Still getting NullReferenceException.

  3. Added the [Inject] attribute on the constructor that takes an IRepository. Still getting NullReferenceException.

  4. Thinking that perhaps Ninject can't intercept the AutoMapperConfiguration.SetupMappings() call in OnApplicationStarted(), I moved it to something that I know is injecting correctly, one of my controllers, like so:

    public class RepositoryController : Controller
    {
        static RepositoryController()
        {
            AutoMapperConfiguration.SetupMappings();
        }
    }
    

    Still getting NullReferenceException.

Question

My question is, how do I get Ninject to inject an IRepository into IdToEntityConverter?

解决方案

You have to give AutoMapper access to the DI container. We use StructureMap, but I guess the below should work with any DI.

We use this (in one of our Bootstrapper tasks)...

    private IContainer _container; //Structuremap container

    Mapper.Initialize(map =>
    {
        map.ConstructServicesUsing(_container.GetInstance);
        map.AddProfile<MyMapperProfile>();
    }

这篇关于我如何使用AutoMapper与Ninject.Web.Mvc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆