Commits

sake  committed f0af92d

test passed

  • Participants
  • Parent commits ec5fbf7

Comments (0)

Files changed (2)

File Usually/Resolve.cs

     {
         public static void Key<T>(this IKeyBuilder<T> m, string name, Func<T, object> getKey, string sequence, Func<object, bool> isDummy)
         {
-            m.Key(name, getKey, (x) => (isDummy(x) ? sequence : null));
+            m.Key(name, getKey, x => (isDummy(x) ? sequence : null));
         }
         public static void IntegerKey<T>(this IKeyBuilder<T> m, string name, Func<T, int?> getKey, string sequence, Func<int, bool> isDummy)
         {
-            m.Key(name, (x) => getKey(x), sequence, (x) => isDummy((int)x));
+            m.Key(name, x => getKey(x), sequence, x => isDummy((int)x));
         }
         public static void IntegerKey<T>(this IKeyBuilder<T> m, string name, Func<T, int?> getKey, string sequence, int dummy = Resolver.DEFAULT_DUMMY_START)
         {
-            m.IntegerKey(name, getKey, sequence, (x) => x <= dummy);
+            m.IntegerKey(name, getKey, sequence, x => x <= dummy);
         }
         public static void DecimalKey<T>(this IKeyBuilder<T> m, string name, Func<T, decimal?> getKey, string sequence, Func<decimal, bool> isDummy)
         {
-            m.Key(name, (x) => getKey(x), sequence, (x) => isDummy((decimal)x));
+            m.Key(name, x => getKey(x), sequence, x => isDummy((decimal)x));
         }
         public static void DecimalKey<T>(this IKeyBuilder<T> m, string name, Func<T, decimal?> getKey, string sequence, decimal dummy = Resolver.DEFAULT_DUMMY_START)
         {
-            m.DecimalKey(name, getKey, sequence, (x) => x <= dummy);
+            m.DecimalKey(name, getKey, sequence, x => x <= dummy);
         }
         public static void StringKey<T>(this IKeyBuilder<T> m, string name, Func<T, object> getKey, string sequence, Func<string, bool> isDummy)
         {
-            m.Key(name, getKey, sequence, (x) => isDummy(x as string));
+            m.Key(name, getKey, sequence, x => isDummy(x as string));
         }
         public static void StringKey<T>(this IKeyBuilder<T> m, string name, Func<T, object> getKey, string sequence, string prefix = "new:")
         {
-            m.StringKey(name, getKey, sequence, (s) => s.StartsWith(prefix));
+            m.StringKey(name, getKey, sequence, s => s.StartsWith(prefix));
         }
     }
     public static class IMetaBuilderExtension
     {
         public static void ExtraKey<T>(this IMetaBuilder<T> m, string group, string name, Func<T, object> getKey, string sequence, Func<object, bool> isDummy)
         {
-            m.ExtraKey(group, name, getKey, (x) => (isDummy(x) ? sequence : null));
+            m.ExtraKey(group, name, getKey, x => (isDummy(x) ? sequence : null));
         }
         public static void ExtraIntegerKey<T>(this IMetaBuilder<T> m, string group, string name, Func<T, int?> getKey, string sequence, Func<int, bool> isDummy)
         {
-            m.ExtraKey(group, name, (x) => getKey(x), sequence, (x) => isDummy((int)x));
+            m.ExtraKey(group, name, x => getKey(x), sequence, x => isDummy((int)x));
         }
         public static void ExtraIntegerKey<T>(this IMetaBuilder<T> m, string group, string name, Func<T, int?> getKey, string sequence, int dummy = Resolver.DEFAULT_DUMMY_START)
         {
-            m.ExtraIntegerKey(group, name, getKey, sequence, (x) => x <= dummy);
+            m.ExtraIntegerKey(group, name, getKey, sequence, x => x <= dummy);
         }
         public static void ExtraDecimalKey<T>(this IMetaBuilder<T> m, string group, string name, Func<T, decimal?> getKey, string sequence, Func<decimal, bool> isDummy)
         {
-            m.ExtraKey(group, name, (x) => getKey(x), sequence, (x) => isDummy((decimal)x));
+            m.ExtraKey(group, name, x => getKey(x), sequence, x => isDummy((decimal)x));
         }
         public static void ExtraDecimalKey<T>(this IMetaBuilder<T> m, string group, string name, Func<T, decimal?> getKey, string sequence, decimal dummy = Resolver.DEFAULT_DUMMY_START)
         {
-            m.ExtraDecimalKey(group, name, getKey, sequence, (x) => x <= dummy);
+            m.ExtraDecimalKey(group, name, getKey, sequence, x => x <= dummy);
         }
         public static void ExtraStringKey<T>(this IMetaBuilder<T> m, string group, string name, Func<T, object> getKey, string sequence, Func<string, bool> isDummy)
         {
-            m.ExtraKey(group, name, getKey, sequence, (x) => isDummy(x as string));
+            m.ExtraKey(group, name, getKey, sequence, x => isDummy(x as string));
         }
         public static void ExtraStringKey<T>(this IMetaBuilder<T> m, string group, string name, Func<T, object> getKey, string sequence, string prefix = "new:")
         {
-            m.ExtraStringKey(group, name, getKey, sequence, (s) => s.StartsWith(prefix));
+            m.ExtraStringKey(group, name, getKey, sequence, s => s.StartsWith(prefix));
         }
     }
     class ColumnsDef<T>
             this.info.ForEachValueChange(o, n, action);
         }
     }
