diff --git "a/DotNetTest/10 \320\262\320\276\320\277\321\200\320\276\321\201.txt" "b/DotNetTest/10 \320\262\320\276\320\277\321\200\320\276\321\201.txt" new file mode 100644 index 0000000..2b6ced0 --- /dev/null +++ "b/DotNetTest/10 \320\262\320\276\320\277\321\200\320\276\321\201.txt" @@ -0,0 +1,10 @@ +Ложный агрегат - это большой сложный агрегат, который не отражает действительность и, скорее всего, имеет круговые зависимости. +Например Юзер имеет список Заказов, Комментов, Настроек и мы связываем эти коллекции в две стороны. +Кроме того, такой агрегат становится антипаттерном Божжественным объектом. +Ложный агрегат в реальном мире не существует. Например, скорее всего нет такого инварианта, что изменение одного Заказа, должно влиять на другие Заказы => нам не нужен список всех заказов у Юзера и тд. +Чтобы это исправить нужно изменить агрегат. Чтобы правильно определить корень агрегата, нужно ответить на следующие вопросы: +1) есть ли аналог в реальном мире? +2) не пересекает ли агрегат Bounded Context? +3) Есть ли инвариант для этой группы объектов? +4) Данные должны изменяться в рамках одной транзакции? +В нашем случае ответ на всё нет, поэтому нужно разделить этот агрегат на три маленьких, уменьшив количество циклических зависимостей. \ No newline at end of file diff --git a/DotNetTest/DotNetTest/ExampleCollection.cs b/DotNetTest/DotNetTest/ExampleCollection.cs index c77fb7a..269e2d6 100644 --- a/DotNetTest/DotNetTest/ExampleCollection.cs +++ b/DotNetTest/DotNetTest/ExampleCollection.cs @@ -1,10 +1,18 @@ -using System.Collections.Generic; +using System.Collections; +using System.Collections.Generic; namespace DotNetTest { - public class ExampleCollection + public class ExampleCollection : IteratorAggregate { private readonly List _collection = new List(); + + bool _direction = true; + + public void ReverseDirection() + { + _direction = !_direction; + } public int Length => _collection.Count; @@ -23,5 +31,10 @@ public string this[int index] get => _collection[index]; set => _collection[index] = value; } + + public override IEnumerator GetEnumerator() + { + return new AlphabeticalOrderIterator(this, _direction); + } } } \ No newline at end of file diff --git a/DotNetTest/DotNetTest/Iterator.cs b/DotNetTest/DotNetTest/Iterator.cs new file mode 100644 index 0000000..8fa3391 --- /dev/null +++ b/DotNetTest/DotNetTest/Iterator.cs @@ -0,0 +1,67 @@ +using System.Collections; + +namespace DotNetTest +{ + abstract class Iterator : IEnumerator + { + object IEnumerator.Current => Current(); + public abstract int Key(); + public abstract object Current(); + public abstract bool MoveNext(); + public abstract void Reset(); + } + + public abstract class IteratorAggregate : IEnumerable + { + public abstract IEnumerator GetEnumerator(); + } + + class AlphabeticalOrderIterator : Iterator + { + private ExampleCollection _collection; + + private int _position = -1; + + private bool _reverse = false; + + public AlphabeticalOrderIterator(ExampleCollection collection, bool reverse = false) + { + this._collection = collection; + this._reverse = reverse; + + if (reverse) + { + this._position = collection.GetItems().Count; + } + } + + public override object Current() + { + return this._collection.GetItems()[_position]; + } + + public override int Key() + { + return this._position; + } + + public override bool MoveNext() + { + var updatedPosition = this._position + (this._reverse ? -1 : 1); + if (updatedPosition >= 0 && updatedPosition < this._collection.GetItems().Count) + { + this._position = updatedPosition; + return true; + } + else + { + return false; + } + } + + public override void Reset() + { + this._position = this._reverse ? this._collection.GetItems().Count - 1 : 0; + } + } +} \ No newline at end of file diff --git a/DotNetTest/DotNetTest/Program.cs b/DotNetTest/DotNetTest/Program.cs index 350c57c..1979d30 100644 --- a/DotNetTest/DotNetTest/Program.cs +++ b/DotNetTest/DotNetTest/Program.cs @@ -6,25 +6,6 @@ namespace DotNetTest { class Program { - public static ExampleCollection AlphabeticalOrderSort(ExampleCollection collection) - { - string temp; - for (var i = 0; i < collection.Length; i++) - { - for (var j = i + 1; j < collection.Length; j++) - { - if (collection[i].ToCharArray()[0] > collection[j].ToCharArray()[0]) - { - temp = collection[i]; - collection[i] = collection[j]; - collection[j] = temp; - } - } - } - - return collection; - } - static void Main(string[] args) { var collection = new ExampleCollection(); @@ -32,7 +13,10 @@ static void Main(string[] args) collection.AddItem("bar"); collection.AddItem("a"); - var sorted = AlphabeticalOrderSort(collection); + foreach (var element in collection) + { + Console.WriteLine(element); + } } }