ASP.NET MVC 웹 사이트에서 AutoMapper를 사용하여 데이터베이스 객체를 ViewModel 객체에 매핑하고 여러 프로파일을 사용하여 동일한 유형을 매핑하려고합니다. 다른 논리. 나는 그가 말한 곳에서 Matt's blog post을 읽음으로써 그렇게 할 생각을 가지고있었습니다 :Automapper에서 프로파일을 사용하여 다른 논리로 동일한 유형을 매핑하십시오.
정말 중요한 부분은 AutoMapper 구성 프로파일입니다. 프로파일로 구성을 그룹화 할 수 있습니다. 하나의 프로파일에서 한 f}으로 날짜를 형식화하고, 다른 프로파일에서 다른 f}으로 날짜를 형식화 할 수 있습니다. 나는 여기서 하나의 프로파일을 사용하고있다.
public class MyProfile : Profile { protected override string ProfileName { get { return "MyProfile"; } } protected override void Configure() { CreateMap<DateTime, String>().ConvertUsing<StringFromDateTimeTypeConverter>(); } } public class StringFromDateTimeTypeConverter : ITypeConverter<DateTime, String> { public string Convert(DateTime source) { return source.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture); } }
그리고 또 다른 경우에 대한 또 다른 하나 : 그러나
public class MyProfile2 : Profile { protected override string ProfileName { get { return "MyProfile2"; } } protected override void Configure() { CreateMap<DateTime, String>().ConvertUsing<AnotherStringFromDateTimeTypeConverter>(); } } public class AnotherStringFromDateTimeTypeConverter : ITypeConverter<DateTime, String> { public string Convert(DateTime source) { return source.ToString("mm - yyyy", CultureInfo.InvariantCulture); } }
, 나는이를 지정
Mapper.Map<>()
방법의 오버로드를 찾을 수 없습니다
은 그래서 하나의 사건에 대한 프로필을 생성 윤곽. 또한 행운이없는 Configuration
개체를 살펴 보았습니다.
마지막으로 등록 된 프로필이 항상 우선합니다.
프로필을 사용하는 방법이 있습니까?
감사합니다. – sebd
잠금없이 스레드가 안전할까요? 예를 들어, 'MapperRegistry.AllMappers()'는 정적이고 'TypeMapFactory'는 정적 사전을 가지고 있습니다. –
일반적으로 구성은 시작시 AppDomain 당 한 번만 호출됩니다. WCF, ASP.NET, WinForms 등에서 다른 모델을 호스팅하는 모델에서 올바른 솔기를 찾아야합니다. –