1. Sakesun Roykiattisak
  2. Usually

Commits

sake  committed 6a7f0ce

start working on Resolver

  • Participants
  • Parent commits a439c7f
  • Branches default

Comments (0)

Files changed (2)

File Usually/Data.cs

View file
             }
             return userEntry;
         }
+        public static T AddNewWithIntegerKey<T>(List<T> list, Func<T, int> getter, Action<T, int> setter, int dummy = -1) where T: new()
+        {
+            var next = list.Any()
+                ? Math.Min(dummy, (from t in list select getter(t)).Min() - 1)
+                : dummy;
+            var r = new T();
+            setter(r, next);
+            list.Add(r);
+            return r;
+        }
+        public static T AddNewWithDecimalKey<T>(List<T> list, Func<T, int> getter, Action<T, int> setter, int dummy = -1) where T : new()
+        {
+            var next = list.Any()
+                ? Math.Min(dummy, (from t in list select getter(t)).Min() - 1)
+                : dummy;
+            var r = new T();
+            setter(r, next);
+            list.Add(r);
+            return r;
+        }
         public static int GetNextIntKeyByMax(IDbTransaction trans, string table, string key)
         {
             using (var cmd = CreateCommand(trans))
             return r.Reconcile(olds, news);
         }
     }
-    public abstract class ResolverItem
+    public interface IResolverItem
     {
-        public abstract ResolverItem Add<T>(Meta<T> meta, IEnumerable<T> t, object masterKeyGetter, object detailKeyGetter);
     }
-    public abstract class Resolver : ResolverItem
+    public class ResolverItem<T>: IResolverItem
     {
-        public abstract ResolverItem Add<T>(Meta<T> meta, IEnumerable<T> olds, IEnumerable<T> news);
+        public ResolverItem(Meta<T> meta, IEnumerable<T> olds, IEnumerable<T> news)
+        {
+        }
+        public ResolverItem<TT> Add<TT>(Meta<TT> meta, Func<T, object> masterKey, Func<TT, object> detailKey, IEnumerable<TT> olds, IEnumerable<TT> news)
+        {
+            return new ResolverItem<TT>(meta, olds, news);
+        }
+    }
+    public class Resolver
+    {
+        private List<IResolverItem> items = new List<IResolverItem>();
+        public Resolver()
+        {
+        }
+        public ResolverItem<T> Add<T>(Meta<T> meta, IEnumerable<T> olds, IEnumerable<T> news)
+        {
+            return new ResolverItem<T>(meta, olds, news);
+        }
     }
     public abstract class TransactionRecord
     {

File UsuallyTests/TestData.cs

View file
             Assert.AreEqual("Hello", Data.CleanUserEntry(" Hello  "));
         }
         [Test]
