Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions DotNetTest/10 вопрос.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Ложный агрегат - это большой сложный агрегат, который не отражает действительность и, скорее всего, имеет круговые зависимости.
Например Юзер имеет список Заказов, Комментов, Настроек и мы связываем эти коллекции в две стороны.
Кроме того, такой агрегат становится антипаттерном Божжественным объектом.
Ложный агрегат в реальном мире не существует. Например, скорее всего нет такого инварианта, что изменение одного Заказа, должно влиять на другие Заказы => нам не нужен список всех заказов у Юзера и тд.
Чтобы это исправить нужно изменить агрегат. Чтобы правильно определить корень агрегата, нужно ответить на следующие вопросы:
1) есть ли аналог в реальном мире?
2) не пересекает ли агрегат Bounded Context?
3) Есть ли инвариант для этой группы объектов?
4) Данные должны изменяться в рамках одной транзакции?
В нашем случае ответ на всё нет, поэтому нужно разделить этот агрегат на три маленьких, уменьшив количество циклических зависимостей.
17 changes: 15 additions & 2 deletions DotNetTest/DotNetTest/ExampleCollection.cs
Original file line number Diff line number Diff line change
@@ -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<string> _collection = new List<string>();

bool _direction = true;

public void ReverseDirection()
{
_direction = !_direction;
}

public int Length => _collection.Count;

Expand All @@ -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);
}
}
}
67 changes: 67 additions & 0 deletions DotNetTest/DotNetTest/Iterator.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
24 changes: 4 additions & 20 deletions DotNetTest/DotNetTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,17 @@ 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();
collection.AddItem("foo");
collection.AddItem("bar");
collection.AddItem("a");

var sorted = AlphabeticalOrderSort(collection);
foreach (var element in collection)
{
Console.WriteLine(element);
}

}
}
Expand Down