Ejemplo n.º 1
0
        public PocoData Build()
        {
            var pocoData = new PocoData(Type, Mapper);

            pocoData.TableInfo = _tableInfoPlan();

            pocoData.Members = _memberPlans.Select(plan => plan(pocoData.TableInfo)).ToList();

            pocoData.Columns = GetPocoColumns(pocoData.Members, false).Where(x => x != null).ToDictionary(x => x.ColumnName, x => x, StringComparer.OrdinalIgnoreCase);
            pocoData.AllColumns = GetPocoColumns(pocoData.Members, true).Where(x => x != null).ToList();

            //Build column list for automatic select
            pocoData.QueryColumns = pocoData.Columns.Where(c => !c.Value.ResultColumn && c.Value.ReferenceType == ReferenceType.None).ToArray();

            return pocoData;
        }
Ejemplo n.º 2
0
        public PocoData ForObject(object o, string primaryKeyName)
        {
            var t = o.GetType();
#if !POCO_NO_DYNAMIC
            if (t == typeof(System.Dynamic.ExpandoObject) || t == typeof(PocoExpando))
            {
                var pd = new PocoData();
                pd.TableInfo = new TableInfo();
                pd.Columns = new Dictionary<string, PocoColumn>(StringComparer.OrdinalIgnoreCase);
                pd.Columns.Add(primaryKeyName, new ExpandoColumn() {ColumnName = primaryKeyName});
                pd.TableInfo.PrimaryKey = primaryKeyName;
                pd.TableInfo.AutoIncrement = true;
                foreach (var col in ((IDictionary<string, object>) o).Keys)
                {
                    if (col != primaryKeyName)
                        pd.Columns.Add(col, new ExpandoColumn() {ColumnName = col});
                }
                return pd;
            }
            else
#endif
                return ForType(t);
        }
Ejemplo n.º 3
0
 public static PocoData ForObjectStatic(object o, string primaryKeyName, bool autoIncrement, Func<Type, PocoData> fallback)
 {
     var t = o.GetType();
     #if !NET35
     if (t == typeof (System.Dynamic.ExpandoObject) || t == typeof (PocoExpando))
     {
         var pd = new PocoData();
         pd.TableInfo = new TableInfo();
         pd.Columns = new Dictionary<string, PocoColumn>(StringComparer.OrdinalIgnoreCase);
         pd.Columns.Add(primaryKeyName, new ExpandoColumn() {ColumnName = primaryKeyName});
         pd.TableInfo.PrimaryKey = primaryKeyName;
         pd.TableInfo.AutoIncrement = autoIncrement;
         foreach (var col in ((IDictionary<string, object>) o))
         {
             if (col.Key != primaryKeyName)
                 pd.Columns.Add(col.Key, new ExpandoColumn()
                 {
                     ColumnName = col.Key,
                     MemberInfoData = new MemberInfoData(col.Key, col.Value.GetTheType() ?? typeof (object), typeof (object)),
                 });
         }
         return pd;
     }
     else
     #endif
         return fallback(t);
 }
Ejemplo n.º 4
0
 public MappingFactory(PocoData pocoData)
 {
     _pocoData = pocoData;
 }
Ejemplo n.º 5
0
 public MappingFactory(PocoData pocoData, DbDataReader dataReader)
 {
     _pocoData = pocoData;
     _rowMapper = RowMappers.Select(mapper => mapper()).First(x => x.ShouldMap(pocoData));
     _rowMapper.Init(dataReader, pocoData);
 }
Ejemplo n.º 6
0
 static List <PropertyInfo> RelevantProperties(PocoData pocoData)
 {
     return(pocoData.Columns.Values.Where(x => !x.MemberInfo.IsField()).Select(x => ((PropertyInfo)x.MemberInfo)).ToList());
 }
Ejemplo n.º 7
0
 public object GetColumnValue(PocoData pd, object target, Func<PocoColumn, object, object> callback = null)
 {
     callback = callback ?? ((_, o) => o);
     if (ReferenceType == ReferenceType.Foreign)
     {
         var member = pd.Members.Single(x => x.MemberInfoData == MemberInfoData);
         var column = member.PocoMemberChildren.SingleOrDefault(x => x.Name == member.ReferenceMemberName);
         if (column == null)
         {
             throw new Exception(string.Format("Could not find member on '{0}' with name '{1}'", member.MemberInfoData.MemberType, member.ReferenceMemberName));
         }
         return callback(column.PocoColumn, column.PocoColumn.GetValue(target));
     }
     else
     {
         return callback(this, GetValue(target));
     }
 }
