如何以及在何处在 WPF 应用程序中实现自动映射器
How and where to implement automapper in WPF application
我有BusinessLayer, DTO library,DataService, EntityModel(wher EDMX sits)
,DTO库是指业务层和数据层。我正在尝试在数据层中实现 automapper
,希望将实体对象映射到 DTO 对象和 dataService
库中的 return DTO。
目前正在这样做
public class DataService
{
private MapperConfiguration config;
public DataService()
{
IMapper _Mapper = config.CreateMapper();
}
public List<Dto.StudentDto> Get()
{
using(var context = new DbContext().GetContext())
{
var studentList = context.Students.ToList();
config = new MapperConfiguration(cfg => {
cfg.CreateMap<Db.Student, Dto.StudentDto>();
});
var returnDto = Mapper.Map<List<Db.Student>, List<Dto.StudentDto>>(studentList);
return returnDto;
}
}
}
如何将所有映射移动到一个 class 并且在调用 dataserive 时 automapper 应该自动初始化?
在 OnAppInitialize
上使用 AutoMapper.Mapper.CreateMap
。您可以在自己的 static
class
中执行课程以获得更好的风格。
这真的没有更多的魔法 - 因为您只需要注册 (CreateMap
) 一次映射。
initialize automatically when call to dataserive is made?
当然你也可以在构造函数中注册它。
Here您可以看一下另一个示例 - 如何以多种扩展方式中的一种或两种方式使用寄存器。
最终 AutoMapper
应该会让您的生活更轻松而不是更艰难。在我看来,最好的方法是在启动应用程序时一次性注册所有内容。
但您也可以按需进行,例如在构造函数中将每个 CreateMap
分开。
两种方式 - 只要确保只调用一次即可。
Is it good practice to use AutoMapper in data layer?
是的。
How can I move all the mappings to one class and automapper should initialize automatically when call to dataserive is made?
您可以只创建一个静态 class 来创建一次映射:
public static class MyMapper
{
private static bool _isInitialized;
public static Initialize()
{
if (!_isInitialized)
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Db.Student, Dto.StudentDto>();
});
_isInitialized = true;
}
}
}
确保您在数据服务中使用此 class:
public class DataService
{
public DataService()
{
MyMapper.Initialize();
}
public List<Dto.StudentDto> GetStudent(int id)
{
using (var context = new DbContext().GetContext())
{
var student = context.Students.FirstOrDefault(x => x.Id == id)
var returnDto = Mapper.Map<List<Dto.StudentDto>>(student);
return returnDto;
}
}
}
根据您实际托管 DAL 的方式,您可以从可执行文件的 Main()
方法或某处调用自定义映射器 class 的 Initialize()
方法除了 DataService
class.
的构造函数
我有BusinessLayer, DTO library,DataService, EntityModel(wher EDMX sits)
,DTO库是指业务层和数据层。我正在尝试在数据层中实现 automapper
,希望将实体对象映射到 DTO 对象和 dataService
库中的 return DTO。
目前正在这样做
public class DataService
{
private MapperConfiguration config;
public DataService()
{
IMapper _Mapper = config.CreateMapper();
}
public List<Dto.StudentDto> Get()
{
using(var context = new DbContext().GetContext())
{
var studentList = context.Students.ToList();
config = new MapperConfiguration(cfg => {
cfg.CreateMap<Db.Student, Dto.StudentDto>();
});
var returnDto = Mapper.Map<List<Db.Student>, List<Dto.StudentDto>>(studentList);
return returnDto;
}
}
}
如何将所有映射移动到一个 class 并且在调用 dataserive 时 automapper 应该自动初始化?
在 OnAppInitialize
上使用 AutoMapper.Mapper.CreateMap
。您可以在自己的 static
class
中执行课程以获得更好的风格。
这真的没有更多的魔法 - 因为您只需要注册 (CreateMap
) 一次映射。
initialize automatically when call to dataserive is made?
当然你也可以在构造函数中注册它。
Here您可以看一下另一个示例 - 如何以多种扩展方式中的一种或两种方式使用寄存器。
最终 AutoMapper
应该会让您的生活更轻松而不是更艰难。在我看来,最好的方法是在启动应用程序时一次性注册所有内容。
但您也可以按需进行,例如在构造函数中将每个 CreateMap
分开。
两种方式 - 只要确保只调用一次即可。
Is it good practice to use AutoMapper in data layer?
是的。
How can I move all the mappings to one class and automapper should initialize automatically when call to dataserive is made?
您可以只创建一个静态 class 来创建一次映射:
public static class MyMapper
{
private static bool _isInitialized;
public static Initialize()
{
if (!_isInitialized)
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Db.Student, Dto.StudentDto>();
});
_isInitialized = true;
}
}
}
确保您在数据服务中使用此 class:
public class DataService
{
public DataService()
{
MyMapper.Initialize();
}
public List<Dto.StudentDto> GetStudent(int id)
{
using (var context = new DbContext().GetContext())
{
var student = context.Students.FirstOrDefault(x => x.Id == id)
var returnDto = Mapper.Map<List<Dto.StudentDto>>(student);
return returnDto;
}
}
}
根据您实际托管 DAL 的方式,您可以从可执行文件的 Main()
方法或某处调用自定义映射器 class 的 Initialize()
方法除了 DataService
class.