Skip to content
Auswöger Matthias edited this page May 12, 2021 · 1 revision

Extract properties in a base class and use it

Objective

In the following tutorial we take the result of Part 3 and improve it. We will extract a common member of our entities to show how to use inheritance with entity classes. All our entities do have an Id property, and we will make a base class that will contain that property and let all the entity classes inherit that property.

Steps

Base Entity Class

Add a new Entity class that look like this:

public abstract class Entity
{
    [Mapping(FieldName="Id"), IsPrimaryKey]
    public abstract int Id { get; }
}

Use base entity

Change all the entities like it's done here for the Customer entity.

Remove the Id property of the entities and derive them all from the Entity class, so that the class below:

public abstract class Customer
{
    [Mapping(FieldName="Id"), IsPrimaryKey]
    public abstract int Id { get; }
	
    [Mapping(FieldName = "FirstName")]
    public abstract string FirstName { get; set; }

looks like this:

public abstract class Customer : Entity
{
    [Mapping(FieldName = "FirstName")]
    public abstract string FirstName { get; set; }

After this is done, everything works as before, just without the need of adding the Id property to every entity class.

Full example

Checkout the full example here.

Clone this wiki locally