我在ASP.NET WEB API中使用Ninject和AutoMapper将服务模型映射到实体模型。在这里,我试图首先将'NewUser'服务模型映射到'User'实体模型,并将'NewUser'分析到数据库,最后将新存储的数据映射到'User'服务模型。 这是NinjectDependencyResolver类:
public sealed class NinjectDipendencyResolver :IDependencyResolver
{
private readonly IKernel _container;
public NinjectDipendencyResolver(IKernel contanier)
{
_container = contanier;
}
public IKernel Container
{
get { return _container; }
}
public object GetService(Type servicetType)
{
return _container.TryGet(servicetType);
}
public IEnumerable<object> GetServices(Type ServiceType)
{
return _container.GetAll(ServiceType);
}
public IDependencyScope BeginScope()
{
return this;
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
以下是NewUser
到User
enity和User
实体到User
服务模型映射实现:
Mapper.Initialize(cfg => cfg.CreateMap<Models.NewUser, Data.Entities.User>()
.ForMember(m => m.UserId, i => i.Ignore())
.ForMember(m => m.AuthKey, i => i.Ignore())
.ForMember(m => m.DisabledDate, i => i.Ignore())
.ForMember(m => m.JoinedDate, i => i.Ignore())
.ForMember(m => m.LastSeen, i => i.Ignore())
.ForMember(m => m.Version, i => i.Ignore())
.ForMember(m => m.Goals, i => i.Ignore()));
Mapper.Initialize(cfg => cfg.CreateMap<Data.Entities.User, Models.User>()
.ForMember(m => m.Links, i => i.Ignore())
.ForMember(m => m.Goals, i => i.MapFrom(j => Mapper.Map<ICollection<Data.Entities.Goal>, List<Models.Goal>>(j.Goals)))
.ReverseMap()
.ForMember(m => m.Goals, i => i.MapFrom(j => Mapper.Map<List<Models.Goal>, ICollection<Data.Entities.Goal>>(j.Goals)))
.ForMember(m => m.Version, i => i.Ignore()));
以下是NinjectConfigurator
类,我将所有映射配置为单IAutoMapperTypeConfigurator
:
public class NinjectConfigurator
{
public void Configure(IKernel container)
{
AddBindings(container);
}
private void AddBindings(IKernel container)
{
ConfigureUnitOfWork(container);
ConfigureAutoMapper(container);
}
private void ConfigureUnitOfWork(IKernel container)
{
var unitOfWork = new UnitOfWork(new AppTestDBContext());
container.Bind<IUnitOfWork>().ToConstant(unitOfWork);
}
private void ConfigureAutoMapper(IKernel container)
{
container.Bind<IAutoMapper>()
.To<AutoMapperAdapter>()
.InSingletonScope();
container.Bind<IAutoMapperTypeConfigurator>()
.To<NewUserMapper>()
.InSingletonScope();
container.Bind<IAutoMapperTypeConfigurator>()
.To<UserMapper>()
.InSingletonScope();
}
}
AutoMapperConfigurator
我在一个地方配置映射:
public class AutoMapperConfigurator
{
public void Configure(IEnumerable<IAutoMapperTypeConfigurator> autoMapperTypeConfigurations)
{
autoMapperTypeConfigurations.ToList().ForEach(m => m.Configure());
Mapper.AssertConfigurationIsValid();
}
}
这是我的Global.ascx.cs,我在Application_Start
上配置映射:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
new AutoMapperConfigurator().Configure(
WebContainerManager.GetAll<IAutoMapperTypeConfigurator>());
}
控制器操作:
public async Task<IHttpActionResult> Post(NewUser newUser)
{
var createdUser = _iUserMaintenanceProcessor.Add(newUser);
if (createdUser != null)
{
return Ok(createdUser);
}
else
return NotFound();
}
和
public Models.User Add(NewUser newUser)
{
var userEntity = _autoMapper.Map<Data.Entities.User>(newUser);
_unitOfWork.User.Add(userEntity);
var user = _autoMapper.Map<Models.User>(userEntity);
return user;
}
问题是每当我通过fiddler调用Api的Post
方法时它会抛出以下异常:
{“消息”:“发生了错误。”,“ExceptionMessage”:“缺少类型 映射配置或不支持的映射。\ r \ n \ r \ nMapping 类型:\ r \ n新的用户 - &gt; User \ r \ nTestApp.Web.Api.Models.NewUser - &gt; TestApp.Data.Entities.User”, “ExceptionType”: “AutoMapper.AutoMapperMappingException”}
请帮我弄明白原因。
更新:
我试图添加初始化mathod本身的映射,它的工作原理。看起来映射已在Application_Start
初始化,但如上所述,我已初始化映射。这是更新的mehthod工作:
public Models.User Add(NewUser newUser)
{
Mapper.Initialize(cfg => cfg.CreateMap<Models.NewUser, Data.Entities.User>()
.ForMember(m => m.UserId, i => i.Ignore())
.ForMember(m => m.AuthKey, i => i.Ignore())
.ForMember(m => m.DisabledDate, i => i.Ignore())
.ForMember(m => m.JoinedDate, i => i.Ignore())
.ForMember(m => m.LastSeen, i => i.Ignore())
.ForMember(m => m.Version, i => i.Ignore())
.ForMember(m => m.Goals, i => i.Ignore()));
var userEntity = _autoMapper.Map<Data.Entities.User>(newUser);
_unitOfWork.User.Add(userEntity);
Mapper.Initialize(cfg => cfg.CreateMap<Data.Entities.User, Models.User>()
.ForMember(m => m.Links, i => i.Ignore())
.ForMember(m => m.Goals, i => i.MapFrom(j => Mapper.Map<ICollection<Data.Entities.Goal>, List<Models.Goal>>(j.Goals)))
.ReverseMap()
.ForMember(m => m.Goals, i => i.MapFrom(j => Mapper.Map<List<Models.Goal>, ICollection<Data.Entities.Goal>>(j.Goals)))
.ForMember(m => m.Version, i => i.Ignore()));
var user = _autoMapper.Map<Models.User>(userEntity);
return user;
}
答案 0 :(得分:1)
您只能运行一次Initialize方法。所以我要你做的就是更改代码如下:
更改IAutoMapperTypeConfigurator接口以获取配置属性
public interface IAutoMapperTypeConfigurator
{
void Configure(IMapperConfigurationExpression configuration);
}
更改映射器以使用此配置属性
public void Configure(IMapperConfigurationExpression configuration)
{
configuration.CreateMap<Models.NewUser, Data.Entities.User>()
.ForMember(m => m.UserId, i => i.Ignore())
.ForMember(m => m.AuthKey, i => i.Ignore())
.ForMember(m => m.DisabledDate, i => i.Ignore())
.ForMember(m => m.JoinedDate, i => i.Ignore())
.ForMember(m => m.LastSeen, i => i.Ignore())
.ForMember(m => m.Version, i => i.Ignore())
.ForMember(m => m.Goals, i => i.Ignore());
}
在AutoMapperConfiguration更改方法中,只运行一次初始化
public void Configure(IEnumerable<IAutoMapperTypeConfigurator> autoMapperTypeConfigurations)
{
Mapper.Initialize(config => autoMapperTypeConfigurations.ToList().ForEach(m => m.Configure(config)));
Mapper.AssertConfigurationIsValid();
}
这应该有用。