M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Generated Code

This lesson explains about the entity class code generated by M#. First, we will take a look at the generated code for an entire class, then a breakdown of the generated code to understand the purpose and usage of each method or property.

In our "HelloWorld" project we have two entities

1) "User" entity, an abstract class. (For more information about "Abstraction", please read lesson Transient, Interface, Abstract in chapter 2.)

2) "Employee" entity, which inherits from "User" entity. (For more information about "Inheritance", please read lesson Inheritance in chapter 2.)

Entities Developed and Customized in M#

 

The screenshots below show the M# entity structures of "User" and "Employee". (For more information about "Entity Model", please read the entity section of lesson Entity, Page, Module in chapter 1.)

 

The picture above demonstrates a "User" entity developed in M# which has the following configuration:

1) Is marked as an "Abstract" class.

2) Has two properties "Email" and "Password".

3) "Email" property is marked as "Unique" and has "Text Pattern" set as "Email Address.

4) Both properties are mandatory.

The picture above demonstrates an "Employee" entity developed in M# which has the following configuration:

1) Inherits "User" entity.

2) Has two properties "Email" and "Password" inherited from "User" entity.

3) Has one additional property "Name", which is marked as the "Default Property".

4) All Properties are marked as mandatory.

Code Generated by M#

The pictures below show the code generated by M# for the User and Employee entities.

Note: M# generated "Partial" classes for each entity type. For more information on partial classes, please read lesson Partial Classes and Business Logic in chapter 2.

Note: M# has a base class "GuidEntity", which is inherited by all the entities. For more information on M# core "Entities and Interface", please read chapter 3.

The picture above shows generated code for the User entity. The class has "Abstract" keyword and two simple properties as configured earlier in the lesson. If you remember previous M# Entity screenshots, we marked "Email and Password" properties of User with some attributes e.g. mandatory, unique, text pattern etc.

In order to implement those attributes, M# implements some methods, which are highlighted above and are discussed in detail later in this lesson.

The picture above shows generated code for the Employee entity class. The class is derived from User entity as highlighted above and has only one "Department" property as configured earlier in the lesson. The Employee class also has implemented functions highlighted above, which are discussed separately later in this lesson.

Note: "Email and Password" properties are inherited from "User" entity and are accessible by "Employee" entity.

Generated Entity Code Details

FindByEmail()

M# generates the method shown above for each property which is marked as "Unique". In our lesson above, we marked "Email" property of "User" entity as "Unique" so; M# generates this method with "Public Static" keywords to make it accessible anywhere in the project and is used to fetch a User record from the database by email address .

ToString()

This is a very important method. M# returns a property in this method, which is either set as unique, default, or picks the first string type property. This property is used for "String / Textual" representation of the entity instance and is used in various procedures e.g. comparing instances, displaying instances on UI etc.

The picture above shows the "ToString" method of the User entity, which returns "Email" property, because of the "Unique" attribute defined in the beginning of our lesson.

The picture above demonstrates the "ToString" method of the Employee entity, which returns "Name" property Although Employee inherits the User entity it doesn’t return "Email" because we marked "Name" as "Default Property" in our lesson and M# "Overrides" the inherited method.

Clone()

M# calls this method whenever an instance is updated, it is also a very important method used by other M# generated functionality.

ValidateProperties()

This method is called every time an entity instance is inserted or updated in the database and implements all the validation related attributes of the defined properties and Entity uniqueness rules configured in M#.

The above method implementation on User entity shows the validation attributes we configured in M#, shown in the beginning of this lesson e.g. Mandatory fields are checked by calling "IsEmpty (M# extension method)", Text pattern of "Email" is checked by validating against a regular expression, and uniqueness of "Email" is checked by calling "Database.Any (M# extension)" on User entity.

The above shown validation method is implemented on the Employee entity and calls base.ValidateProerties() method to validate all the inherited properties before validating any owned properties.