Skip to content

Commit ad78a56

Browse files
committed
update domain
1 parent c5567fc commit ad78a56

File tree

3 files changed

+97
-70
lines changed

3 files changed

+97
-70
lines changed

src/api/modules/Catalog/Catalog.Domain/Brand.cs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,47 @@
55
namespace FSH.Starter.WebApi.Catalog.Domain;
66
public class Brand : AuditableEntity, IAggregateRoot
77
{
8-
public string Name { get; private set; } = default!;
8+
public string Name { get; private set; } = string.Empty;
99
public string? Description { get; private set; }
1010

11-
public static Brand Create(string name, string? description)
12-
{
13-
var brand = new Brand
14-
{
15-
Name = name,
16-
Description = description
17-
};
11+
private Brand() { }
1812

19-
brand.QueueDomainEvent(new BrandCreated() { Brand = brand });
13+
private Brand(Guid id, string name, string? description)
14+
{
15+
Id = id;
16+
Name = name;
17+
Description = description;
18+
QueueDomainEvent(new BrandCreated { Brand = this });
19+
}
2020

21-
return brand;
21+
public static Brand Create(string name, string? description)
22+
{
23+
return new Brand(Guid.NewGuid(), name, description);
2224
}
2325

2426
public Brand Update(string? name, string? description)
2527
{
26-
if (name is not null && Name?.Equals(name, StringComparison.OrdinalIgnoreCase) is not true) Name = name;
27-
if (description is not null && Description?.Equals(description, StringComparison.OrdinalIgnoreCase) is not true) Description = description;
28-
29-
this.QueueDomainEvent(new BrandUpdated() { Brand = this });
28+
bool isUpdated = false;
3029

31-
return this;
32-
}
30+
if (!string.IsNullOrWhiteSpace(name) && !string.Equals(Name, name, StringComparison.OrdinalIgnoreCase))
31+
{
32+
Name = name;
33+
isUpdated = true;
34+
}
3335

34-
public static Brand Update(Guid id, string name, string? description)
35-
{
36-
var brand = new Brand
36+
if (!string.Equals(Description, description, StringComparison.OrdinalIgnoreCase))
3737
{
38-
Id = id,
39-
Name = name,
40-
Description = description
41-
};
38+
Description = description;
39+
isUpdated = true;
40+
}
4241

43-
brand.QueueDomainEvent(new BrandUpdated() { Brand = brand });
42+
if (isUpdated)
43+
{
44+
QueueDomainEvent(new BrandUpdated { Brand = this });
45+
}
4446

45-
return brand;
47+
return this;
4648
}
4749
}
4850

51+

