내가 읽은 표준은 Automapper가 맵을 감지하기 위해 Profile 클래스를 상속하는 것입니다. https://automapper.org/
public class GamerVM : Profile
{
public GamerVM()
{
CreateMap<GamerVM, Gamer>();
CreateMap<Gamer, GamerVM>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Tag { get; set; }
}
내 뷰 모델에서이 클래스를 상속하면 Json이 추가 속성과 함께 반환됩니다.
{
"id": 8,
"name": "Ashton Heir",
"tag": "Legend",
"defaultMemberConfig": {
"nameMapper": {
"getMembers": {},
"namedMappers": [
{
"methodCaseSensitive": false
},
{},
{
"prefixes": [
"Get"
],
"postfixes": [],
"destinationPrefixes": [],
"destinationPostfixes": []
}
]
},
"memberMappers": [
{
"nameMapper": {
"getMembers": {},
"namedMappers": [
{
"methodCaseSensitive": false
},
{},
{
"prefixes": [
"Get"
],
"postfixes": [],
"destinationPrefixes": [],
"destinationPostfixes": []
}
]
}
},
{
"sourceMemberNamingConvention": {
"splittingExpression": {
"pattern": "(\\p{Lu}+(?=$|\\p{Lu}[\\p{Ll}0-9])|\\p{Lu}?[\\p{Ll}0-9]+)",
"options": 0
},
"separatorCharacter": ""
},
"destinationMemberNamingConvention": {
"splittingExpression": {
"pattern": "(\\p{Lu}+(?=$|\\p{Lu}[\\p{Ll}0-9])|\\p{Lu}?[\\p{Ll}0-9]+)",
"options": 0
},
"separatorCharacter": ""
}
}
]
}
나는 이것을 올바르게하고 있는가? JSON이 이러한 추가 속성을 무시하는 방법이 있습니까?
모델이 프로필을 상속하면 안됩니다. 프로필을 하위 클래스로 분류하여 모델 대 모델 매핑을 구성합니다.
public class GamerMappingProfile : Profile
{
public GamerMappingProfile()
{
CreateMap<Gamer, GamerVM>();
CreateMap<GamerVM, Gamer>();
}
}
그런 다음 매퍼 인스턴스를 만들 때 프로필을로드합니다.
var config = new MapperConfiguration(cfg => {
cfg.AddProfile<GamerMappingProfile>();
cfg.AddProfile<MyOtherProfile>();
});
var mapper = config.CreateMapper();
이제 모델이 깨끗합니다. 속성 만 포함되며 직렬화에는 추가 사용자 지정 코드가 필요하지 않습니다.
프로필을 자동 검색하려면
http://docs.automapper.org/en/stable/Configuration.html#assembly-scanning-for-auto-configuration
위 링크에서 복사
// Scan for all profiles in an assembly
// ... using instance approach:
var config = new MapperConfiguration(cfg => {
cfg.AddProfiles(myAssembly);
});
// ... or static approach:
Mapper.Initialize(cfg => cfg.AddProfiles(myAssembly));
// Can also use assembly names:
Mapper.Initialize(cfg =>
cfg.AddProfiles(new [] {
"Foo.UI",
"Foo.Core"
});
);
// Or marker types for assemblies:
Mapper.Initialize(cfg =>
cfg.AddProfiles(new [] {
typeof(HomeController),
typeof(Entity)
});
);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다