asp.net-mvc – Automapper和Windsor的问题
当我尝试使用依赖注入的自定义解析器时,我遇到了Automapper的问题.
我有以下型号: public class User : Entity { public virtual string Name { get; set; } public virtual Country Country { get; set; } } public class Country : Entity { public virtual string Name { get; set; } } 和以下视图模型: public class RegistrationViewModel { [Required] public string Name { get; set; } public int CountryId { get; set; } public IEnumerable<Country> Countries { get; set; } } 为了映射我使用以下代码: Mapper.Map(registrationViewModel,user); 我之前注册以下内容: Mapper.Reset(); container = new WindsorContainer(); container.AddFacility<FactorySupportFacility>(); container.Register(Component.For<ISession>(). UsingFactoryMethod(() => NHibernateSessionFactory.RetrieveSession()). LifeStyle.Is(LifestyleType.Transient)); container.Register(Component.For(typeof(LoadingEntityResolver<>)).ImplementedBy(typeof(LoadingEntityResolver<>)).LifeStyle.Transient); Mapper.Initialize(x => { x.AddProfile<BasicProfile>(); x.ConstructServicesUsing(container.Resolve); }); 我的BasicProfile如下: public class BasicProfile : Profile { public const string VIEW_MODEL = "MyBasicProfile"; public override string ProfileName { get { return VIEW_MODEL; } } protected override void Configure() { CreateMaps(); } private void CreateMaps() { CreateMap<RegistrationViewModel,User>() .ForMember(dest => dest.Country,_ => _.ResolveUsing<LoadingEntityResolver<Country>>().FromMember(src => src.CountryId)) ); } } 自定义解析程序按以下方式完成: public class LoadingEntityResolver<TEntity> : ValueResolver<int,TEntity> where TEntity: Entity { private readonly ISession _session; public LoadingEntityResolver(ISession session) { _session = session; } protected override TEntity ResolveCore(int source) { return _session.Load<TEntity>(source); } } 运行映射代码时,我得到以下异常:
我不知道可能出错了什么.它可能是构建解析器的东西.当我尝试以下时没有问题: var resolver = container.Resolve<LoadingEntityResolver<Country>>(); Assert.IsInstanceOf<LoadingEntityResolver<Country>>(resolver); 我会很乐意为你提供帮助. 最好的祝福 解决方法
你有一些非常沉重的DI东西:-)我会避免让AutoMapper从数据库或其他任何东西解析实体.使代码难以理解,跟随对象的生命周期可能成为一场噩梦.
无论如何,要解决您的问题,只需从(错误)交换订单: Mapper.Initialize(x => { x.AddProfile<BasicProfile>(); x.ConstructServicesUsing(container.Resolve); }); 纠正): Mapper.Initialize(x => { x.ConstructServicesUsing(container.Resolve); x.AddProfile<BasicProfile>(); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 如何监视SQL Server中的活动连接池?
- asp.net-mvc – 使用GoogleOAuth2AuthenticationOptions时出
- ASP.NET Core 运行原理剖析
- asp.net-mvc – asp.net mvc 3中的参考脚本库是什么?
- asp.net – 干净的方式来防止输入按钮提交表单
- asp.net-mvc-3 – 即使关闭了自动完成功能,Firefox也会缓存
- asp.net-mvc – SignalR 2不生成/ signalr / hubs
- asp.net – VirtualPathUtility.ToAbsolute()VS. Url.Conte
- asp.net-mvc – 是否可以手动更新ModelState.IsValid?
- asp.net – 谁在生产应用程序中实际使用DataGrid / GridVie
- asp.net-mvc – 是否可以在按钮点击时“交换”部
- asp.net – 什么时候应该使用工厂模式?
- Asp.net成员资格表:aspnet_UsersInRoles未出现在
- asp.net-mvc – jQuery Mobile和不显眼的验证
- asp.net-core – “netcoreapp1.0”是一个不受支
- Shadow Copying导致ASP.NET应用启动很慢的解决办
- asp.net-mvc-3 – 你能在ASP.net MVC3中使用嵌套
- asp.net-mvc-5 – aspnet身份避免同时登录同一帐
- 如何在ASP.Net MVC标记中克服我对<%%>的恐惧?
- asp.net-mvc – MVC 3从web.config中的AppSettin