Ale*_*der 9 c# dependency-injection ninject automapper asp.net-mvc-5
我试图在ASP.NET MVC 5应用程序中使用Ninject,该应用程序使用AutoMapper将模型映射到视图模型,反之亦然.不幸的是,我收到一条错误消息,指出缺少类型映射配置.
我创建了一个Ninject依赖解析器:
namespace MyNamespace.Infrastructure
{
public class NinjectDependencyResolver: IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
kernel.Bind<IMyBLL>().To<MyBLL>();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我用它来创建一个控制器:
namespace MyNamespace.Controllers
{
[Authorize]
public class HomeController : Controller
{
private IMyBLL _myBLL;
public HomeController(IMyBLL myBLLParam)
{
_myBLL = myBLLParam;
}
public PartialViewResult AddRecord()
{
return PartialView(new AddRecordViewModel());
}
[HttpPost]
public void AddRecord(AddRecordViewModel recordViewModel)
{
var record = Mapper.Map<Record>(recordViewModel);
_myBLL.AddRecord(record, User.Identity.Name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Global.asax中:
namespace MyNamespace.WebApplication
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ApplicationUserManager.StartupAsync();
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutoMapperWebConfiguration.Configure();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会调用AutoMapper配置:
namespace MyNamespace.WebApplication.Infrastructure
{
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg => cfg.AddProfile(new RecordProfile()));
}
}
public class RecordProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap();
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我收到以下错误消息:
Missing type map configuration or unsupported mapping.
Mapping types:
AddRecordViewModel -> Record
MyNamespace.WebApplication.ViewModels.Home.AddRecordViewModel -> MyNamespace.Model.Record
Destination path:
Record
Source value:
MyNamespace.WebApplication.ViewModels.Home.AddRecordViewModel
Run Code Online (Sandbox Code Playgroud)
我想念一些东西吗?在使用Ninject依赖项解析器之前,它工作正常.现在它似乎没有找到映射.
如果我将Mapping Creation直接添加到控制器方法,它可以工作:
[HttpPost]
public void AddRecord(AddRecordViewModel recordViewModel)
{
Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap();
var record = Mapper.Map<Record>(recordViewModel);
_myBLL.AddRecord(record, User.Identity.Name);
}
Run Code Online (Sandbox Code Playgroud)
映射本身以及模型和视图模型似乎不是问题.我猜这个程序以某种方式找不到映射.
即使我在控制器方法中调用自动映射器Web配置,它也可以:
public void AddRecord(AddRecordViewModel recordViewModel)
{
Infrastructure.AutoMapperWebConfiguration.Configure();
var record = Mapper.Map<Record>(recordViewModel);
_myBLL.AddRecord(record, User.Identity.Name);
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*ard 13
您还需要添加反向映射.您可以通过以下两种方式之一完成:
Mapper.CreateMap<AddRecordViewModel, Record>();
Mapper.CreateMap<Record, AddRecordViewModel>();
Run Code Online (Sandbox Code Playgroud)
或者像这样:
Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap();
Run Code Online (Sandbox Code Playgroud)
如果你问我,后者更可取.