ValueInjecter和数据表 [英] ValueInjecter and DataTable

查看:122
本文介绍了ValueInjecter和数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出ValueInjecter,以便可以在我们自己生产的小ORM中使用它.由于我应该支持DataRow和DataTable映射,因此我正在尝试为此类型实现映射器.老实说,文档还不够好,我想试一试.也许Omu或该库的其他一些用户会回答.

I was trying to figure out ValueInjecter so i can use it in our home-grown little ORM. Since i should support DataRow and DataTable mapping, i am trying to implement mappers for this types. And honestly documentation is not good enough and i wanted to give it a shot. Maybe Omu or some other users of this library will answer.

这是我的DataRow注射器

here is my DataRow injector

public class DataRowInjection: KnownSourceValueInjection<DataRow>
    {
        protected override void Inject(DataRow source, object target)
        {
            for (var i = 0; i < source.ItemArray.Count(); i++)
            {

                //TODO: Read from attributes or target type
                var activeTarget = target.GetProps().GetByName(source.Table.Columns[i].ToString(), true);
                if (activeTarget == null) continue;

                var value = source.ItemArray[i];
                if (value == DBNull.Value) continue;

                activeTarget.SetValue(target, value);
            }
        }
    }

这很好用.所以这是一个问题,我如何才能为DataTable实现此功能并返回Ienumarable或IList.我期望做的代码段就像.

this works pretty well. so here is the question how can i implement this for DataTable and return Ienumarable or IList. the code snippet i expect to do is like.

public class DataTableInjector  : ?????
    {
        protected override void Inject(DataTable source, IList<T> target)   where T : class
        {

          // Do My Staff
            foreach (var row in source.Rows)
            {
                target[i].InjectFrom<DataRowInjection>(row);
            }

            //return IList?
        }
    }

我该如何做到这一点. 谢谢

How can i accomplish this. Thank you

~~~~~~这是我在Omu的帮助下编写的完整代码

~~~~~~ here is complete code i wrote with Omu's help

 public class DataTableInjection<T> : ValueInjection where T  : new()
    {
        protected override void Inject(object source, object target)
        {
            var dt = source as DataTable;
            var t = target as IList<T>;

            foreach (DataRow dr in dt.Rows)
            {
                var t2 = new T();
                t2.InjectFrom<DataRowInjection>(dr);
                t.Add(t2);
            }
        }
    }

推荐答案

与您为DataRow所做的相同,现在只需使用KnownSourceValueInjection<DataTable>.

Same as you did it for DataRow, you just use KnownSourceValueInjection<DataTable> now.

您还可以看到Inject方法是void,因此您无需返回任何内容,只需更改目标对象(已存在)即可.

Also as you can see the Inject method is void so you don't return anything you just change the target object (which already exists).

请记住,InjectFrom不会创建新对象,而是将值注入到现有对象中.

Remember that InjectFrom doesn't create new objects, it injects values into an existing one.

因此您将拥有:

var list = new List<T>();
list.InjectFrom<MyFromDataTableInj>(dataTable)

实际上,在您的情况下,您将仅使用从DataTable到IList<T>的注入 所以你可以这样:

Actually in your case you are going to use this injection only from DataTable to IList<T> so you could do like this:

   public class My<T> : ValueInjection
   {
        protected override void Inject(object source, object target)
        {
            var dt = source as DataTable;
            var t = target as IList<T>;
... 
        }
    }

和用法:

list.InjectFrom<My<Foo>>(datatable):

这篇关于ValueInjecter和数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