从 DataTable 填充类
我有一个类需要从 DataTable 对象中进行水化。通常我以手动方式执行此操作。 (参见代码 snipit)。使用 ADO.NET 和 TSql 填充的 DataTable 对象。我需要将数据表中的值传输到我的 .NET 类中。有没有一种实用方法可以自动为我执行此操作?这样我就可以避免像下面这样的重复代码?
DriverSummary driver = new DriverSummary();
driver.Id = (int)row["Id"];
driver.UserId = row["UserId"] as string;
driver.Name = row["Name"] as string;
driver.TruckType = row["TruckType"] as string;
summaries.Add(driver);
我知道实体框架是一个应该填补这一空白的工具。我还没有完全跳转到实体框架。现在我想要一个类似于 MVC 的实用方法 UpdateModel() 的方法,它是轻量级且简单的,并且通过将键名称与属性名称相匹配来从表单值对列表中生成一个类。
这样一个实用的方法会节省我大量的时间!
I have a class that I need to hydrate from a DataTable object. Usually I do this the manual way. (see code snipit). The DataTable object populated using ADO.NET and TSql. I need to transfer the values from the DataTable into my .NET class. Is there a utility method that will do this for me automagically? So that I can avoid repetitive code like the following?
DriverSummary driver = new DriverSummary();
driver.Id = (int)row["Id"];
driver.UserId = row["UserId"] as string;
driver.Name = row["Name"] as string;
driver.TruckType = row["TruckType"] as string;
summaries.Add(driver);
I know that the Entity Framework is a tool that is supposed to fill this gap. I haven't quite made the jump to Entity Framework. For now I'd like to have a method that is similar to MVC's utility method UpdateModel() Which is lightweight and simple and hydrates a class from a list of form-value pairs by matching up key names with property names.
Such a utility method would save me tons of time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像上面提到的,我相信 AutoMapper 现在可以做到这一点。您还可以查看 ValueInjecter。 ValueInjecter 和 DataTable
Like mentioned above I believe AutoMapper can do this now. You could also look at a ValueInjecter. ValueInjecter and DataTable