oskardudycz / EventSourcing.NetCore

Examples and Tutorials of Event Sourcing in .NET
https://event-driven.io
Creative Commons Attribution Share Alike 4.0 International
3.42k stars 515 forks source link

Using multiple constructors in the aggregate #194

Open mehdihadeli opened 1 year ago

mehdihadeli commented 1 year ago

Hi Oskar, I have a question about this private constructor in th Order.

    private Order(Guid id, Guid clientId, IReadOnlyList<PricedProductItem> productItems, decimal totalPrice)
    {
        var @event = OrderInitialized.Create(
            id,
            clientId,
            productItems,
            totalPrice,
            DateTime.UtcNow
        );

        Enqueue(@event);
        Apply(@event);
    }

Why do you need this constructor? We can just call this functionality inner Initialize static method, like below code and remove this parameters constructor:

    public static Order Initialize(
        Guid orderId,
        Guid clientId,
        IReadOnlyList<PricedProductItem> productItems,
        decimal totalPrice)
    {
        var order = new Order();

        var @event = OrderInitialized.Create(
            id,
            clientId,
            productItems,
            totalPrice,
            DateTime.UtcNow
        );

        order.Enqueue(@event);
        order.Apply(@event);

        return order;
    }

It is less code :D