Entities
Entities are domain objects that are uniquely defined by a unique identifier, and not by their attributes.
To implement new entity in your application inherit entity class from Entity<TEntity, TKey> or CompositeEntity.
Entity
Entity<TEntity, TKey> is base class for an entity TEntity with unique identifier TKey.
Properties:
Id
TKey
The entity unique identifier.
Methods:
IsTransient
bool
Returns true if current entity is transient (created in memory but not saved in data store), otherwise false.
Equals
bool
Indicates whether the current entity is equal to specified entity of the same type.
Entity<TEntity, TKey> also have implementation of operators == and !=.
Example of simple entity:
// User entity with unique identifier property "int Id { get; protected set; }".
public class User : Entity<User, int>
{
public string Name { get; private set; }
private User() : base() { }
public User(int id, string name) : base()
{
Id = id;
Name = name;
}
}Let's compare two object of type User:
You can also use enum, Guid, string and other types for identifier:
In real world when you using ORM (like EF6 or EFCore) on board you don't setting entity identifier in constructor for auto-increment primary keys. It's because ORM do it when you call SaveChanges(). That's why you may need method IsTransient().
Example:
CompositeEntity
CompositeEntity<TEntity> is base class for an entity TEntity with composite unique identifier.
CompositeEntity<TEntity> have all the same methods as Entity<TEntity, TKey>, but don't have and Id property. Instead of public property Id that class have protected abstract method GetCompositeId() that should be implementing to resolve composite identifier.
Using this class is very helpful when setting up entity type configurations in EF6 or EFCore DbContext.
Example:
And then simply setting up entity type configuration in DbContext, for example EFCore:
Auto-auditing
Structr.Domain provides some abstract classes and interfaces to easy implement auto-auditing in ORM DbContext.
Auto-auditing - it's when you call SaveChanges() entity's properties like DateCreated or DateModified filling and tracking automatically.
List of auditable interfaces and abstract classes:
ICreatable
Provides information about an auditable entity creation date.
IModifiable
Provides information about an auditable entity modification date.
ISoftDeletable
Provides information about an auditable entity deletion date.
ISignedCreatable
Provides information about who created an auditable entity and when.
ISignedModifiable
Provides information about who modified an auditable entity and when.
ISignedSoftDeletable
Provides information about who deleted an auditable entity and when.
AuditableEntity
Provides information about an auditable entity creation date and modification date.
SignedAuditableEntity
Provides information about who created and modified an auditable entity and when.
Example of usage:
Configure your DbContext to use auto-auditing:
Important: For using Structr.Domain benefits with DbContext you should install Structr.EntityFramework or Structr.EntityFrameworkCore package.
Last updated
Was this helpful?