填充模型对象的最佳实践
假设我有几个模型类,如 Person.cs、Car.cs、Manufacturer.cs,每个类都有 30-40 个不同数据类型的属性。这些模型必须使用基于 Linq 的框架(称为“XrmContext”)基于 Guid(主键)匹配来填充。
执行此操作的普通方法是手动一一填充每一列,例如
Person modelObject = new Person();
var xrm = new DataContext("MyXrmService");
var xrmPerson = xrm.CreateEntity("new_person");
xrmPerson.SetPropertyValue("new_ssn", modelObject.SSN);
xrmPerson.SetPropertyValue("new_personid", new Guid(modelObject.PersonGuid));
.... 20-30 个这样的语句,一一填充每个属性。
有没有更好的方法可以定义 Linq DataSource 属性和模型属性之间的映射。
Suppose I have few model classes like Person.cs, Car.cs, Manufacturer.cs each of which has 30-40 properties of varying datatypes. These models have to be populated using a Linq based framework called 'XrmContext' based on a Guid (primary key) match.
Ordinary way of doing this to populated each column one by one manually like
Person modelObject = new Person();
var xrm = new DataContext("MyXrmService");
var xrmPerson = xrm.CreateEntity("new_person");
xrmPerson.SetPropertyValue("new_ssn", modelObject.SSN);
xrmPerson.SetPropertyValue("new_personid", new Guid(modelObject.PersonGuid));
.... 20-30 statements like this which populate each property one by one.
Is there a better way of doing it where I can define mappings between Linq DataSource attributes and model properties.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 AutoMapper。您可以生成映射,用另一个对象的值填充一个对象的属性。
Take a look at AutoMapper. You can generate mappings which fills the properties of one object with the values of another one.