-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntityBuilder.cs
More file actions
352 lines (278 loc) · 9.26 KB
/
Copy pathEntityBuilder.cs
File metadata and controls
352 lines (278 loc) · 9.26 KB
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
void Main()
{
using (var scope = GetContainer().BeginLifetimeScope())
{
var builder = scope.Resolve<IEntityBuilder<IndividualPartner>>();
builder.Build().Dump();
}
}
IContainer GetContainer()
{
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(EntityBuilder<>)).AsImplementedInterfaces();
builder.RegisterType<Partners>().AsImplementedInterfaces();
builder.RegisterType<PartnersAddresses>().AsImplementedInterfaces();
builder.RegisterType<PartnersPhones>().AsImplementedInterfaces();
return builder.Build();
}
interface IEntityComponent<TResult>
{
IEnumerable<TResult> Execute(IEnumerable<TResult> seed);
}
interface IEntitySeed<TResult>
{
IEnumerable<TResult> Execute();
}
class Partners : IEntitySeed<IndividualPartner>
{
public IEnumerable<IndividualPartner> Execute()
{
return GetPartners().Select(p => new IndividualPartner(p, ImmutableArray.Create<AddressInformation>(), ImmutableArray.Create<PhoneNumber>(), 0));
}
private IEnumerable<IndividualPartnerInformation> GetPartners()
{
return new List<IndividualPartnerInformation>
{
new IndividualPartnerInformation(1, "Don", "", "", "W", "", "", "en-US", true),
new IndividualPartnerInformation(2, "Kyla", "", "", "W", "", "", "en-US", true),
new IndividualPartnerInformation(3, "Vern", "", "", "W", "", "", "en-US", true)
}.ToImmutableArray();
}
}
class PartnersAddresses : IEntityComponent<IndividualPartner>
{
public IEnumerable<IndividualPartner> Execute(IEnumerable<IndividualPartner> seed)
{
return seed.Joiner<IndividualPartner, AddressInformation, IndividualPartner>
((a, b) => new IndividualPartner(a, b.ToImmutableArray(), ImmutableArray<PhoneNumber>.Empty, 0))
(GetAddresses());
}
public IEnumerable<AddressInformation> GetAddresses()
{
return new List<AddressInformation>
{
new AddressInformation(1, 1, "9876", "", "City", "12345", "IA", 1, "Scott", "US", 0.0, 0.0),
new AddressInformation(2, 1, "1234", "", "Town", "54321", "IA", 1, "Polk", "US", 0.0, 0.0),
new AddressInformation(3, 1, "444", "", "Metropolis", "23456", "IA", 1, "Muscatine", "US", 0.0, 0.0)
}.ToImmutableArray();
}
}
class PartnersPhones : IEntityComponent<IndividualPartner>
{
public IEnumerable<IndividualPartner> Execute(IEnumerable<IndividualPartner> seed)
{
return seed.Joiner<IndividualPartner, PhoneNumber, IndividualPartner>
((a, b) => new IndividualPartner(a, a.Addresses, b.ToImmutableArray(), 0))
(GetPhones());
}
public IEnumerable<PhoneNumber> GetPhones()
{
return new List<PhoneNumber>
{
new PhoneNumber(1, 1, "1234567890", 1),
new PhoneNumber(1, 2, "5551231234", 1),
new PhoneNumber(2, 3, "5553214321", 1)
}.ToImmutableArray();
}
}
interface IEntityBuilder<TType>
{
IEnumerable<TType> Build();
}
class EntityBuilder<TType> : IEntityBuilder<TType>
{
private readonly IEntitySeed<TType> _seed;
private readonly IEnumerable<IEntityComponent<TType>> _components;
public EntityBuilder(
IEntitySeed<TType> seed,
IEnumerable<IEntityComponent<TType>> components)
{
_seed = seed;
_components = components;
}
public IEnumerable<TType> Build()
{
return this._components
.Aggregate(_seed.Execute(), (seed, curr) => curr.Execute(seed).ToList());
}
}
static class Extensions
{
public static Func<IEnumerable<TRight>, IEnumerable<TResult>>
Joiner<TLeft, TRight, TResult>(this IEnumerable<TLeft> left, Func<TLeft, IEnumerable<TRight>, TResult> selector)
where TLeft : IHasCustomerId
where TRight : IHasCustomerId
{
return b => left.GroupJoin(b, l => l.CustomerId, r => r.CustomerId, selector);
}
}
public interface IHasCustomerId
{
int CustomerId { get; }
}
public sealed class IndividualPartnerInformation : IHasCustomerId
{
private readonly int customerId;
public readonly string FirstName;
public readonly string PreferredName;
public readonly string MiddleName;
public readonly string LastName;
public readonly string Suffix;
public readonly string Email;
public readonly string Culture;
public readonly bool IsPrimaryDecisionMaker;
internal IndividualPartnerInformation(
int customerId,
string firstName,
string preferredName,
string middleName,
string lastName,
string suffix,
string email,
string culture,
bool isPrimaryDecisionMaker)
{
this.customerId = customerId;
this.FirstName = firstName;
this.PreferredName = preferredName;
this.MiddleName = middleName;
this.LastName = lastName;
this.Suffix = suffix;
this.Email = email;
this.Culture = culture;
this.IsPrimaryDecisionMaker = isPrimaryDecisionMaker;
}
public int CustomerId
{
get { return this.customerId; }
}
internal static IndividualPartnerInformation Empty
{
get
{
return new IndividualPartnerInformation(0, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, false);
}
}
}
public sealed class AddressInformation : IHasCustomerId
{
private readonly int customerId;
public readonly int AddressTypeId;
public readonly string AddressLine1;
public readonly string AddressLine2;
public readonly string City;
public readonly string RegionId;
public readonly string PostalCode;
public readonly int CountyId;
public readonly string County;
public readonly string CountryId;
public readonly double Latitude;
public readonly double Longitude;
internal AddressInformation(int pioneerId, int addressTypeId, string street1, string street2, string city, string postalCode, string regionId, int countyId, string county, string countryId, double latitude, double longitude)
{
this.customerId = pioneerId;
this.AddressTypeId = addressTypeId;
this.AddressLine1 = street1;
this.AddressLine2 = street2;
this.City = city;
this.PostalCode = postalCode;
this.RegionId = regionId;
this.CountyId = countyId;
this.County = county;
this.CountryId = countryId;
this.Latitude = latitude;
this.Longitude = longitude;
}
public int CustomerId
{
get
{
return this.customerId;
}
}
}
public sealed class PhoneNumber : IHasCustomerId
{
private readonly int customerId;
public readonly int PhoneNumberId;
public readonly string Number;
public readonly int PhoneNumberTypeId;
public PhoneNumber(int customerId, int phoneNumberId, string number, int phoneNumberTypeId)
{
this.customerId = customerId;
this.PhoneNumberId = phoneNumberId;
this.Number = number;
this.PhoneNumberTypeId = phoneNumberTypeId;
}
internal PhoneNumber(PhoneNumberInformation phoneNumberInformation)
{
this.customerId = phoneNumberInformation.CustomerId;
this.PhoneNumberId = phoneNumberInformation.PhoneNumberId;
this.Number = phoneNumberInformation.Number;
this.PhoneNumberTypeId = phoneNumberInformation.PhoneNumberTypeId;
}
public int CustomerId
{
get { return this.customerId; }
}
}
public sealed class PhoneNumberInformation : IHasCustomerId
{
public readonly int PhoneNumberId;
private readonly int customerId;
public readonly string Number;
public readonly int PhoneNumberTypeId;
public readonly bool IsPrimary;
internal PhoneNumberInformation(int phoneNumberId, int pioneerId, string phoneNumber, int phoneNumberTypeId, bool isPrimary)
{
this.PhoneNumberId = phoneNumberId;
this.customerId = pioneerId;
this.Number = phoneNumber;
this.PhoneNumberTypeId = phoneNumberTypeId;
this.IsPrimary = isPrimary;
}
public int CustomerId
{
get { return this.customerId; }
}
}
public sealed class IndividualPartner : IHasCustomerId
{
public int CustomerId
{
get { return this.customerId; }
}
private readonly int customerId;
public readonly string LastName;
public readonly string FirstName;
public readonly string MiddleName;
public readonly string PreferredName;
public readonly string Suffix;
public readonly string Email;
public readonly ImmutableArray<AddressInformation> Addresses;
public readonly ImmutableArray<PhoneNumber> PhoneNumbers;
public readonly string CultureName;
public readonly int PrimaryPhoneNumberId;
public IndividualPartner(int customerId, string lastName, string firstName, string middleName, string preferredName, string suffix, string email, ImmutableArray<AddressInformation> addresses, ImmutableArray<PhoneNumber> phoneNumbers, string cultureName, int primaryPhoneNumberId)
{
this.customerId = customerId;
this.LastName = lastName;
this.FirstName = firstName;
this.MiddleName = middleName;
this.PreferredName = preferredName;
this.Suffix = suffix;
this.Email = email;
this.Addresses = addresses;
this.PhoneNumbers = phoneNumbers;
this.CultureName = cultureName;
this.PrimaryPhoneNumberId = primaryPhoneNumberId;
}
public IndividualPartner(IndividualPartnerInformation partnerInfo, ImmutableArray<AddressInformation> addresses, ImmutableArray<PhoneNumber> phoneNumbers, int primaryPhoneNumberId)
: this(partnerInfo.CustomerId, partnerInfo.LastName, partnerInfo.FirstName, partnerInfo.MiddleName, partnerInfo.PreferredName, partnerInfo.Suffix, partnerInfo.Email, addresses, phoneNumbers, partnerInfo.Culture, primaryPhoneNumberId)
{
}
public IndividualPartner(IndividualPartner partnerInfo, ImmutableArray<AddressInformation> addresses, ImmutableArray<PhoneNumber> phoneNumbers, int primaryPhoneNumberId)
: this(partnerInfo.CustomerId, partnerInfo.LastName, partnerInfo.FirstName, partnerInfo.MiddleName, partnerInfo.PreferredName, partnerInfo.Suffix, partnerInfo.Email, addresses, phoneNumbers, partnerInfo.CultureName, primaryPhoneNumberId)
{
}
}