logo

Managers of entities

Base manager

The base manager is created for the entity type created in the entity editor.

public class EntityManager<T, IdT> : AbstractNHEntityManager<T, IdT> where T : IEntity<IdT>
{
}

This manager already contains a specific set of methods for working with objects. The bas methods are the following:

// Load an object from the database by a unique identifier (ID type is configured in the entity editor)
// If the object with the given id does not exist, return an exception
public sealed override T Load (IdT id)

// Load an object from the database by a unique identifier (ID type is configured in the entity editor)
// If the object with the given id does not exist, return null
public sealed override T LoadOrNull (IdT id)

// Save the object in the database
public sealed override void Save (T obj)

// Write changes to the database
public sealed override void Update (T obj)

// Remove the object from the database
public sealed override void Delete (T obj)

// Remove all objects of this type
public override void DeleteAll ()

// Re-read the object from the database (in this case the cache is updated for this object)
public sealed override void Refresh (T obj)

// Get all objects of this type
public sealed override ICollection <T> FindAll ()

// Get all the objects that meet the filter (consider the filter, which is generated for the object type in the entity editor)
public sealed override ICollection <T> Find (Filter filter, FetchOptions fetchOptions)

In order to obtain the base manager for an object type the EntityManager<TEntity>.Instance is used.

Overriding base manager

If you need to override the default behavior of the manager, you need to write a new class and inherit it from the EntityManager. For example, suppose that there is a task to create a password and the "My Documents" folder for each new system user. To do this, in the manager for the object type User, override a method of saving an object in the database.

public class UserManager: EntityManager <User, long>
{
// Method to create the "My Documents" folder for the user 
private static void CreateFolders (User user)
{
if (MyDocumentFolderManager.Instance.LoadByUser (user) == null)
{
MyDocumentFolderManager.Instance.CreateForUser (user);
}
}

// Overrides the method of saving a user to the database
protected override void SaveEntity (User obj)
{
// If the object is new
if (obj.Id == 0)
// Encrypt Password
obj.Password = EncryptionHelper.GetMd5Hash (obj.Password);
// Call the base method of saving an object in the database
base.SaveEntity (obj);
if (obj.Id> 1)
// Create the folder "My Documents" (if it does not already exist)
CreateFolders (obj);
}
}