From a3b025ee105d521ceaab166424b60807a8801e66 Mon Sep 17 00:00:00 2001 From: OsmanthusPeace <3139714892@qq.com> Date: Sun, 21 Jul 2024 20:50:54 +0800 Subject: [PATCH] feat: basically finish the design of budget aggregate --- src/Backend/Asset.Domain/Asset.Domain.csproj | 4 ++ src/Backend/Asset.Domain/Class1.cs | 6 --- .../Budget.Domain/Budget.Domain.csproj | 4 ++ .../BudgetAggregate/BudgetEntity/Budget.cs | 39 +++++++++++++++++++ .../BudgetAggregate/BudgetEntity/BudgetId.cs | 9 +++++ .../Events/BudgetCreatedDomainEvent.cs | 8 ++++ .../Events/SubBudgetAddedDomainEvent.cs | 9 +++++ .../BudgetAggregate/IBudgetRepository.cs | 6 +++ .../Events/SubBudgetCreatedDomainEvent.cs | 8 ++++ .../SubBudgetEntity/SubBudget.cs | 22 +++++++++++ .../SubBudgetEntity/SubBudgetId.cs | 6 +++ src/Backend/Budget.Domain/Class1.cs | 6 --- 12 files changed, 115 insertions(+), 12 deletions(-) delete mode 100644 src/Backend/Asset.Domain/Class1.cs create mode 100644 src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Budget.cs create mode 100644 src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/BudgetId.cs create mode 100644 src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Events/BudgetCreatedDomainEvent.cs create mode 100644 src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Events/SubBudgetAddedDomainEvent.cs create mode 100644 src/Backend/Budget.Domain/BudgetAggregate/IBudgetRepository.cs create mode 100644 src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/Events/SubBudgetCreatedDomainEvent.cs create mode 100644 src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/SubBudget.cs create mode 100644 src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/SubBudgetId.cs delete mode 100644 src/Backend/Budget.Domain/Class1.cs diff --git a/src/Backend/Asset.Domain/Asset.Domain.csproj b/src/Backend/Asset.Domain/Asset.Domain.csproj index fa71b7a..5b635c6 100644 --- a/src/Backend/Asset.Domain/Asset.Domain.csproj +++ b/src/Backend/Asset.Domain/Asset.Domain.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/src/Backend/Asset.Domain/Class1.cs b/src/Backend/Asset.Domain/Class1.cs deleted file mode 100644 index c104ae8..0000000 --- a/src/Backend/Asset.Domain/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Asset.Domain; - -public class Class1 -{ - -} diff --git a/src/Backend/Budget.Domain/Budget.Domain.csproj b/src/Backend/Budget.Domain/Budget.Domain.csproj index fa71b7a..ab9071f 100644 --- a/src/Backend/Budget.Domain/Budget.Domain.csproj +++ b/src/Backend/Budget.Domain/Budget.Domain.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Budget.cs b/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Budget.cs new file mode 100644 index 0000000..c22e703 --- /dev/null +++ b/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Budget.cs @@ -0,0 +1,39 @@ +using Budget.Domain.BudgetAggregate.BudgetEntity.Events; +using Budget.Domain.BudgetAggregate.SubBudgetEntity; +using Primitives.Entity; +using Shared.Primitives; + +namespace Budget.Domain.BudgetAggregate.BudgetEntity; + +public class Budget : EntityBase, IAggregateRoot +{ + private Budget(DateTime startDate, DateTime endDate, decimal amount, string description) : base(new BudgetId(1)) + { + StartDate = startDate; + EndDate = endDate; + Amount = amount; + Description = description; + SubBudgets = new List(); + } + + public DateTime StartDate { get; private set; } + public DateTime EndDate { get; private set; } + public decimal Amount { get; private set; } + public string Description { get; private set; } + private List SubBudgets { get; set; } + + + public static Budget CreateBudget(DateTime startDate, DateTime endDate, decimal amount, string description) + { + var budget= new Budget(startDate, endDate, amount, description); + budget.AddDomainEvent(new BudgetCreatedDomainEvent(budget.Id)); + return budget; + } + + public void AddSubBudget(string tag, decimal amount) + { + var subBudget = SubBudget.CreateSubBudget(tag, amount); + SubBudgets.Add(subBudget); + subBudget.AddDomainEvent(new SubBudgetAddedDomainEvent(subBudget.Id)); + } +} \ No newline at end of file diff --git a/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/BudgetId.cs b/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/BudgetId.cs new file mode 100644 index 0000000..04d3b26 --- /dev/null +++ b/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/BudgetId.cs @@ -0,0 +1,9 @@ +namespace Budget.Domain.BudgetAggregate.BudgetEntity; + +public readonly record struct BudgetId(long Value) +{ + public override string ToString() + { + return Value.ToString(); + } +} \ No newline at end of file diff --git a/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Events/BudgetCreatedDomainEvent.cs b/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Events/BudgetCreatedDomainEvent.cs new file mode 100644 index 0000000..0d21889 --- /dev/null +++ b/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Events/BudgetCreatedDomainEvent.cs @@ -0,0 +1,8 @@ +using Shared.Primitives.DomainEvent; + +namespace Budget.Domain.BudgetAggregate.BudgetEntity.Events; + +public class BudgetCreatedDomainEvent(BudgetId budgetId):IDomainEvent +{ + public BudgetId BudgetId { get; } = budgetId; +} \ No newline at end of file diff --git a/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Events/SubBudgetAddedDomainEvent.cs b/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Events/SubBudgetAddedDomainEvent.cs new file mode 100644 index 0000000..ef3710a --- /dev/null +++ b/src/Backend/Budget.Domain/BudgetAggregate/BudgetEntity/Events/SubBudgetAddedDomainEvent.cs @@ -0,0 +1,9 @@ +using Budget.Domain.BudgetAggregate.SubBudgetEntity; +using Shared.Primitives.DomainEvent; + +namespace Budget.Domain.BudgetAggregate.BudgetEntity.Events; + +public class SubBudgetAddedDomainEvent(SubBudgetId subBudgetId) : IDomainEvent +{ + public SubBudgetId SubBudgetId { get; } = subBudgetId; +} \ No newline at end of file diff --git a/src/Backend/Budget.Domain/BudgetAggregate/IBudgetRepository.cs b/src/Backend/Budget.Domain/BudgetAggregate/IBudgetRepository.cs new file mode 100644 index 0000000..5b39d52 --- /dev/null +++ b/src/Backend/Budget.Domain/BudgetAggregate/IBudgetRepository.cs @@ -0,0 +1,6 @@ +namespace Budget.Domain.BudgetAggregate; + +public interface IBudgetRepository +{ + +} \ No newline at end of file diff --git a/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/Events/SubBudgetCreatedDomainEvent.cs b/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/Events/SubBudgetCreatedDomainEvent.cs new file mode 100644 index 0000000..dd14a51 --- /dev/null +++ b/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/Events/SubBudgetCreatedDomainEvent.cs @@ -0,0 +1,8 @@ +using Shared.Primitives.DomainEvent; + +namespace Budget.Domain.BudgetAggregate.SubBudgetEntity.Events; + +public class SubBudgetCreatedDomainEvent(SubBudgetId subBudgetId) : IDomainEvent +{ + public SubBudgetId SubBudgetId { get; } = subBudgetId; +} \ No newline at end of file diff --git a/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/SubBudget.cs b/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/SubBudget.cs new file mode 100644 index 0000000..39d7ac1 --- /dev/null +++ b/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/SubBudget.cs @@ -0,0 +1,22 @@ +using Budget.Domain.BudgetAggregate.SubBudgetEntity.Events; +using Primitives.Entity; + +namespace Budget.Domain.BudgetAggregate.SubBudgetEntity; + +public class SubBudget : EntityBase +{ + private SubBudget(string tag, decimal amount) : base(new SubBudgetId(1)) + { + Tag = tag; + Amount = amount; + } + public static SubBudget CreateSubBudget(string tag, decimal amount) + { + var subBudget= new SubBudget(tag, amount); + subBudget.AddDomainEvent(new SubBudgetCreatedDomainEvent(subBudget.Id)); + return subBudget; + } + + public string Tag { get; private set; } + public decimal Amount { get; private set; } +} \ No newline at end of file diff --git a/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/SubBudgetId.cs b/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/SubBudgetId.cs new file mode 100644 index 0000000..c15e7a0 --- /dev/null +++ b/src/Backend/Budget.Domain/BudgetAggregate/SubBudgetEntity/SubBudgetId.cs @@ -0,0 +1,6 @@ +namespace Budget.Domain.BudgetAggregate.SubBudgetEntity; + +public readonly record struct SubBudgetId(long Value) +{ + public override string ToString() => Value.ToString(); +} \ No newline at end of file diff --git a/src/Backend/Budget.Domain/Class1.cs b/src/Backend/Budget.Domain/Class1.cs deleted file mode 100644 index d084fdf..0000000 --- a/src/Backend/Budget.Domain/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Budget.Domain; - -public class Class1 -{ - -}