Commits

sake  committed be9ccd6

adapt

  • Participants
  • Parent commits 3ba5843

Comments (0)

Files changed (1)

File Usually/Resolve.cs

         public void ForEach(T obj, Action<string, object> action)
         {
             for (int i = 0; i < this.names.Count; i++)
-                action(this.names[i], this.getters[this.names[i]](obj));
+                action(this.names[i], (obj == null) ? obj : this.getters[this.names[i]](obj));
         }
         public void ForEachChange(T o, T n, Action<string, object, object> action)
         {
         {
             this.group = @group;
         }
+        public void ForEachInfoValue(T obj, Action<string, object> action)
+        {
+            this.keys.ForEach(obj, action);
+            foreach (var keys in this.extraKeys.Values) keys.ForEach(obj, action);
+            this.values.ForEach(obj, action);
+        }
     }
     class MetaInfoBuilder<T> : MetaInfoBuilderBase<T>, IMetaInfo<T>
     {
         {
             this.extraKeys[group].ForEach(obj, action);
         }
-        void IMetaInfo<T>.ForEachInsertValue(T obj, Action<string, object> action)
-        {
-            this.keys.ForEach(obj, action);
-            foreach (var keys in this.extraKeys.Values) keys.ForEach(obj, action);
-            this.values.ForEach(obj, action);
-        }
         void IMetaInfo<T>.ForEachValueChange(T o, T n, Action<string, object, object> action)
         {
             this.values.ForEachChange(o, n, action);
             for (int i = 0; i < actualKey.Length; i++)
                 dict[this.refList[i]] = actualKey[i];
         }
+        void IMetaInfo<T>.ForEachInsertValue(T obj, Action<string, object> action)
+        {
+            this.ForEachInfoValue(obj, action);
+        }
     }
     public delegate void Meta<T>(IMetaBuilder<T> m);
     abstract class AbstractReconciler<OldT, NewT>
             }
         }
     }
-    public class Settler<T>
+    public class Settler<T> where T: class
     {
         private class GenericItemList
         {
-            private class InnerInfoBuilder : MetaInfoBuilderBase<T>, IMetaInfo<object[]>
+            private class MetaInfoAdapter : MetaInfoBuilderBase<T>
             {
-                private IList<string> extraKeysNames = null;
-                string IMetaInfo<object[]>.TableName 
+                public IMetaInfo<object[]> Adapt()
                 {
-                    get
+                    var names = new List<string>();
+                    var indices = new Dictionary<string, int>();
+                    this.ForEachInfoValue(null, (k, v) =>
                     {
-                        return this.tableName;
+                        if (! indices.ContainsKey(k))
+                        {
+                            indices.Add(k, names.Count);
+                            names.Add(k);
+                        }
+                    });
+                    Func<string, Func<object[], object>> createGetter = name =>
+                    {
+                        int i = indices[name];
+                        return array => array[i];
+                    };
+                    var b = new MetaInfoBuilder<object[]>() as IMetaBuilder<object[]>;
+                    b.Table(this.tableName);
+                    this.keys.ForEach(null, (k, v) =>
+                    {
+                        b.Key(k, createGetter(k), value => this.keys.GeneratorExpr(k, value));
+                    });
+                    foreach (var g in this.extraKeys.Keys)
+                    {
+                        var source = this.extraKeys[g];
+                        var target = b.ExtraKey(g);
+                        source.ForEach(null, (k, v) =>
+                        {
+                            target.Key(k, createGetter(k), value => source.GeneratorExpr(k, value));
+                        });
                     }
-                }
-                object[] IMetaInfo<object[]>.GetKey(object[] obj)
-                {
-                    return obj.Take(this.keys.Count).ToArray();
-                }
-                object[] IMetaInfo<object[]>.GetKey(IDictionary<string, object> dict)
-                {
-                    return this.keys.GetFrom(dict);
-                }
-                object[] IMetaInfo<object[]>.GetExtraKey(string group, object[] obj)
-                {
-                    int start = 0;
-                    foreach (var g in this.extraKeys.Keys.OrderBy(x => x))
+                    if (! String.IsNullOrEmpty(this.group)) b.MasterExtaKey(this.group);
+                    this.values.ForEach(null, (k, v) =>
                     {
-                    }
-                    return null;
-                }
-                object[] IMetaInfo<object[]>.GetExtraKey(string group, IDictionary<string, object> dict)
-                {
-                    return null;
-                }
-                string IMetaInfo<object[]>.GeneratorExpr(string name, object value)
-                {
-                    return null;
-                }
-                void IMetaInfo<object[]>.ForEachKey(object[] obj, Action<string, object> action)
-                {
-                }
-                void IMetaInfo<object[]>.ForEachExtraKey(string group, object[] obj, Action<string, object> action)
-                {
-                }
-                void IMetaInfo<object[]>.ForEachInsertValue(object[] obj, Action<string, object> action)
-                {
-                }
-                void IMetaInfo<object[]>.ForEachValueChange(object[] o, object[] n, Action<string, object, object> action)
-                {
-                }
-                string IMetaInfo<object[]>.MasterKeyGroup
-                {
-                    get
-                    {
-                        return null;
-                    }
-                }
-                object[] IMetaInfo<object[]>.GetMasterRef(object[] obj)
-                {
-                    return null;
-                }
-                void IMetaInfo<object[]>.SetActualMasterKey(IDictionary<string, object> dict, object[] actualKey)
-                {
+                        if (this.refList.Contains(k))
+                        {
+                            b.Ref(k, createGetter(k));
+                        }
+                        else
+                        {
+                            b.Value(k, createGetter(k));
+                        }
+                    });
+                    return b as IMetaInfo<object[]>;
                 }
             }
             private IMetaInfo<T> info = null;