+        public void TestAddNewIntegerKey()
+        {
+            var list = new List<Order>();
+            var o1 = Data.AddNewWithIntegerKey(list, (o) => o.OrderId.Value, (o, v) => o.OrderId = v);
+            Assert.AreEqual(1, list.Count);
+            Assert.AreEqual(-1, o1.OrderId);
+            var o2 = Data.AddNewWithIntegerKey(list, (o) => o.OrderId.Value, (o, v) => o.OrderId = v);
+            Assert.AreEqual(2, list.Count);
+            Assert.AreEqual(-2, o2.OrderId);
+            var o3 = Data.AddNewWithIntegerKey(list, (o) => o.OrderId.Value, (o, v) => o.OrderId = v);
+            Assert.AreEqual(3, list.Count);
+            Assert.AreEqual(-3, o3.OrderId);
+        }
+        [Test]
         public void TestReconcile()
         {
             var olds = new OrderLine[] {
                 new OrderLine { LineId=7, OrderId=101, ItemId=1007, ItemName="Thousand Seven", Quantity=4, Unit="Litre" },
             }.ToList();
             var news = Use.SerializeClone(olds);
-            Meta<OrderLine> meta = (m) =>
-            {
-                m.Table("ORDER_LINES");
-                m.IntKey("LINE_ID", (x) => x.LineId, "SEQ_ORDER_LINES");
-                m.Value("ORDER_ID", (x) => x.OrderId);
-                m.Value("ITEM_ID", (x) => x.ItemId);
-                m.Value("ITEM_NAME", (x) => x.ItemName);
-                m.Value("QUANTITY", (x) => x.Quantity);
-                m.Value("UNIT", (x) => x.Unit);
-            };
             news[2 - 1].ItemId = 2002;
             news[2 - 1].ItemName = "Two Oo Oo Two";
             news[5 - 1].Quantity = null;
             news.Add(new OrderLine { LineId = -1, OrderId = 101, ItemId = 8008, ItemName = "Eight Thousand Eight", Quantity = 8, Unit = "Piece" });
             news.Add(new OrderLine { LineId = 9, OrderId = 101, ItemId = 9009, ItemName = "Nine Thousand Nine", Quantity = 9, Unit = "Square Metre" });
             Assert.AreEqual(8, news.Count);
-            var changes = Data.Reconcile(meta, olds, news);
+            var changes = Data.Reconcile(OrderLine.Meta, olds, news);
             Assert.AreEqual(8, changes.Count());
             var e = changes.GetEnumerator();
             Assert.True(e.MoveNext());
             }
             Assert.False(e.MoveNext());
         }
+        [Test]
+        public void TestResolver()
+        {
+            var r = new Resolver();
+            var orders = new List<Order>();
+            var lines = new List<OrderLine>();
+            var order = r.Add(Order.Meta, orders, orders);
+            var line = order.Add(OrderLine.Meta, (x) => x.OrderId, (x) => x.OrderId, lines, lines);
+        }
     }
 
     [Serializable]
         public int? CustomerId { get; set; }
         public DateTime? OrderDate { get; set; }
         public string Status { get; set; }
+        public static void Meta(IMetaBuilder<Order> m)
+        {
+            m.Table("ORDERS");
+            m.IntKey("ORDER_ID", (x) => x.OrderId, "SEQ_ORDERS");
+            m.Value("ORDER_NUMBER", (x) => x.OrderNumber);
+            m.Value("CUSTOMER_ID", (x) => x.CustomerId);
+            m.Value("ORDER_DATE", (x) => x.OrderDate);
+            m.Value("STATUS", (x) => x.Status);
+        }
     }
 
     [Serializable]
         public string ItemName { get; set; }
         public string Unit { get; set; }
         public decimal? Quantity { get; set; }
+        public static void Meta(IMetaBuilder<OrderLine> m)
+        {
+            m.Table("ORDER_LINES");
+            m.IntKey("LINE_ID", (x) => x.LineId, "SEQ_ORDER_LINES");
+            m.Value("ORDER_ID", (x) => x.OrderId);
+            m.Value("ITEM_ID", (x) => x.ItemId);
+            m.Value("ITEM_NAME", (x) => x.ItemName);
+            m.Value("QUANTITY", (x) => x.Quantity);
+            m.Value("UNIT", (x) => x.Unit);
+        }
     }
 
     [Serializable]
         public int? OrderLineId { get; set; }
         public decimal? Quantity { get; set; }
         public DateTime? ShipDate { get; set; }
+        public static void Meta(IMetaBuilder<Shipment> m)
+        {
+            m.Table("ORDER_SHIPMENTS");
+            m.IntKey("SHIPMENT_ID", (x) => x.ShipmentId, "SEQ_ORDER_SHIPMENTS");
+            m.Value("ORDER_LINE_ID", (x) => x.OrderLineId);
+            m.Value("QUANTITY", (x) => x.Quantity);
+            m.Value("SHIP_DATE", (x) => x.ShipDate);
+        }
     }
 
 }