Ejemplo n.º 8
0
 private static List <Change> Diff(T original, T current, PocoData pocoData)
 {
     differ = differ ?? GenerateDiffer(pocoData);
     return(differ(original, current));
 }
Ejemplo n.º 9
0
 private static T Clone(T myObject, PocoData data)
 {
     cloner = cloner ?? GenerateCloner(data);
     return(cloner(myObject));
 }
Ejemplo n.º 10
0
 public Snapshot(PocoData data, T original)
 {
     pocoData        = data;
     memberWiseClone = Clone(original, pocoData);
     trackedObject   = original;
 }
Ejemplo n.º 11
0
        private static Func <T, T, List <Change> > GenerateDiffer(PocoData pocoData)
        {
            var dm = new DynamicMethod("DoDiff", typeof(List <Change>), new Type[] { typeof(T), typeof(T) }, true);

            var il = dm.GetILGenerator();

            // change list
            il.DeclareLocal(typeof(List <Change>));
            il.DeclareLocal(typeof(Change));
            il.DeclareLocal(typeof(object)); // boxed change
            il.DeclareLocal(typeof(object)); // orig val

            il.Emit(OpCodes.Newobj, typeof(List <Change>).GetConstructor(Type.EmptyTypes));
            // [list]
            il.Emit(OpCodes.Stloc_0);

            foreach (var prop in RelevantProperties(pocoData))
            {
                // []
                il.Emit(OpCodes.Ldarg_0);
                // [original]
                il.Emit(OpCodes.Callvirt, prop.GetGetMethod());
                // [original prop val]

                il.Emit(OpCodes.Dup);
                // [original prop val, original prop val]

                if (prop.PropertyType != typeof(string))
                {
                    il.Emit(OpCodes.Box, prop.PropertyType);
                    // [original prop val, original prop val boxed]
                }

                il.Emit(OpCodes.Stloc_3);
                // [original prop val]

                il.Emit(OpCodes.Ldarg_1);
                // [original prop val, current]

                il.Emit(OpCodes.Callvirt, prop.GetGetMethod());
                // [original prop val, current prop val]

                il.Emit(OpCodes.Dup);
                // [original prop val, current prop val, current prop val]

                if (prop.PropertyType != typeof(string))
                {
                    il.Emit(OpCodes.Box, prop.PropertyType);
                    // [original prop val, current prop val, current prop val boxed]
                }

                il.Emit(OpCodes.Stloc_2);
                // [original prop val, current prop val]

                il.EmitCall(OpCodes.Call, typeof(Snapshot <T>).GetMethod("AreEqual", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(new Type[] { prop.PropertyType }), null);
                // [result]

                Label skip = il.DefineLabel();
                il.Emit(OpCodes.Brtrue_S, skip);
                // []

                il.Emit(OpCodes.Newobj, typeof(Change).GetConstructor(Type.EmptyTypes));
                // [change]
                il.Emit(OpCodes.Dup);
                // [change,change]

                il.Emit(OpCodes.Stloc_1);
                // [change]

                il.Emit(OpCodes.Ldstr, prop.Name);
                // [change, name]
                il.Emit(OpCodes.Callvirt, typeof(Change).GetMethod("set_Name"));
                // []

                il.Emit(OpCodes.Ldloc_1);
                // [change]

                il.Emit(OpCodes.Ldloc_3);
                // [change, original prop val boxed]

                il.Emit(OpCodes.Callvirt, typeof(Change).GetMethod("set_OldValue"));
                // []

                il.Emit(OpCodes.Ldloc_1);
                // [change]

                il.Emit(OpCodes.Ldloc_2);
                // [change, boxed]

                il.Emit(OpCodes.Callvirt, typeof(Change).GetMethod("set_NewValue"));
                // []

                il.Emit(OpCodes.Ldloc_0);
                // [change list]
                il.Emit(OpCodes.Ldloc_1);
                // [change list, change]
                il.Emit(OpCodes.Callvirt, typeof(List <Change>).GetMethod("Add"));
                // []

                il.MarkLabel(skip);
            }

            il.Emit(OpCodes.Ldloc_0);
            // [change list]
            il.Emit(OpCodes.Ret);

            return((Func <T, T, List <Change> >)dm.CreateDelegate(typeof(Func <T, T, List <Change> >)));
        }
Ejemplo n.º 12
0
 public Snapshot(PocoData pocoData, T trackedObject)
 {
     _pocoData      = pocoData;
     _trackedObject = trackedObject;
     PopulateValues(trackedObject);
 }