加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – Automapper和Windsor的问题

发布时间:2020-12-16 03:26:07 所属栏目:asp.Net 来源:网络整理
导读:当我尝试使用依赖注入的自定义解析器时,我遇到了Automapper的问题. 我有以下型号: public class User : Entity{ public virtual string Name { get; set; } public virtual Country Country { get; set; }}public class Country : Entity{ public virtual s
当我尝试使用依赖注入的自定义解析器时,我遇到了Automapper的问题.

我有以下型号:

public class User : Entity
{
    public virtual string Name { get; set; }
    public virtual Country Country { get; set; }
}

public class Country : Entity
{
    public virtual string Name { get; set; }
}

和以下视图模型:

public class RegistrationViewModel
{
    [Required]        
    public string Name { get; set; }

    public int CountryId { get; set; }

    public IEnumerable<Country> Countries { get; set; }
}

为了映射我使用以下代码:

Mapper.Map(registrationViewModel,user);

我之前注册以下内容:

Mapper.Reset();
container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.Register(Component.For<ISession>().
                               UsingFactoryMethod(() => NHibernateSessionFactory.RetrieveSession()).
                               LifeStyle.Is(LifestyleType.Transient));
container.Register(Component.For(typeof(LoadingEntityResolver<>)).ImplementedBy(typeof(LoadingEntityResolver<>)).LifeStyle.Transient);
 Mapper.Initialize(x =>
      {
           x.AddProfile<BasicProfile>();
           x.ConstructServicesUsing(container.Resolve);
      });

我的BasicProfile如下:

public class BasicProfile : Profile
{
    public const string VIEW_MODEL = "MyBasicProfile";

    public override string ProfileName
    {
        get { return VIEW_MODEL; }
    }

    protected override void Configure()
    {
        CreateMaps();
    }

    private void CreateMaps()
    {
        CreateMap<RegistrationViewModel,User>()
           .ForMember(dest => dest.Country,_ => _.ResolveUsing<LoadingEntityResolver<Country>>().FromMember(src => src.CountryId))
           );

    }
}

自定义解析程序按以下方式完成:

public class LoadingEntityResolver<TEntity> : ValueResolver<int,TEntity>
    where TEntity: Entity
{
    private readonly ISession _session;

    public LoadingEntityResolver(ISession session)
    {
        _session = session;
    }

   protected override TEntity ResolveCore(int source)
   {
       return _session.Load<TEntity>(source);
   }

}

运行映射代码时,我得到以下异常:

AutoMapper.AutoMapperMappingException : Trying to map ViewModels.RegistrationViewModel to Models.User.
Using mapping configuration for ViewModels.RegistrationViewModel to Models.User
Exception of type ‘AutoMapper.AutoMapperMappingException’ was thrown.
—-> AutoMapper.AutoMapperMappingException : Trying to map ViewModels.RegistrationViewModel to LModels.Country.
Using mapping configuration for ViewModels.RegistrationViewModel to Models.User
Destination property: Country
Exception of type ‘AutoMapper.AutoMapperMappingException’ was thrown.
—-> System.ArgumentException : Type ‘Mapping.LoadingEntityResolver`1[Models.Country]’ does not have a default constructor

我不知道可能出错了什么.它可能是构建解析器的东西.当我尝试以下时没有问题:

var resolver = container.Resolve<LoadingEntityResolver<Country>>();

Assert.IsInstanceOf<LoadingEntityResolver<Country>>(resolver);

我会很乐意为你提供帮助.

最好的祝福
卢卡斯

解决方法

你有一些非常沉重的DI东西:-)我会避免让AutoMapper从数据库或其他任何东西解析实体.使代码难以理解,跟随对象的生命周期可能成为一场噩梦.

无论如何,要解决您的问题,只需从(错误)交换订单:

Mapper.Initialize(x =>
{
    x.AddProfile<BasicProfile>();
    x.ConstructServicesUsing(container.Resolve);
});

纠正):

Mapper.Initialize(x =>
{
    x.ConstructServicesUsing(container.Resolve);
    x.AddProfile<BasicProfile>();
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读