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
    public class AssetContainer : IAddAsset
    {

        public AssetContainer()
        {

        }

        public string FamilyName { get; set; }

        public IList<Asset> Assets { get; set; }

        public void AddAsset(Asset asset)
        {
            if (Assets == null)
                Assets = new List<Asset>();
            Assets.Add(asset);
        }


        public void SetFamilyName(string familyName)
        {
            this.FamilyName = familyName;
        }
    }

    public interface IAddAsset
    {
        void AddAsset(Asset asset);

        void SetFamilyName(string familyName);
    }


    public class AddAssetContext
    {
        public AddAssetContext Bind()
        {
            IAddAsset = new AssetContainer();
            Asset = new Asset { ClassificationA = "A", ClassificationB = "B" };
            Families = new List<Family> { new Family { Name = "1", ClassificationA = "A1", ClassificationB = "B1" }, 
                new Family { Name="2", ClassificationA = "A", ClassificationB = "B" } };
            return this;
        }

        public IAddAsset IAddAsset { get; set; }

        public IList<Family> Families { get; set; }

        public Asset Asset { get; set; }

        public AddAssetContext AddAsset()
        {
            IAddAsset.AddAssetWithSameFamily(Families, Asset);
            return this;
        }

        public void Persist()
        {
        }
    }

    public static partial class AssetContainerRole
    {
        public static void AddAssetWithSameFamily(this IAddAsset addAsset, IList<Family> families, Asset asset)
        {
            var family = families.FindAssetFamily(asset);
            asset.SetFamilyName(family.Name);
            addAsset.SetFamilyName(family.Name);
            addAsset.AddAsset(asset);
        }
    }

    public static class FamiliyRole
    {
        public static Family FindAssetFamily(this IList<Family> families, Asset asset)
        {
            foreach (var family in families)
            {
                if (asset.ClassificationA == family.ClassificationA && asset.ClassificationB == family.ClassificationB)
                {
                    return family;
                }
            }
            return null;
        }
    }

//===============================================================================================//
        [TestMethod]
        public void AddAsset()
        {
            AddAssetContext ctx = new AddAssetContext();
            ctx.Bind().AddAsset();

            var assetContainer = ((AssetContainer)ctx.IAddAsset);

            Assert.AreEqual(1, assetContainer.Assets.Count);

            Assert.AreEqual("2", assetContainer.FamilyName);
        }