gpt4 book ai didi

automapper - ValueInjecter问题

转载 作者:行者123 更新时间:2023-12-02 04:11:03 25 4
gpt4 key购买 nike

使用AutoMapper后,我在此站点上遇到了ValueInjecter。我正在尝试,但是我只能停留在一个非常简单的场景上。

但是在我深入研究代码示例之前,是否有人知道ValueInjecter是否可以在“中等信任”网络环境中工作? (像哥达迪?)

好的,到代码了!我有以下型号:

public class NameComponent 
{
public string First { get; set; }
public string Last { get; set; }
public string MiddleInitial { get; set; }
}
public class Person
{
public NameComponent Name { get; set; }
}

我想映射到以下DTO:
public class PersonDTO : BaseDTO
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { NotifyPropertyChanged(() => FirstName, ref _firstName, value); }
}

private string _middleInitial;
public string MiddleInitial
{
get { return _middleInitial; }
set { NotifyPropertyChanged(() => MiddleInitial, ref _middleInitial, value); }
}

private string _lastName;
public string LastName
{
get { return _lastName; }
set { NotifyPropertyChanged(() => LastName, ref _lastName, value); }
}
}

因此,当我想从模型映射到DTO时,我需要一个Model.Name.First-> DTO.FirstName
从DTO转到模型时,我需要FirstName-> Name.First。根据我的理解,这不是一个简单的Flatten / UnFlatten,因为这些单词本身也会反转,即:FirstName <-> Name.First。因此,名字和姓氏可以使用相同的规则,但是MiddleInitial呢? Model.Name.MiddleInitial-> DTO.MiddleInitial。

我看到有一些插件,但是它们似乎都没有我想要的。还有其他人遇到这种情况吗?

最佳答案

基本思想是我将NameFirstName匹配,以此为枢轴点,在通常将值设置为一个(FirstName)属性的方法中,将其设置为3个属性-这是FromNameComp
ToNameComp中,我匹配相同的属性,但是我从3中取值并创建一个并进行设置

    public class SimpleTest
{
[Test]
public void Testit()
{
var p = new Person { Name = new NameComponent { First = "first", Last = "last", MiddleInitial = "midd" } };
var dto = new PersonDTO();
dto.InjectFrom<FromNameComp>(p);

Assert.AreEqual(p.Name.First, dto.FirstName);
Assert.AreEqual(p.Name.Last, dto.LastName);
Assert.AreEqual(p.Name.MiddleInitial, dto.MiddleInitial);

var pp = new Person();
pp.InjectFrom<ToNameComponent>(dto);

Assert.AreEqual(dto.LastName, pp.Name.Last);
Assert.AreEqual(dto.FirstName, pp.Name.First);
Assert.AreEqual(dto.MiddleInitial, pp.Name.MiddleInitial);

}

public class FromNameComp : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name == "Name" && c.SourceProp.Type == typeof(NameComponent)
&& c.TargetProp.Name == "FirstName"
&& c.SourceProp.Value != null;
}

protected override object SetValue(ConventionInfo c)
{
dynamic d = c.Target.Value;
var nc = (NameComponent)c.SourceProp.Value;
//d.FirstName = nc.First; return nc.First does this
d.LastName = nc.Last;
d.MiddleInitial = nc.MiddleInitial;
return nc.First;
}
}

public class ToNameComponent : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.TargetProp.Name == "Name" && c.TargetProp.Type == typeof(NameComponent)
&& c.SourceProp.Name == "FirstName";
}

protected override object SetValue(ConventionInfo c)
{
dynamic d = c.Source.Value;
var nc = new NameComponent { First = d.FirstName, Last = d.LastName, MiddleInitial = d.MiddleInitial };
return nc;
}
}

public class NameComponent
{
public string First { get; set; }
public string Last { get; set; }
public string MiddleInitial { get; set; }
}

public class Person
{
public NameComponent Name { get; set; }
}

public class PersonDTO
{
public string FirstName { get; set; }
public string MiddleInitial { get; set; }
public string LastName { get; set; }
}
}

但是在深入研究代码示例之前,
有谁知道ValueInjecter
在中等信任的网络中工作
环境? (像哥达迪?)

它不使用reflection.emit,所以它应该可以工作

关于automapper - ValueInjecter问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5176629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com