aspnetboilerplate EventCloud 示例 - 设计模式
aspnetboilerplate EventCloud example - design pattern
在本教程中关于 EventCloud 示例应用程序:
https://aspnetboilerplate.com/Pages/Documents/Articles/Developing-MultiTenant-SaaS-ASP.NET-CORE-Angular/index.html
文本指出:必须使用 "Event" class 中的静态方法 "Create"(不使用 "new Entity(....)")
1) 所以我的第一个问题是:这是哪种设计模式?工厂?建设者?其他?
[Table("AppEvents")]
public class Event : FullAuditedEntity<Guid>, IMustHaveTenant
{
......
....
...
/// <summary>
/// We don't make constructor public and forcing to create events using <see cref="Create"/> method.
/// But constructor can not be private since it's used by EntityFramework.
/// Thats why we did it protected.
/// </summary>
protected Event()
{
}
public static Event Create(int tenantId, string title, DateTime date, string description = null, int maxRegistrationCount = 0)
{
var @event = new Event
{
Id = Guid.NewGuid(),
TenantId = tenantId,
Title = title,
Description = description,
MaxRegistrationCount = maxRegistrationCount
};
@event.SetDate(date);
@event.Registrations = new Collection<EventRegistration>();
return @event;
}
....
...
2)第二题:
比文章说的...
事件管理器....所有事件操作都应使用此class...(事件管理器)
执行
好的,CreateAsync 方法调用存储库插入方法,静态 "Event.Create" 是从存储库插入方法内部调用的吗?如果是的话,你能告诉我abp源代码中的重点吗?
还是EntityFramework的内部问题?
public class EventManager : IEventManager
{
......
....
..
public async Task CreateAsync(Event @event)
{
await _eventRepository.InsertAsync(@event);
}
这是我的答案:
1-) 正在使用静态工厂方法创建事件。在 Domain Driven Design
中有 2 种创建实体的方法。
- 使用静态工厂方法创建:这是创建业务实体的便捷方式。而这个方法正在EventCloud中使用。这种方法的唯一缺点是它是静态的!如果您的实体处于保持状态,则不利于可测试性。但是这种方法有 3 个优点;
- 他们有名字:例如
Event.CreatePublicEvent()
、Create.PrivateEvent()
- They can cache: 您可以将它们缓存在私有静态
HashSet
或 Dictionary
.
- 他们可以子类型。
使用构造函数创建:如果您只有一个构造函数,那么通过其 public 构造函数创建对象是 Domain Driven Design
。只要您使无参数构造函数受保护或私有。此外,一个实体应对其自身的数据完整性和有效性负责,因此您必须将所有与业务相关的 public 属性设置为私有 setter 并且您应该允许它们通过 public 方法进行更改。
更多信息,参见https://www.yegor256.com/2017/11/14/static-factory-methods.html
2-) EventManager is a domain service that is used for business logic. And Event.Create()
is being used in the EventAppService
class. Click here to see where exactly is being executed. 甚至 Event.Create()
方法也由一行代码组成,但它是开放扩展的。
希望有用 ;)
编码愉快...
在本教程中关于 EventCloud 示例应用程序: https://aspnetboilerplate.com/Pages/Documents/Articles/Developing-MultiTenant-SaaS-ASP.NET-CORE-Angular/index.html
文本指出:必须使用 "Event" class 中的静态方法 "Create"(不使用 "new Entity(....)")
1) 所以我的第一个问题是:这是哪种设计模式?工厂?建设者?其他?
[Table("AppEvents")]
public class Event : FullAuditedEntity<Guid>, IMustHaveTenant
{
......
....
...
/// <summary>
/// We don't make constructor public and forcing to create events using <see cref="Create"/> method.
/// But constructor can not be private since it's used by EntityFramework.
/// Thats why we did it protected.
/// </summary>
protected Event()
{
}
public static Event Create(int tenantId, string title, DateTime date, string description = null, int maxRegistrationCount = 0)
{
var @event = new Event
{
Id = Guid.NewGuid(),
TenantId = tenantId,
Title = title,
Description = description,
MaxRegistrationCount = maxRegistrationCount
};
@event.SetDate(date);
@event.Registrations = new Collection<EventRegistration>();
return @event;
}
....
...
2)第二题:
比文章说的...
事件管理器....所有事件操作都应使用此class...(事件管理器)
执行好的,CreateAsync 方法调用存储库插入方法,静态 "Event.Create" 是从存储库插入方法内部调用的吗?如果是的话,你能告诉我abp源代码中的重点吗? 还是EntityFramework的内部问题?
public class EventManager : IEventManager
{
......
....
..
public async Task CreateAsync(Event @event)
{
await _eventRepository.InsertAsync(@event);
}
这是我的答案:
1-) 正在使用静态工厂方法创建事件。在 Domain Driven Design
中有 2 种创建实体的方法。
- 使用静态工厂方法创建:这是创建业务实体的便捷方式。而这个方法正在EventCloud中使用。这种方法的唯一缺点是它是静态的!如果您的实体处于保持状态,则不利于可测试性。但是这种方法有 3 个优点;
- 他们有名字:例如
Event.CreatePublicEvent()
、Create.PrivateEvent()
- They can cache: 您可以将它们缓存在私有静态
HashSet
或Dictionary
. - 他们可以子类型。
- 他们有名字:例如
使用构造函数创建:如果您只有一个构造函数,那么通过其 public 构造函数创建对象是
Domain Driven Design
。只要您使无参数构造函数受保护或私有。此外,一个实体应对其自身的数据完整性和有效性负责,因此您必须将所有与业务相关的 public 属性设置为私有 setter 并且您应该允许它们通过 public 方法进行更改。更多信息,参见https://www.yegor256.com/2017/11/14/static-factory-methods.html
2-) EventManager is a domain service that is used for business logic. And Event.Create()
is being used in the EventAppService
class. Click here to see where exactly is being executed. 甚至 Event.Create()
方法也由一行代码组成,但它是开放扩展的。
希望有用 ;)
编码愉快...