-
Notifications
You must be signed in to change notification settings - Fork 1
Creating Notifications
Eros Stein edited this page Jan 6, 2017
·
2 revisions
Notifications are triggered every time a record is successfully created/modified/removed in the database. This is how we use it:
- Right-click your Notifications folder inside the Domain project and select
Add > New Item - Select Blayer and choose Notify
- 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.Notifications
{
/// <summary>
/// Notifications for entity User
/// </summary>
public class UserNotify : INotify
{
/// <summary>
/// Notifications for User
/// </summary>
/// <param name="state">Entity's current state</param>
/// <param name="entityObject">Entity</param>
/// <param name="originalEntity">Unmodified entity</param>
public void Notify(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;
}
}
}
}Let's say every time the user modifies the password we want to send an email to him so he can take appropriate actions in case this was not intended or not done by him. Then we'd add this code to the case System.Data.Entity.EntityState.Modified:
if (!string.IsNullOrWhiteSpace(entity.Password))
{
var pwd = Tools.GetEncryptedPassword(entity.Password);
if (dbEntity.Password != pwd)
{
// send email to user information about password change
}
}After that is done we need to go back to our repository class and tell it to use this notification. Like this:
using Blayer.Data;
using Your.Namespace.Poco;
namespace Your.Namespace.Domain.Repositories
{
public class UserRepository : Repository<User>
{
public override INotify GetNotify()
{
return new Notifications.UserNotify();
}
}
}That's it, now Blayer.Data knows about this customization.