Domain Drive Design – DDD and entity libraries, models using multiple identity types

I have a model that looks like this:

public interface IEntity
{
int Id {get; set; }
}

Then my idea is to let my entity inherit from this interface:

public class User: IEntity
{
public int Id {get; set; }
}

However, one of my entities actually has a Guid as an identifier.

p>

public class UserSession
{
public Guid Id {get; set; }
}

I really want me All entities inherit from the same interface, but this will force me to add an integer-based identity column to the UserSession, which is unnecessary because Guid is the identity, but it will keep the domain model good because all entities will Inherited from the same foundation.

What are my options in this situation? Can I have two basic interfaces, one for int and one for Guid? I should add an identity column to the UserSession entity, although is it unnecessary? I need Guid so I can’t just get rid of it and replace it with an integer.

Any ideas on best practices?

You can of course implement the same IEntity interface in all entities, and in each Id field There are still different types.

Enter generics..

Define your interface like this.

public interface IEntity
{
T Id {get; set; }
}

Implement the interface in your entity..

public class User: IEntity
{
public int Id {get; set; }
}


public class UserSession: IEntity
{
public Guid Id {get; set; }
}

I have a look It looks like this model:

public interface IEntity
{
int Id {get; set; }
}

Then my idea is to let my entity inherit from this interface:

public class User: IEntity
{
public int Id {get; set; }
}

However, one of my entities actually has a Guid as an identifier.

public class UserSession 
{
public Guid Id {get; set; }
}

I really want all my entities to inherit from the same interface, but this will force me Adding an integer-based identity column to UserSession is unnecessary because Guid is the identity, but it will keep the domain model good, because all entities will inherit from the same foundation.

Here What are my options in this situation? Can I have two basic interfaces, one for int and one for Guid? I should add an identity column to the UserSession entity, although is it unnecessary? I need Guid so I can't just get rid of it and replace it with an integer.

Any ideas on best practices?

You can of course implement the same IEntity interface in all entities, and still have different types in each Id field.

Enter generics..

Define your interface like this.

public interface IEntity
{
T Id {get; set; }
}

Implement the interface in your entity..

public class User: IEntity 
{
public int Id {get; set; }
}


public class UserSession: IEntity
{
public Guid Id {get; set; }
}

Leave a Comment

Your email address will not be published.