在哪里放置AutoMapper.CreateMaps? [英] Where to place AutoMapper.CreateMaps?
问题描述
我在 ASP.NET MVC
应用程序中使用 AutoMapper
。我被告知,我应该移动 AutoMapper.CreateMap
在其他地方,因为他们有很多开销。我不太确定如何设计我的应用程序,把这些调用只在一个地方。
我有一个web层,服务层和数据层。每个项目自己。我使用 Ninject
DI一切。我将在Web和服务层中使用 AutoMapper
。
那么你对 AutoMapper
的CreateMap有什么设置?你把它放在哪里?
无关紧要,只要它是一个静态类。
我们的约定是每个层(网络,服务,数据)都有单个文件 AutoMapperXConfiguration.cs
,使用一个名为 Configure()
的方法,其中 c> c c $ c> private
方法。 以下是我们的web层配置示例:
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
ConfigureUserMapping
ConfigurePostMapping();
}
private static void ConfigureUserMapping()
{
Mapper.CreateMap< User,UserViewModel>();
}
// ... etc
}
我们为每个聚合(User,Post)创建一个方法,以便将它们分开。
然后您的 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>();
}
}
更干净/更健壮。
I'm using AutoMapper
in an ASP.NET MVC
application. I was told that I should move the AutoMapper.CreateMap
elsewhere as they have a lot of overhead. I'm not too sure how to design my application to put these calls in just 1 place.
I have a web layer, service layer and a data layer. Each a project of its own. I use Ninject
to DI everything. I'll utilize AutoMapper
in both web and service layers.
So what are your setup for AutoMapper
's CreateMap? Where do you put it? How do you call it?
Doesn't matter, as long as it's a static class. It's all about convention.
Our convention is that each "layer" (web, services, data) has a single file called AutoMapperXConfiguration.cs
, with a single method called Configure()
, where X
is the layer.
The Configure()
method then calls private
methods for each area.
Here's an example of our web tier config:
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
ConfigureUserMapping();
ConfigurePostMapping();
}
private static void ConfigureUserMapping()
{
Mapper.CreateMap<User,UserViewModel>();
}
// ... etc
}
We create a method for each "aggregate" (User, Post), so things are separated nicely.
Then your Global.asax
:
AutoMapperWebConfiguration.Configure();
AutoMapperServicesConfiguration.Configure();
AutoMapperDomainConfiguration.Configure();
// etc
It's kind of like an "interface of words" - can't enforce it, but you expect it, so you can code (and refactor) if necessary.
EDIT:
Just thought I'd mention that I now use AutoMapper profiles, so the above example becomes:
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>();
}
}
Much cleaner/more robust.
这篇关于在哪里放置AutoMapper.CreateMaps?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!