简体   繁体   English

如何将值传递给自定义ValueResolver?

[英]How to pass value to a custom ValueResolver?

I'm struggling with the format for passing in a value to a custom ValueResolver. 我正在努力将值传递给自定义ValueResolver。 I am basically trying to convert an Int to and Object based on my ViewModel. 我基本上试图根据我的ViewModel将Int转换为Object。 However, I cannot get it to pass it the value to my ValueResolver 但是,我不能让它将值传递给我的ValueResolver

I have the following Custom ValueResolver: 我有以下Custom ValueResolver:

public class CustomResolver : ValueResolver<CreateContainerViewModel, Container>
{ 
    private int id = 0;

    public CustomResolver(int? sourceId)
    {
        if(sourceId.HasValue)
            id = sourceId.Value;
    }
    protected override Container ResolveCore(CreateContainerViewModel source)
    {
        if (id == 0) return null;
        else
        {
            ISession _session = DependencyResolver.Current.GetService<ISessionFactory>().GetCurrentSession();
            return _session.Get<Container>(id);
        }
    }
}

It works fine when I create the map using a hard coded int like the following code 当我使用硬编码的int创建地图时,它工作正常,如下面的代码

Mapper.CreateMap<CreateContainerViewModel, Container>()
.ForMember(a => a.CurrentContainer, opt => opt.ResolveUsing(src => new CustomResolver(33)));

but it isn't working (throwing mapping exception) when I try and get the value from my viewmodel 但是当我尝试从我的viewmodel获取值时,它不起作用(抛出映射异常)

Mapper.CreateMap<CreateContainerViewModel, Container>()
            .ForMember(dest => dest.CurrentContainer, opt => opt.MapFrom(src => new CustomResolver(src.CurrentContainerId)));

Can anyone point me in the direction of the correct syntax for passing in a value to a custom ValueResolver? 任何人都可以指出我正确的语法方向将值传递给自定义ValueResolver?

Thanks 谢谢

UPDATE: 更新:

basically trying to write something like this: 基本上尝试写这样的东西:

  public class IdToObjectResolver<T> : ValueResolver<int, T> where T : class
{ 
    private ISession _session;
    private int id;
    public IdToObjectResolver(Nullable<int> sourceId)
    {
        _session = DependencyResolver.Current.GetService<ISessionFactory>().GetCurrentSession();
        if(sourceId.HasValue)
            id = sourceId.Value;
    }

    protected override T ResolveCore(int source)
    {
        return _session.Get<T>(source);
    }
}

This code doesnt currently make sense, but just trying to get a way of passing in the int... 这段代码目前没有意义,但只是试图获得一种传递int ...

If I understand correctly, I think that you might be over complicating the ValueResolver a bit. 如果我理解正确,我认为你可能会使ValueResolver过度复杂化。 As you can see, the entire CreateContainerViewModel class is send in the ResolveCore method in the ValueResolver. 如您所见,整个CreateContainerViewModel类在ResolveCore中的ResolveCore方法中发送。 That will be the entire source object with values. 这将是具有值的整个源对象。

Therefore, you should be able to pull the id value from the source object by using this mapping: opt.ResolveUsing(src => new CustomResolver()) . 因此,您应该能够使用以下映射从源对象中提取id值: opt.ResolveUsing(src => new CustomResolver()) (with no constructor arg). (没有构造函数arg)。

Hope this helps. 希望这可以帮助。

EDIT: 编辑:

My Test: 我的测试:

        [TestMethod]
        public void TestMethod1()
        {
            Mapper.CreateMap<CreateContainerViewModel, Container>()
                .ForMember(a => a.CurrentContainer, opt => opt.ResolveUsing<CustomResolver>());


            var source = new CreateContainerViewModel()
            {
                ID = 3
            };

            var destination = new Container();

            Mapper.Map(source, destination);

            Assert.AreEqual(destination.CurrentContainer.ID, 3);

        }

CustomResolver: CustomResolver:

public class CustomResolver : ValueResolver<CreateContainerViewModel, Container>
{
    protected override Container ResolveCore(CreateContainerViewModel source)
    {

            return new Container() { ID = source.ID };  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM