3 回答

TA貢獻1833條經驗 獲得超4個贊
沒關系,只要它是靜態類即可。一切都與慣例有關。
我們的約定是,每個“層”(Web,服務,數據)都有一個名為的文件AutoMapperXConfiguration.cs,并帶有一個名為的方法Configure(),其中X是該層。
Configure()然后,該private方法為每個區域調用方法。
這是我們的Web層配置的示例:
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
ConfigureUserMapping();
ConfigurePostMapping();
}
private static void ConfigureUserMapping()
{
Mapper.CreateMap<User,UserViewModel>();
}
// ... etc
}
我們為每個“聚合”(用戶,發布)創建一個方法,因此可以很好地分離事物。
然后您的Global.asax:
AutoMapperWebConfiguration.Configure();
AutoMapperServicesConfiguration.Configure();
AutoMapperDomainConfiguration.Configure();
// etc
它有點像“單詞的界面”-無法強制執行,但是您期望得到它,因此可以在必要時進行編碼(和重構)。
編輯:
只是以為我提到我現在使用AutoMapper 配置文件,因此上面的示例變為:
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new UserProfile());
cfg.AddProfile(new PostProfile());
});
}
}
public class UserProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<User,UserViewModel>();
}
}
更清潔/更堅固。
- 3 回答
- 0 關注
- 1143 瀏覽
添加回答
舉報