首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >aspnetboilerplate EventCloud示例-设计模式

aspnetboilerplate EventCloud示例-设计模式
EN

Stack Overflow用户
提问于 2018-06-08 14:20:03
回答 1查看 140关注 0票数 0

在关于EventCloud示例应用程序的本教程中:https://aspnetboilerplate.com/Pages/Documents/Articles/Developing-MultiTenant-SaaS-ASP.NET-CORE-Angular/index.html

文本声明:必须使用"Event“类中的静态方法"Create”创建新实体(而不是使用"new Entity(....)")

1)所以我的第一个问题是:这是哪种设计模式?工厂?建造者?其他的?

代码语言:javascript
复制
[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的内部问题?

代码语言:javascript
复制
public class EventManager : IEventManager
{
 ......
 ....
 ..
 public async Task CreateAsync(Event @event)
 {
     await _eventRepository.InsertAsync(@event);
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-09 06:06:01

以下是我的答案:

1-)事件正在使用静态工厂方法创建。在Domain Driven Design中创建实体有两种方法。

带有静态工厂方法的

  1. Creating :这是一种创建业务实体的便捷方法。这种方法正在EventCloud中使用。这个方法唯一的缺点是它是静态的!如果你的实体处于保持状态,这对可测试性是不好的。但是这种方法有3个优点:
    1. They有名字:例如Event.CreatePublicEvent()
      1. They可以缓存:你可以将它们缓存在私有的静态Event.CreatePublicEvent()中,或者
        1. They可以subtype.

带有构造函数的

  1. Creating :如果只有一个构造函数,那么在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()方法只有一行代码,但它是开放的,可以进行扩展。

我希望这将是有用的;)

快乐的编码...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50754488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档