-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyInversionPrinciple.cs
More file actions
103 lines (86 loc) · 2.97 KB
/
Copy pathDependencyInversionPrinciple.cs
File metadata and controls
103 lines (86 loc) · 2.97 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
//uses value tuples from C# 7 so you many need to install that package to your visual studio
#region Principle Description
// Using a geneology database example we can define different levels of abstraction
//
//
//
#endregion
namespace DesignPrinciples
{
public enum Relationship
{
Parent,
Child,
Sibling
}
//High level
public class Person
{
public string Name;
//public DateTime DateOfBirth etc..
}
//To prevent Low level data from being exposed create an interface that doesn't access the data directly
public interface IRelationshpBrowser
{
IEnumerable<Person> FindAllChildrenOf(string name);
}
//Low level API using an abstraction that is the IRelationship Browser
public class Relationships: IRelationshpBrowser
{
private List<(Person, Relationship, Person)> relations
= new List<(Person, Relationship, Person)>();
public void AddParentAndChild(Person parent, Person child)
{
relations.Add((parent, Relationship.Parent, child));
relations.Add((child, Relationship.Child, parent));
}
public IEnumerable<Person> FindAllChildrenOf(string name)
{
foreach (var r in relations.Where(
x => x.Item1.Name == name &&
x.Item2 == Relationship.Parent))
{
yield return r.Item3;
}
}
// public List<(Person, Relationship, Person)> Relations => relations; *BAD
}
public class DependencyInversionPrinciple
{
//public Research(Relationships relationships) *BAD
//{
// var relations = relationships.Relations;
// foreach (var r in relations.Where(
// x => x.Item1.Name == "John" &&
// x.Item2 == Relationship.Parent))
// {
// WriteLine($"John has a child called {r.Item3.Name}");
// }
//}
public DependencyInversionPrinciple(IRelationshpBrowser browser)
{
foreach (var p in browser.FindAllChildrenOf("John"))
WriteLine($"John has a child caller {p.Name}");
}
static void Main(string[] args)
{
//This way exposes low level data and private data as public and high level (accessible) which is not a good thing
var parent = new Person { Name = "John"};
var child1 = new Person { Name = "Betsy" };
var child2 = new Person { Name = "Bo" };
var relationships = new Relationships();
relationships.AddParentAndChild(parent, child1);
relationships.AddParentAndChild(parent, child2);
new DependencyInversionPrinciple(relationships);
}
}
}