title: How to a raise a domain event for the entity when the entity is created in clean architecture
body: <p>I have a project created using the <a href="https://github.com/ardalis/CleanArchitecture#using-the-dotnet-cli-template" rel="nofollow noreferrer">clean architecture template</a>.</p>
  <p>If I want a domain event be raised when a new project is created, where do I add that?</p>
  <p>If I have to raise an event whenever a new item be added to a project, I can accomplish that in the <a href="https://github.com/ardalis/CleanArchitecture/blob/af7379fedcef0176f3f1dfc0dc41bf047c3843db/src/Clean.Architecture.Core/ProjectAggregate/Project.cs#L27" rel="nofollow noreferrer">Project entity as shown here</a>.</p>
  <p>Similarly for <a href="https://github.com/ardalis/CleanArchitecture/blob/af7379fedcef0176f3f1dfc0dc41bf047c3843db/src/Clean.Architecture.Core/ProjectAggregate/ToDoItem.cs#L18" rel="nofollow noreferrer">MarkCompletion of a ToDoItem as done here</a>.</p>
  <p>But its not clear where do I put the code to raise an event when a new Project is created?</p>
  <p>One option is doing something like the <a href="https://github.com/ardalis/CleanArchitecture/blob/af7379fedcef0176f3f1dfc0dc41bf047c3843db/src/Clean.Architecture.Web/Endpoints/ProjectEndpoints/Create.cs#L37" rel="nofollow noreferrer">following in Create End Point here</a>.</p>
  <pre class="lang-cs prettyprint-override"><code>    newProject.Events.Add(new ProjectCreatedEvent(newProject));
  </code></pre>
  <p>But this is in UI, away from the domain model, and so does not feel right.</p>
  <p>The other option is using <a href="https://docs.microsoft.com/en-us/ef/core/logging-events-diagnostics/interceptors#savechanges-interception" rel="nofollow noreferrer">ef core interceptors</a>. So when ever save changes is called, just <a href="https://jaliyaudagedara.blogspot.com/2020/12/ef-core-50-savechanges-events-and.html" rel="nofollow noreferrer">raise event as appropriate something like here</a>.</p>
  <p>And if I add event in Project ctor, then this is triggered even in case of an update.</p>
  <pre class="lang-cs prettyprint-override"><code>      public Project(string name)
        {
          Name = Guard.Against.NullOrEmpty(name, nameof(name));
          
          var newProjectCreatedEvent = new NewProjectCreatedEvent(this);
          
          Events.Add(newProjectCreatedEvent);
        }
  </code></pre>
  <p>Are there any better options/patterns?</p>
  <p>Any pointer is deeply appreciated.</p>