Skip to content

Entities

Overview

An entity is a data model defined in the configuration and used in different parts of the generated solution.

The Entity Services App builder will create separate projects for entities used by repositories and by endpoints. Endpoint entities will be more lightweight, they won’t have any dependencies to Entity Services libraries and can be exposed (as nuget packages) to the consumers of your API.

Entity Keys

The following is valid for the entities used in Data Api:

Property with the name Id will be the key of the entity.

If “autogeneratedKey” field was set to true, the Id value will be automatically created (in that case the type of that property can be either string or uuid). If the string type is used, the value will be autogenerated sequential uuid value.

Note that the property with the name Id still has to be defined in the configuration.

Grouping

The entities can be used for different purposes and sometimes you want to use only some of the entities defined in your configuration or use them with different components or features. To achieve that, you can associate them in different groups, as described in the Group section.

Implementation

Repository Entities

public class Asset : BaseEntity<Guid>, IEquatable<Asset?>
{
    public string Name { get; init; }
    public int Quantity { get; init; }

    public Asset(Guid id, string name, int quantity) : base(id)
    public Asset(Guid id, string name, int quantity, string? lastRevision) : this(id, name, quantity)

    public override int GetHashCode()
    public override int GetRevision()

    public override bool Equals(object? obj)
    public bool Equals(Asset? other)

    public static bool operator ==(Asset? left, Asset? right)
    public static bool operator !=(Asset? left, Asset? right)
}


public abstract class BaseEntity<TId> : IEntityWithPrimaryKey<TId>
{
    public virtual TId Id { get; init; }

    public TId EntityId { get; init; }

    public string EntityType { get; init; } = "Entity";

    public DateTime ChangedAt { get; set; } = DateTime.UtcNow;

    public virtual string Revision
    {
        get => GetRevision().ToString();
        set { }
    }

    public string? OriginalRevision { get; set; }

    public abstract int GetRevision();

    protected BaseEntity(TId id)
    {
        Id = id;
        EntityId = id;
    }
}

Repository History Entities

public class AssetHistory : BaseEntityHistory<Guid>
{
    public string Name { get; set; }

    public int Quantity { get; set; }

    public AssetHistory(Guid entityId, string name, int quantity, string id, string entityRevision, DateTime changedAt, DateTime? deletedAt = null) : base(id, entityId, entityRevision, changedAt, deletedAt)
    {
        Name = name;
        Quantity = quantity;
    }

    public AssetHistory(Asset asset, string id, DateTime changedAt, DateTime? deletedAt = null) : base(id, asset.Id, asset.Revision, changedAt, deletedAt)
    {
        Name = asset.Name;
        Quantity = asset.Quantity;
    }
}

public abstract class BaseEntityHistory<TEntityKey> : IEntityHistory<TEntityKey>
{
    public string Id { get; init; }

    public string EntityType { get; init; } = "History";

    public DateTime ChangedAt { get; set; }
    public DateTime? DeletedAt { get; set; }

    public TEntityKey EntityId { get; init; }
    public string EntityRevision { get; set; }



    protected BaseEntityHistory(string id, TEntityKey entityId, string entityRevision, DateTime changedAt, DateTime? deletedAt = null)
    {
        Id = id;
        EntityId = entityId;
        EntityRevision = entityRevision;
        ChangedAt = changedAt;
        DeletedAt = deletedAt;
    }
}

Endpoint Entities

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

    public string Name { get; set; }

    public int Quantity { get; set; }

    public string? OriginalRevision { get; set; }

    public AssetContent(Guid id, string name, int quantity, string? originalRevision = null)
    {
        Id = id;
        Name = name;
        Quantity = quantity;
        OriginalRevision = originalRevision;
    }
}

public class Asset : AssetContent
{
    public DateTime ChangedAt { get; set; }

    public Asset(Guid id, string name, int quantity, DateTime changedAt, string? originalRevision = null) : base(id, name, quantity, originalRevision)
    {
        Id = id;
        ChangedAt = changedAt;
    }
}

public class AssetHistory
{
    public string Id { get; set; }

    public DateTime ChangedAt { get; set; }

    public DateTime? DeletedAt { get; set; }

    public Guid EntityId { get; set; }

    public string EntityRevision { get; set; }

    public string Name { get; set; }

    public int Quantity { get; set; }

    public AssetHistory(Guid entityId, string name, int quantity, string entityRevision, string id, DateTime changedAt, DateTime? deletedAt = null)
    {
        Id = id;
        ChangedAt = changedAt;
        DeletedAt = deletedAt;
        EntityId = entityId;
        EntityRevision = entityRevision;
        Name = name;
        Quantity = quantity;
    }
}
Back to top