src/api/modules/Catalog/Catalog.Domain/Product.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,64 @@
55
namespace FSH.Starter.WebApi.Catalog.Domain;
66
public class Product : AuditableEntity, IAggregateRoot
77
{
8-
public string Name { get; private set; } = default!;
8+
public string Name { get; private set; } = string.Empty;
99
public string? Description { get; private set; }
1010
public decimal Price { get; private set; }
1111
public Guid? BrandId { get; private set; }
1212
public virtual Brand Brand { get; private set; } = default!;
1313

14-
public static Product Create(string name, string? description, decimal price, Guid? brandId)
15-
{
16-
var product = new Product();
14+
private Product() { }
1715

18-
product.Name = name;
19-
product.Description = description;
20-
product.Price = price;
21-
product.BrandId = brandId;
16+
private Product(Guid id, string name, string? description, decimal price, Guid? brandId)
17+
{
18+
Id = id;
19+
Name = name;
20+
Description = description;
21+
Price = price;
22+
BrandId = brandId;
2223

23-
product.QueueDomainEvent(new ProductCreated() { Product = product });
24+
QueueDomainEvent(new ProductCreated { Product = this });
25+
}
2426

25-
return product;
27+
public static Product Create(string name, string? description, decimal price, Guid? brandId)
28+
{
29+
return new Product(Guid.NewGuid(), name, description, price, brandId);
2630
}
2731

2832
public Product Update(string? name, string? description, decimal? price, Guid? brandId)
2933
{
30-
if (name is not null && Name?.Equals(name, StringComparison.OrdinalIgnoreCase) is not true) Name = name;
31-
if (description is not null && Description?.Equals(description, StringComparison.OrdinalIgnoreCase) is not true) Description = description;
32-
if (price.HasValue && Price != price) Price = price.Value;
33-
if (brandId.HasValue && brandId.Value != Guid.Empty && !BrandId.Equals(brandId.Value)) BrandId = brandId.Value;
34+
bool isUpdated = false;
3435

35-
this.QueueDomainEvent(new ProductUpdated() { Product = this });
36-
return this;
37-
}
36+
if (!string.IsNullOrWhiteSpace(name) && !string.Equals(Name, name, StringComparison.OrdinalIgnoreCase))
37+
{
38+
Name = name;
39+
isUpdated = true;
40+
}
3841

39-
public static Product Update(Guid id, string name, string? description, decimal price, Guid? brandId)
40-
{
41-
var product = new Product
42+
if (!string.Equals(Description, description, StringComparison.OrdinalIgnoreCase))
43+
{
44+
Description = description;
45+
isUpdated = true;
46+
}
47+
48+
if (price.HasValue && Price != price.Value)
49+
{
50+
Price = price.Value;
51+
isUpdated = true;
52+
}
53+
54+
if (brandId.HasValue && brandId.Value != Guid.Empty && BrandId != brandId.Value)
4255
{
43-
Id = id,
44-
Name = name,
45-
Description = description,
46-
Price = price,
47-
BrandId = brandId
48-
};
56+
BrandId = brandId.Value;
57+
isUpdated = true;
58+
}
4959

50-
product.QueueDomainEvent(new ProductUpdated() { Product = product });
60+
if (isUpdated)
61+
{
62+
QueueDomainEvent(new ProductUpdated { Product = this });
63+
}
5164

52-
return product;
65+
return this;
5366
}
5467
}
68+

src/api/modules/Todo/Domain/TodoItem.cs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,44 @@
33
using FSH.Starter.WebApi.Todo.Domain.Events;
44

55
namespace FSH.Starter.WebApi.Todo.Domain;
6-
public class TodoItem : AuditableEntity, IAggregateRoot
6+
public sealed class TodoItem : AuditableEntity, IAggregateRoot
77
{
8-
public string? Title { get; set; }
8+
public string Title { get; private set; } = string.Empty;
9+
public string Note { get; private set; } = string.Empty;
910

10-
public string? Note { get; set; }
11+
private TodoItem() { }
1112

12-
public static TodoItem Create(string title, string note)
13+
private TodoItem(string title, string note)
1314
{
14-
var item = new TodoItem();
15-
16-
item.Title = title;
17-
item.Note = note;
18-
19-
item.QueueDomainEvent(new TodoItemCreated(item.Id, item.Title, item.Note));
20-
15+
Title = title;
16+
Note = note;
17+
QueueDomainEvent(new TodoItemCreated(Id, Title, Note));
2118
TodoMetrics.Created.Add(1);
22-
23-
return item;
2419
}
2520

21+
public static TodoItem Create(string title, string note) => new(title, note);
22+
2623
public TodoItem Update(string? title, string? note)
2724
{
28-
if (title is not null && Title?.Equals(title, StringComparison.OrdinalIgnoreCase) is not true) Title = title;
29-
if (note is not null && Note?.Equals(note, StringComparison.OrdinalIgnoreCase) is not true) Note = note;
25+
bool isUpdated = false;
3026

31-
this.QueueDomainEvent(new TodoItemUpdated(this));
27+
if (!string.IsNullOrWhiteSpace(title) && !string.Equals(Title, title, StringComparison.OrdinalIgnoreCase))
28+
{
29+
Title = title;
30+
isUpdated = true;
31+
}
3232

33-
return this;
33+
if (!string.IsNullOrWhiteSpace(note) && !string.Equals(Note, note, StringComparison.OrdinalIgnoreCase))
34+
{
35+
Note = note;
36+
isUpdated = true;
37+
}
3438

39+
if (isUpdated)
40+
{
41+
QueueDomainEvent(new TodoItemUpdated(this));
42+
}
43+
44+
return this;
3545
}
3646
}

0 commit comments

Comments
 (0)