-    public class BaseResolverItem
-    {
-    }
     public class Resolver
     {
         public const int DEFAULT_DUMMY_START = -1;
         }
         public IEnumerable<TransactionRecord> Resolve()
         {
-            return this.items.SelectMany((i) => i.ResolveForRoot());
+            return this.items.SelectMany(i => i.ResolveForRoot());
         }
         public static IEnumerable<TransactionRecord> Resolve<T>(Meta<T> meta, IEnumerable<T> olds, IEnumerable<T> news) 
         {
                 foreach (var g in @info.GetExtraKeyGroups())
                 {
                     var @xkeys = new List<string>();
-                    this.specific.ForEachExtraKey(g, null, (k, v) =>
+                    @info.ForEachExtraKey(g, null, (k, v) =>
                     {
                         @indices.Add(k, i++);
                         @xkeys.Add(k);
     }
     public class Settler
     {
-        private Func<string, object> generator = null;
-        private IList<ISettlerItem> items = new List<ISettlerItem>();
-        private IList<ISettlerItem> roots = new List<ISettlerItem>();
         public interface ISettlerItem
         {
             string TableName { get; }
             {
                 get { return this.info.MasterKeyGroup; }
             }
-            public ISettlerItem Add(string name, Meta<T> meta)
+            public SettlerItem<TT> Add<TT>(Meta<TT> meta) where TT: class
             {
-                this.host.CheckBeforeAdd(name);
-                var r = new SettlerItem<T>(this.host, meta);
+                var r = new SettlerItem<TT>(this.host, meta);
+                this.host.CheckBeforeAdd((r as ISettlerItem).TableName);
                 this.items.Add(r);
                 return r;
             }
                 }
             }
         }
+        private Func<string, object> generator = null;
+        private IList<string> tables = new List<string>();
+        private IList<ISettlerItem> roots = new List<ISettlerItem>();
         public Settler(Func<string, object> generator)
         {
             this.generator = generator;
         }
         void CheckBeforeAdd(string name)
         {
-            if (this.items.Any(i => (i.TableName == name))) throw new Exception("Duplicate item: " + name);
+            if (this.tables.Contains(name)) throw new Exception("Duplicate item: " + name);
+            this.tables.Add(name);
         }
-        public SettlerItem<T> Add<T>(string name, Meta<T> meta) where T: class
+        public SettlerItem<T> Add<T>(Meta<T> meta) where T: class
         {
-            this.CheckBeforeAdd(name);
             var r = new SettlerItem<T>(this, meta);
+            this.CheckBeforeAdd((r as ISettlerItem).TableName);
             this.roots.Add(r);
             return r;
         }
         }
         public IEnumerable<TransactionRecord> Resolve()
         {
-            return this.items.SelectMany((i) => i.ResolveForRoot());
+            return this.roots.SelectMany(i => i.ResolveForRoot());
         }
     }
 }

File UsuallyTests/TestResolve.cs

             ).Add(Shipment.Meta, this.oldShips, this.newShips);
             return r;
         }
+        private Settler CreateSettler()
+        {
+            var s = new Settler(CreateGenerator());
+            s.Add<Order>(Order.Meta
+            ).Add<OrderLine>(OrderLine.Meta
+            ).Add<Shipment>(Shipment.Meta);
+            return s;
+        }
         [Test]
         public void TestNoChange()
         {
             var r = this.CreateResolver();
             Assert.AreEqual(0, r.Resolve().Count());
         }
-        [Test]
-        public void TestChange()
+        private void DoChange()
         {
             this.newShips.RemoveAll((x) => x.ShipmentId == 10201);   // 1 deleted
             this.newShips.RemoveAll((x) => x.ShipmentId == 10303);   // 1 deleted
                 ln.Quantity = 12345;
             foreach (var sh in (from sh in this.newShips where sh.OrderLineId == 103 select sh))  // 3 updated
                 sh.Quantity = 54321;
-            var changes = this.CreateResolver().Resolve().ToList();
+        }
+        private void DoCheck(IList<TransactionRecord> changes)
+        {
             Assert.AreEqual(24, changes.Count);
             var e = changes.GetEnumerator() as IEnumerator<TransactionRecord>;
-            AssertNextUpdate(e, "ORDERS", "ORDER_ID", 1, (d) => 
-            { 
-                d["STATUS"] = "ARCHIVED"; 
+            AssertNextUpdate(e, "ORDERS", "ORDER_ID", 1, (d) =>
+            {
+                d["STATUS"] = "ARCHIVED";
             });
             AssertNextUpdate(e, "ORDER_SHIPMENTS", "SHIPMENT_ID", 10301, (d) =>
             {
             AssertNextDelete(e, "ORDERS", "ORDER_ID", 3);
             Assert.False(e.MoveNext());
         }
+        [Test]
+        public void TestChange()
+        {
+            this.DoChange();
+            this.DoCheck(this.CreateResolver().Resolve().ToList());
+        }
+        [Test]
+        public void TestSettleChange()
+        {
+            var s = this.CreateSettler();
+            s.Load(k =>
+            {
+                if (k == "ORDERS") return this.newOrders;
+                if (k == "ORDER_LINES") return this.newLines;
+                if (k == "ORDER_SHIPMENTS") return this.newShips;
+                throw new Exception("Unknown key: " + k);
+            });
+            this.DoChange();
+            this.DoCheck(s.Resolve().ToList());
+        }
     }
 }