-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
378 lines (342 loc) · 9.23 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectLiteral
{
public class Program
{
static void Main(string[] args)
{
Order order = GetOrder();
string result = ObjectToObjectLiteral.ToObjectInitializer
(
order,
globalExcludeProperties: "Description",
entityRestrictions: new Restriction[0]
);
WriteToFile("test.txt", result);
Console.WriteLine(result);
// Poor man's unit test, paste results into
// GetOrderFromObjectListeral()
if( CompareOrders( order, GetOrderFromObjectLiteral()) )
{
Console.WriteLine( "Object Literal matches original");
}
else
{
Console.WriteLine( "Error: Object Literal does not match");
}
Console.ReadLine();
}
private static void WriteToFile(string fileName, string content)
{
System.IO.File.WriteAllText(fileName, content);
}
private static bool CompareOrders( Order lhs, Order rhs )
{
var itemDifferences = lhs.Items.Except(rhs.Items,new ItemEqualityComparer());
if( itemDifferences.Any())
return false ;
if( rhs.Items[0].Product.Category.Departments[0].Description != null )
{
return false ; // Property should have been skipped
}
return lhs.Key == rhs.Key &&
lhs.Items.Count == rhs.Items.Count &&
lhs.Address.Key == rhs.Address.Key ;
}
public static Order GetOrder()
{
Customer customer = new Customer
{
Key = 1,
FirstName = "Mortimer",
LastName = "Sneurd",
Timestamp = "3"
};
Address homeAddress = new Address
{
Key = 0,
Customer = customer,
Street = "1313 Mockingbird Lane",
City="Candy Land",
AddressType = AddressType.Residential,
ZipCode= 90210,
Timestamp="1"
};
Address businessAddress = new Address
{
Key = 1,
Customer = customer,
Street = "411 Bonham",
City = "Sao Antonio",
AddressType = AddressType.Business,
ZipCode = 66016,
Timestamp = "2"
};
customer.Addresses = new List<Address> { homeAddress, businessAddress };
Department department1 = new Department { Key = 0, Name = "Electronics", Timestamp ="4" };
Department department2 = new Department { Key = 1, Name = "Computers", Timestamp = "5" };
Department department3 = new Department { Key = 2, Name = "Music", Timestamp = "6" };
Department[] batteryDepartments =
new Department[] { department1, department2 };
Category category1 = new Category
{
Key = 0,
Name = "Batteries",
SortOrder = 1,
Departments = batteryDepartments,
Timestamp = "7"
};
Category category2 = new Category
{
Key = 2,
Name = "Sheet Music",
SortOrder = 2,
Departments = new Department[2] { department2, department3 },
Timestamp = "8"
};
Product product1 = new Product { Key = 0, Name = "Everready", Category = category1, Timestamp = "9" };
Product product2 = new Product { Key = 1, Name = "Shroeder's Theme", Category = category2, Timestamp = "10" };
// Add a parent ref to
Item item1 = new Item { Key = 0, Price = 13.13F, Quantity = 6, Product = product1, Timestamp = "11" };
Item item2 = new Item { Key = 1, Price = 15.00F, Quantity = 1, Product = product2, Timestamp = "12" };
Item item3 = new Item { Key = 2, Price = 5.00F, Quantity = 1, Product = product1, Timestamp = "13" };
Order order = new Order
{
Key = 1,
Name = "Business Order #1",
Customer = customer,
Address = businessAddress,
Items = new List<Item>{ item1, item2, item3},
SentDate = null,
Hold = true,
Coupons = new List<String> { "Summer", "Fall"},
Alerts = new long[] { 1234, 5678 },
ContactTimes = new DateTime[] { new DateTime(2013,12,25,09,30,0), new DateTime(2014,1,1,10,0,0)},
Timestamp = "14"//,
//Chars = new List<char?> { 'a', null, 'b', 'c' } // Not supported
};
item1.Order = order;
item2.Order = order;
item3.Order = order;
return order;
}
/// <summary>
/// Paste the output of the Object Literal Serializer here
/// </summary>
/// <returns></returns>
private static Order GetOrderFromObjectLiteral()
{
// Object Literal
// Globally Excluded Properties:
// Description
var Order1 = new Order
{
Name = "Business Order #1",
SentDate = null,
Hold = true,
Key = 1L,
Timestamp = "14"
};
Order1.Customer = new Customer
{
FirstName = "Mortimer",
LastName = "Sneurd",
Key = 1L,
Timestamp = "3"
};
var Address0 = new Address
{
Street = "1313 Mockingbird Lane",
City = "Candy Land",
AddressType = AddressType.Residential,
ZipCode = 90210,
Key = 0L,
Timestamp = "1"
};
Address0.Customer = Order1.Customer;
var Address1 = new Address
{
Key = 1L,
Street = "411 Bonham",
City = "Sao Antonio",
AddressType = AddressType.Business,
ZipCode = 66016,
Timestamp = "2"
};
Address1.Customer = Order1.Customer;
Order1.Customer.Addresses = new List<Address> { Address0, Address1 };
Order1.Address = Address1;
var Item0 = new Item
{
Quantity = 6,
Price = 13.13F,
Key = 0L,
Timestamp = "11"
};
Item0.Order = Order1;
Item0.Product = new Product
{
Name = "Everready",
Key = 0L,
Timestamp = "9"
};
Item0.Product.Category = new Category
{
Name = "Batteries",
SortOrder = 1,
Key = 0L,
Timestamp = "7"
};
var Department0 = new Department
{
Name = "Electronics",
Key = 0L,
Timestamp = "4"
};
var Department1 = new Department
{
Key = 1L,
Name = "Computers",
Timestamp = "5"
};
Item0.Product.Category.Departments = new Department[] { Department0, Department1 };
var Item1 = new Item
{
Key = 1L,
Quantity = 1,
Price = 15F,
Timestamp = "12"
};
Item1.Order = Order1;
Item1.Product = new Product
{
Name = "Shroeder's Theme",
Key = 1L,
Timestamp = "10"
};
Item1.Product.Category = new Category
{
Name = "Sheet Music",
SortOrder = 2,
Key = 2L,
Timestamp = "8"
};
var Department2 = new Department
{
Key = 2L,
Name = "Music",
Timestamp = "6"
};
Item1.Product.Category.Departments = new Department[] { Department1, Department2 };
var Item2 = new Item
{
Key = 2L,
Quantity = 1,
Price = 5F,
Timestamp = "13"
};
Item2.Order = Order1;
Item2.Product = Item0.Product;
Order1.Items = new List<Item> { Item0, Item1, Item2 };
Order1.Alerts = new Int64[] { 1234L, 5678L };
Order1.ContactTimes = new DateTime[] { new DateTime(2013, 12, 25, 9, 30, 0), new DateTime(2014, 1, 1, 10, 0, 0) };
Order1.Coupons = new List<String> { "Summer", "Fall" };
return Order1;
}
}
/***********************/
/* Test Classes */
/***********************/
public class PersistantObject
{
public long Key { get; set; }
public string Timestamp { get; set;}
}
public enum AddressType { Business, Residential };
public class Address : PersistantObject
{
public Customer Customer { get; set; }
public string Street { get; set;}
public string City { get; set; }
public AddressType AddressType { get; set;}
public int ZipCode { get; set; }
}
public class Customer : PersistantObject
{
public string FirstName { get; set;}
public string LastName { get;set;}
public List<Address> Addresses {get; set;}
}
public class Department : PersistantObject
{
public string Name { get; set; }
public string Description { get; set;}
}
public class Category : PersistantObject
{
public string Name { get; set; }
public short SortOrder { get; set; }
public Department[] Departments { get; set; }
}
public class Product : PersistantObject
{
public string Name { get; set; }
public Category Category { get; set; }
}
public class ProductEqualityComparer : IEqualityComparer<Product>
{
public bool Equals(Product lhs, Product rhs)
{
return lhs.Key == rhs.Key &&
lhs.Timestamp == rhs.Timestamp &&
lhs.Name.Equals( rhs.Name ) &&
lhs.Category.Key == rhs.Category.Key &&
lhs.Category.Departments.Length == rhs.Category.Departments.Length;
}
public int GetHashCode(Product product)
{
return product.Key.GetHashCode();
}
}
public class Item : PersistantObject
{
public Order Order { get; set; }
public int Quantity { get; set; }
public float Price { get; set; }
public Product Product { get; set; }
}
public class ItemEqualityComparer : IEqualityComparer<Item>
{
public bool Equals( Item lhs, Item rhs )
{
var productEqualityComparer = new ProductEqualityComparer();
return lhs.Key == rhs.Key &&
lhs.Price == rhs.Price &&
lhs.Order.Key == rhs.Order.Key &&
lhs.Quantity == rhs.Quantity &&
lhs.Timestamp == rhs.Timestamp &&
productEqualityComparer.Equals(lhs.Product,rhs.Product);
}
public int GetHashCode( Item item )
{
return item.Key.GetHashCode();
}
}
public class Order : PersistantObject
{
public string Name { get; set; }
public Customer Customer { get; set; }
public Address Address { get; set; }
public List<Item> Items { get; set; }
public Nullable<DateTime> SentDate { get; set; }
public long[] Alerts { get; set; }
public DateTime[] ContactTimes { get; set; }
public List<String> Coupons { get; set; }
public bool Hold { get; set; }
// public List<char?> Chars { get; set; } Not Supported
}
}