在关于EventCloud示例应用程序的本教程中:https://aspnetboilerplate.com/Pages/Documents/Articles/Developing-MultiTenant-SaaS-ASP.NET-CORE-Angular/index.html
文本声明:必须使用"Event“类中的静态方法"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)第二个问题:
比文章上说的..。
事件管理器....所有事件操作都应该使用这个类来执行...(EventManager)
好的,CreateAsync方法调用存储库插入方法,静态的"Event.Create“是从存储库插入方法内部调用的吗?如果是,你能告诉我总部基地源代码中的哪一点吗?或者这是EntityFramework的内部问题?
public class EventManager : IEventManager
{
......
....
..
public async Task CreateAsync(Event @event)
{
await _eventRepository.InsertAsync(@event);
}
发布于 2018-06-09 06:06:01
以下是我的答案:
1-)事件正在使用静态工厂方法创建。在Domain Driven Design
中创建实体有两种方法。
带有静态工厂方法的
Event.CreatePublicEvent()
,Event.CreatePublicEvent()
中,或者
带有构造函数的
Domain Driven Design
中,通过其公共构造函数创建对象是最方便的方法。只要您将无参数构造函数设置为受保护的或私有的。此外,实体应该对自己的数据完整性和有效性负责,因此您必须将所有与业务相关的公共属性设置为私有setter,并且应该允许它们通过公共方法进行更改。有关更多信息,请参阅https://www.yegor256.com/2017/11/14/static-factory-methods.html
2-) EventManager是用于业务逻辑的域服务。在EventAppService
类中使用了Event.Create()
。Click here to see where exactly is being executed.甚至Event.Create()
方法只有一行代码,但它是开放的,可以进行扩展。
我希望这将是有用的;)
快乐的编码...
https://stackoverflow.com/questions/50754488
复制相似问题