值注入器问题
使用 AutoMapper 后,我在这个网站上发现了 ValueInjecter。我正在尝试,但我陷入了一个可能非常简单的场景。
但在我深入研究代码示例之前,有谁知道 ValueInjecter 是否可以在中等信任的 Web 环境中工作? (像 Godaddy 一样?)
好的,进入代码!我有以下模型:
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.名字 当从 DTO 转到模型时,我需要 FirstName ->姓名.第一名.根据我的理解,这不是一个简单的扁平化/取消扁平化,因为这些词也会颠倒过来,即:FirstName <-->姓名.第一名.因此名字和姓氏可以使用相同的规则,但是 MiddleInitial 呢?型号.Name.MiddleInitial -> DTO.MiddleInitial。
我看到有一些插件,但它们似乎都没有达到我想要的效果。还有其他人遇到过这种情况吗?
After working with AutoMapper I came across ValueInjecter on this site. I am trying it out but I am stuck on what is probably a very simple scenario.
But before I dig into the code sample, does anyone know if ValueInjecter works in a Medium-Trust web environment? (Like Godaddy?)
Ok, onto the code! I have the following models:
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; }
}
that I want to map to the following 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); }
}
}
So when I want to Map from Model to DTO I need a Model.Name.First -> DTO.FirstName
and when going from DTO to Model I need FirstName -> Name.First. From my understanding this is not a simple Flatten/UnFlatten, because the words also reverse themselves, ie: FirstName <--> Name.First. So First and Last names could use the same kind of rule, but what about MiddleInitial? Model.Name.MiddleInitial -> DTO.MiddleInitial.
I see there are some plugins, but none of them seem to do what I would want. Has anyone else come across this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本思想是我将
Name
与FirstName
相匹配,我将其作为枢轴点,并且在通常将值设置为 1 (FirstName) 的方法中属性 我将其设置为 3 个属性 - 这是用于ToNameComp
中的
FromNameComp
我匹配相同的属性,但我从 3 中获取值并创建一个并设置它它不使用reflection.emit,所以它应该可以工作
the basic idea is that I match the
Name
with theFirstName
, I take this as a pivot point, and in the method that usually sets the value to just one (FirstName) property I set it to 3 properties - that's for theFromNameComp
in the
ToNameComp
i match the same properties but I take the value from 3 and create one and set itit doesn't use reflection.emit so it should work