ValueInjecter and DataTable

夙愿已清 提交于 2019-11-26 21:49:37

问题


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.

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);
            }
        }
    }

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

~~~~~~ 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);
            }
        }
    }

回答1:


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

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).

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

So you are going to have:

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

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>;
... 
        }
    }

And usage:

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


来源:https://stackoverflow.com/questions/5462671/valueinjecter-and-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!