DDD: Enum like entities

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-02-10T14:22:17Z Indexed on 2010/03/09 11:06 UTC
Read the original article Hit count: 224

Hi all,

I have the following DB model:

**Person table**
ID    |    Name    | StateId
------------------------------
1          Joe       1
2          Peter     1
3          John      2

**State table**
ID    |    Desc
------------------------------
1          Working
2          Vacation

and domain model would be (simplified):

public class Person
{
    public int Id { get; }
    public string Name { get; set; }
    public State State { get; set; }
}

public class State
{
    private int id;
    public string Name { get; set; }
}

The state might be used in the domain logic e.g.:

if(person.State == State.Working)
    // some logic

So from my understanding, the State acts like a value object which is used for domain logic checks. But it also needs to be present in the DB model to represent a clean ERM.

So state might be extended to:

public class State
{
    private int id;
    public string Name { get; set; }

    public static State New {get {return new State([hardCodedIdHere?], [hardCodeNameHere?]);}}
}

But using this approach the name of the state would be hardcoded into the domain.

Do you know what I mean? Is there a standard approach for such a thing? From my point of view what I am trying to do is using an object (which is persisted from the ERM design perspective) as a sort of value object within my domain. What do you think?

Question update: Probably my question wasn't clear enough.

What I need to know is, how I would use an entity (like the State example) that is stored in a database within my domain logic. To avoid things like:

  if(person.State.Id == State.Working.Id)
      // some logic

or

if(person.State.Id == WORKING_ID)
// some logic

© Stack Overflow or respective owner

Related posts about .NET

Related posts about domain-driven-design