Pregunta

Im hooking into the MemberService.Saved event to trigger some indexing using ElasticSearch whenever a members details are updated via the backoffice or our custom MVC form. I also want to do this when a new member is added.

The problem is that this event seems to be called a lot of different times; ie during calls to MembershipHelper.Login and MembershipHelper.GetCurrentMemberProfileModel and other operations involving the MemberService. Not sure why the Umbraco core does a save every time a member is accessed but its causing my indexing process to trigger more times than necessary.

Is there a better way of intercepting inserts/updates on members and triggering my indexing process?

Kind regards

Ben

¿Fue útil?

Solución

The MemberService.Saving and MemberService.Savedevents are typically triggered when something like the LastLoginDate is updated, which is what is happening when you use MembershipHelper.Login.

What you can do is to check certain properties and check if they are dirty (meaning they have been changed) before you go ahead with your indexing process.

You can check if the member object that is passed to the Saved event is new by using the IsNewEntity() extension method as described here: http://our.umbraco.org/documentation/Reference/Events-v6/determining-new-entity

And if the member object isn't new you can iterate the properties and check if only the LastLoginDate is dirty, in which case you probably don't want to trigger the indexer.

Here is an example:

public class RegisterEvents : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        MemberService.Saved += MemberService_Saved;
    }

    void MemberService_Saved(IMemberService sender, Core.Events.SaveEventArgs<IMember> e)
    {
        foreach (var member in e.SavedEntities)
        {
            if (member.IsNewEntity())
            {
                //This is a brand new member object
                //Trigger indexing
            }
            else
            {
                var dirtyMember = (ICanBeDirty) member;
                var dirtyProperties = member.Properties.Where(x => x.IsDirty()).ToList();
                if (dirtyMember.IsDirty() || dirtyProperties.Count() > 1)
                {
                    //More then one property or the member object itself is dirty
                    //so we know that its not only LastLoginDate that is changed
                }
            }
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top