DDD (Domain Driven Design) Can I use inheritance ?

Majkel

Consider the following example:

  • We have Order, and any order can be of diffrent types.
  • The order has transactions, and each type of order can assign n-transaction.

For example:

OrderA must have one transaction.

OrderB must have two transaction.

OrderC can have n-transaction.

When we update the value of the Order, we need calculate the value of the transaction according to the algorithm -specific order type.

How can we design a model for this case ? Can we use inheritance in aggregateRoot or maybe we should use composition ? For Example ?

 /// <summary>
///     AggregateRoot
/// </summary>
public abstract class Order
{
    protected ISet<Transaction> _transactions;

    public IEnumerable<Transaction> Transactions
    {
        get { return _transactions; }
    }

    public abstract OrderType OrderType { get; }
    public decimal? Value { get; protected set; }

    public void SetValue(decimal? value)
    {
        Value = value;
        UpdateReleatedTransaction();
    }

    protected abstract void UpdateReleatedTransaction();
}

public class OrderA : Order
{
    public OrderA()
    {
        _transactions.Add(new Transaction(this));
    }

    public override OrderType OrderType
    {
        get { return OrderType.OrderTypeA; }
    }

    protected override void UpdateReleatedTransaction()
    {
        EnumerableExtensions.ForEach(_transactions, tran => { tran.SetValue(Value); });
    }
}

public class OrderB : Order
{
    public OrderB()
    {
        _transactions.Add(new Transaction(this)
        {
            Status = Status.Open
        });
        _transactions.Add(new Transaction(this)
        {
            Status = Status.Close
        });
    }

    public override OrderType OrderType
    {
        get { return OrderType.OrderTypeB; }
    }

    protected override void UpdateReleatedTransaction()
    {
        EnumerableExtensions.ForEach(_transactions, tran =>
        {
            if (tran.Status == Status.Open)
                tran.SetValue(Value);
            if (tran.Status == Status.Close)
                tran.SetValue(-1*Value);
        });
    }
}
Augusto

Inheritance in DDD Aggragate Roots is a tricky topic. I think most people (and I think I also read this in Implementing Domain-Driven Design) would opt for not having inheritance in AR.

The main point to think about is behaviour. Will the different Order types have different public interface at any time in the future? If the answer is yes, then you have to go through the route of having different ARs per Order (and repositories too). I know you haven't asked this, but it's good to keep it in mind :).

Given your example, IMHO there's not much value of using composition, as the new AR would be just a very thin wrapper around Order, and that wrapper wouldn't have any resposibility other than delegate. If you keep the code clean, refactoring this (creating an AR that encapsulates the Order) should be quite easy.

Disclaimer: I've been using DDD for the last 5 years, and I still have a hard time getting my head around it... so take my advice with a pinch of salt.

Collected from the Internet

Please contact debug[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Domain Driven Design (DDD) and database generated reports

From Dev

Domain Driven Design: Can Infrastructure or Repositories use Domain objects?

From Dev

Domain Driven Design: Can Infrastructure or Repositories use Domain objects?

From Dev

Where to put security when using DDD - domain driven design

From Dev

Do I need to test the domain services in Domain driven design?

From Dev

Layers in Domain Driven Design

From Dev

Domain Driven Design - Domain or Security

From Dev

When we shouldn't use Domain-Driven Design approach?

From Dev

Domain driven design can value objects reference / embed an entity

From Dev

Domain Driven Design; Can ValueObject contains invariants or specifications?

From Dev

Access Control in Domain Driven Design

From Dev

Domain Driven Design and batch processing

From Dev

Implementing Domain Driven Design Cost

From Dev

Implementing Domain Driven Design Cost

From Dev

Domain Driven Design Bounded Context Domain Objects

From Dev

Domain Driven Design: infrastructure concern or domain concern?

From Dev

Domain Driven Design: infrastructure concern or domain concern?

From Dev

Domain Driven Design. Entity type design

From Dev

Implementing Paging and Sorting with Domain Driven Design

From Dev

Implementing Domain-Driven Design and Transactions

From Dev

Domain driven design repository implementation in infrastructure layer

From Dev

Domain Driven Design in Node.js Application

From Dev

Fetching associated aggregates in Domain driven Design

From Dev

Domain Driven Design - CQRS + ES usage

From Dev

Where is the call to persistence in the domain driven design

From Dev

Implementing Domain Driven Design Book Confusion

From Dev

Domain-Driven-Design Entities and Value Objects

From Dev

Proper way to get aggregates in Domain Driven Design

From Dev

Domain Driven Design Auto Incremented Entity Key

Related Related

  1. 1

    Domain Driven Design (DDD) and database generated reports

  2. 2

    Domain Driven Design: Can Infrastructure or Repositories use Domain objects?

  3. 3

    Domain Driven Design: Can Infrastructure or Repositories use Domain objects?

  4. 4

    Where to put security when using DDD - domain driven design

  5. 5

    Do I need to test the domain services in Domain driven design?

  6. 6

    Layers in Domain Driven Design

  7. 7

    Domain Driven Design - Domain or Security

  8. 8

    When we shouldn't use Domain-Driven Design approach?

  9. 9

    Domain driven design can value objects reference / embed an entity

  10. 10

    Domain Driven Design; Can ValueObject contains invariants or specifications?

  11. 11

    Access Control in Domain Driven Design

  12. 12

    Domain Driven Design and batch processing

  13. 13

    Implementing Domain Driven Design Cost

  14. 14

    Implementing Domain Driven Design Cost

  15. 15

    Domain Driven Design Bounded Context Domain Objects

  16. 16

    Domain Driven Design: infrastructure concern or domain concern?

  17. 17

    Domain Driven Design: infrastructure concern or domain concern?

  18. 18

    Domain Driven Design. Entity type design

  19. 19

    Implementing Paging and Sorting with Domain Driven Design

  20. 20

    Implementing Domain-Driven Design and Transactions

  21. 21

    Domain driven design repository implementation in infrastructure layer

  22. 22

    Domain Driven Design in Node.js Application

  23. 23

    Fetching associated aggregates in Domain driven Design

  24. 24

    Domain Driven Design - CQRS + ES usage

  25. 25

    Where is the call to persistence in the domain driven design

  26. 26

    Implementing Domain Driven Design Book Confusion

  27. 27

    Domain-Driven-Design Entities and Value Objects

  28. 28

    Proper way to get aggregates in Domain Driven Design

  29. 29

    Domain Driven Design Auto Incremented Entity Key

HotTag

Archive