如何在ioc中使用存储库模式中的模型验证 [英] How to use model validation in repository pattern with ioc
问题描述
RESTful Day#3:使用Inversion of Control解决依赖关系的依赖关系ASP.NET Web API中使用Unity容器和托管扩展性框架(MEF)进行依赖注入 [ ^ ]
我遵循这些模式
但最后我坚持如何使用
带属性的模型验证
我是MVC的新手
请帮助我
我尝试了什么:
我用这种方式
if(productToValidate.Name.Trim()。Length == 0)
_modelState.AddModelError(Name,Name is required。);
但如果我有更大的条目呢?
<块引用>我通过使用自动映射器完成
就像这样
在模型文件夹中制作模型
< br $>
和控制器中的地图
var product = _productServices.GetProductById(id) ;
Mapper.Initialize(cfg => cfg.CreateMap< ProductEntity,ProductModel>());
var itemModel = Mapper.Map< ProductEntity,ProductModel>(product);
var product
在此我得到来自服务层的所有数据
然后映射由automapper完成,最终数据我将进入
itemModel
RESTful Day #3: Resolve dependency of dependencies using Inversion of Control and dependency injection in ASP.NET Web APIs with Unity Container and Managed Extensibility Framework (MEF)[^]
I follow these pattern
But at last i stuck how to use
Model validation with attributes
I'm new in MVC
Please help me
What I have tried:
I use in these way
if (productToValidate.Name.Trim().Length == 0) _modelState.AddModelError("Name", "Name is required.");
But if I have larger entries then?
i`m done by using auto mapper
like this way
Make Model In Model Folder
and map in controller
var product = _productServices.GetProductById(id); Mapper.Initialize(cfg => cfg.CreateMap<ProductEntity, ProductModel>()); var itemModel = Mapper.Map<ProductEntity, ProductModel>(product);
var productIn this i get all data from service layer
then mapping is done by automapper and final data i will get initemModel
这篇关于如何在ioc中使用存储库模式中的模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!