-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsharp_complex.cs
More file actions
85 lines (70 loc) · 2.33 KB
/
csharp_complex.cs
File metadata and controls
85 lines (70 loc) · 2.33 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
// Repositories
public interface IRepository { }
public interface IXmlRepository : IRepository {
XmlDocument GetXml();
}
// Repositories.Impl
public abstract class BillingXmlRepository : IXmlRepository {
protected long quoteID;
public BillingXmlRepository(long quoteID) {
this.quoteID = quoteID;
}
public abstract XmlDocument GetXml();
}
public sealed class XmlInvoiceRepository : BillingXmlRepository {
public XmlInvoiceRepository(long quoteID) : base(quoteID) { }
public override XmlDocument GetXml() {
// XML retrival here...
}
}
// DataAggregation
pubilc interface IAggregatableData<T> {
T GetData();
}
public abstract class DataAggregator<T> : IAggregatableData<T> {
public abstract T GetData();
protected abstract ICollection<IRepository> GetAllRequiredRepositories();
}
public abstract class XmlDataAggregator : DataAggregator<XmlDocument> {
public override XmlDocument GetData() {
XmlDocument root = new XmlDocument();
foreach (IRepository repository in this.GetAllRequiredRepositories()) {
XmlDocument xml = (repository as IXmlRepository).GetXml();
XmlDocumentFragment fragment = root.CreateDocumentFragment();
fragment.InnerXml = xml.InnerXml;
root.DocumentElement.AppendChild(fragment);
}
return root;
}
}
public sealed class BillingDataAggregator : XmlDataAggregator {
private long quoteID;
public BillingDataAggregator(long quoteID) {
this.quoteID = quoteID;
}
protected override void ICollection<IRepository> GetAllRequiredRepositories() {
return new List<IRepository> {
new XmlBillingRepository(this.quoteID);
}
}
}
// Mappers to map raw XML to classes
public interface IMappable<T, K> {
T Map(K rawData);
}
public interface IXmlMappable<T> : IMappable<T, XmlDocument> {
}
public sealed class BillingXmlMapper : IXmlMappable<BillingInfo> {
public BillingInfo Map(XmlDocument rawData) {
// LINQ code to traverse the XML and map it into a BillingInfo DTO
}
}
// Entities
public sealed class BillingInfo {
// simple properties
}
// Consumer example
XmlDataAggregator aggregator = new BillingDataAggregator(1234);
var xml = aggregator.GetData();
var mapper = new BillingXmlMapper();
BillingInfo info = mapper.Map(xml);