A domain entity is a class that represent a single database table. The properties of these classes become the columns in the database for that entity table. The following document will describe how to create domain entities in EvenCart plugin.
Domain entity classes
A domain entity class is a class that inherits the abstract class EvenCart.Core.Data.FoundationEntity
exposed by EvenCart. All the domain entities in EvenCart have Id
as it's primary key column of type integer. The FoundationEntity
class declares an integer property called Id
to satisfy this.
A few things that need to be aware of while creating domain entity classes are as follows.
- Only the properties which are
public
and have bothgetter
andsetter
signature are eligible to be mapped for columns. - The domain entity class should be
public
and should not be declaredabstract
. If a class is declaredabstract
it is not mapped to any database table. However it's concrete sub-classes can be mapped to database tables, provided that they also fulfill the conditions listed here. - The properties that need to be mapped to database columns can only have primitive return types.
- If a property contains a complex data type such as
class
then those properties must be declaredvirtual
. These properties will be excluded from mapping. - Make sure that domain class name doesn't have similar name to that of EvenCart entities. Otherwise the plugin is likely to produce unwanted errors.
Creating a domain entity class
The following example shows how to create a domain entity classes named Book
and Page
. We recommend that each domain entity class should be kept in a separate file with same name.
//Data\Book.cs
@using EvenCart.Core.Data;
public class Book : FoundationEntity
{
public string Title {get; set;}
public string Isbn {get; set;}
public string Author {get;set;}
}
//Data\Page.cs
public class Page : FoundationEntity
{
public int BookId {get;set;}
public string PageNumber {get;set;}
public string Content {get; set;}
}
Defining relationships between domain classes
In a typical plugin, we'll have multiple domain classes mapping to multiple database tables. And there will always be relationships between those tables.
To define such relationships between domain classes, virtual
properties are used.
The following example modifies the above code to define a one-to-many relationship between Book
and Page
entities.
//Data\Book.cs
@using EvenCart.Core.Data;
using System.Collections.Generic;
public class Book : FoundationEntity
{
public string Title {get; set;}
public string Isbn {get; set;}
public string Author {get;set;}
//property defined as virtual so it's not mapped to any database property
public virtual IList<Page> Pages {get;set;}
}
//Data\Page.cs
public class Page : FoundationEntity
{
public int BookId {get;set;}
public string PageNumber {get;set;}
public string Content {get; set;}
//property defined as virtual so it's not mapped to any database property
public virtual Book Book {get;set;}
}
Note - You should only create domain classes for entities that are specific to your plugin.
Next steps
Once your've created your domain classes, you can move on to create database versions.