Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about Approach 10 - Aggregates with MediatR Events #5

Open
opcodewriter opened this issue May 21, 2021 · 1 comment
Open

Comments

@opcodewriter
Copy link

opcodewriter commented May 21, 2021

Isn't an issue that the Product domain model has public c-tors which do not check for the validity of the name?
This makes it possible to have an invalid Product model.

    public class Product
    {
        public Product()
        {
        }
        public Product(string name)
        {
            Name = name;
        }

        ....
   }

https://github.com/ardalis/DDD-NoDuplicates/blob/master/NoDuplicatesDesigns/10_AggregateWithMediatR/Product.cs

I'm not sure what's the solution for this. Maybe make the Product c-tors private and add a factory method and a new domain event, something like this:

public class Product 
{
       private Product()
       {
       }
       private Product(string name)
       {
           Name = name;
       }

      public static Product Create(int catalogId, string name)
      {
             DomainEvents.Raise(new ProductCreationRequested(catalogId, name)).GetAwaiter().GetResult();
             return new Product(name);
      }
}

Add add the new event handler class inside the Catalog class:

public class ProductCreationHandler : INotificationHandler<ProductCreationRequested>
{
    public Task Handle(ProductCreationRequested notification, CancellationToken cancellationToken)
    {
        var catalog = _catalogRepository.GetById(notification.CatalogId);
        catalog.AddProduct(notification.Name); // AddProduct(catalogId, name) would be private in the Catalog class
        
        return Task.CompletedTask;
    }
}

What do you think?

@opcodewriter
Copy link
Author

2nd question: By taking a dependency on the MediatR, isn't considered a hidden dependency?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant