-
Notifications
You must be signed in to change notification settings - Fork 1
Creating AdditionalSteps
Eros Stein edited this page Jan 6, 2017
·
2 revisions
Additional steps are useful when we need to execute actions every time a record is created/modified/removed. Let's say that for every User that is created we need to cryptograph the password, and set the creation date. This is how we would do it:
- Right-click your AdditionalSteps folder inside the Domain project and select
Add > New Item - Select Blayer and choose Additional Steps
- Use the same name you used for the POCO class
A file is created with a similar content to this one:
using Blayer.Data;
using Your.Namespace.Poco;
namespace Your.Namespace.Domain.AdditionalSteps
{
/// <summary>
/// Additional steps to be executed for the entity User
/// </summary>
public class UserAdditionalSteps : IAdditionalStep
{
/// <summary>
/// Execute additional steps for User
/// </summary>
/// <param name="state">Entity's current state</param>
/// <param name="entityObject">Entity</param>
/// <param name="originalEntity">Unmodified entity</param>
public void Execute(System.Data.Entity.EntityState state, object entityObject, object originalEntity)
{
User entity = (User)entityObject;
User dbEntity = originalEntity as User;
AppContext ctx = entity.Context;
switch (state)
{
// If the entity was added (data creation)
case System.Data.Entity.EntityState.Added:
{
}
break;
// If the entity was removed (data removal)
case System.Data.Entity.EntityState.Deleted:
{
}
break;
// If the entity was updated (data update)
case System.Data.Entity.EntityState.Modified:
{
}
break;
default:
break;
}
}
}
}Then we add this code to the case System.Data.Entity.EntityState.Added:
var pwd = Tools.GetEncryptedPassword(entity.Password);
entity.Password = pwd;
entity.CreationDate = DateTime.Now;Now every time we add a new record of type User the password will be encrypted and the property CreationDate will be set.