Skip to content

Introduction to Models

In AventusSharp, models are primarily used to define the data schema rather than encapsulating business logic. Models represent the structure of your data and outline the properties of each entity in your application. They are fundamental components in the MVC (Model-View-Controller) pattern but are limited to describing the data structure alone.

In AventusSharp, defining a model is simple. By extending Storable<T>, where T is the model type itself, you can specify the schema of an entity without embedding additional logic or operations within the model. This keeps models as pure data representations, making them lightweight and easier to maintain.

Storable<T> only contains the property Id (int). If you need to store timestamp information you can use the class StorableTimestamp that has CreatedDate (DateTime) and UpdatedDate (DateTime).

Defining a Model Schema

For example, here is how to define a User model schema in AventusSharp:

public class User : Storable<User>
{
// Define data properties for the User model, such as Name, Age, Email, etc.
}

In this structure:

  • The User model only describes the data properties (schema) of a user.
  • There is no embedded business logic within the model, keeping it focused solely on data structure.

CRUD Operations

Though models themselves do not contain logic, AventusSharp provides built-in methods to perform basic CRUD operations with ease. Once a model is defined, you can perform the following operations externally:

  • Create a New Record
    To create a new instance of User and add it to storage:

    User u = new User();
    u.Create();
  • Retrieve All Records
    To retrieve all instances of the User model:

    var allUsers = User.GetAll();

You can learn more about CRUD in the next sections.

By defining models purely as data schemas, AventusSharp encourages a clean separation between data representation and business logic, making your application structure clearer and easier to manage.