Entity validation with IStoreListener

44 views
Skip to first unread message

Håkan Edling

unread,
Oct 24, 2014, 5:14:11 PM10/24/14
to rav...@googlegroups.com
Hi there! I read the suggestion in this blog post (http://ayende.com/blog/159042/handling-entities-validations-in-ravendb) to wire up entity validations from the IStoreListener, and it seems like a great idea. However, I have another question regarding it. In some cases I need to perform a database lookup in the validation to ensure uniqueness of a certain member in the document (like a unique constraint). For example, I have the following in my repository for validation:

// Some regular validation
if (String.IsNullOrWhiteSpace(model.NewUrl))
    errors.Add(new ModelError("NewUrl", ModelError.ErrorType.Required, "NewUrl is required"));
else if (model.NewUrl.Length > 255)
    errors.Add(new ModelError("NewUrl", ModelError.ErrorType.StringLength, "NewUrl has a maximum length of 255 characters"));

// Validate uniqueness of member
if (session.Get<Models.Alias>(where: a => a.OldUrl == model.OldUrl && a.Id != model.Id).Count() > 0)
    errors.Add(new ModelError("OldUrl", ModelError.ErrorType.Duplicate, "Duplicate URL found"));

So my question is, is there anyway to get a hold of the current session in the BeforeStore method, or do you have any other suggestions? Making OldUrl document key does not count, as it doesn't work in this case ;)

Regards

/ Håkan

Kijana Woodard

unread,
Oct 24, 2014, 6:34:10 PM10/24/14
to rav...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Håkan Edling

unread,
Nov 26, 2014, 8:26:52 AM11/26/14
to rav...@googlegroups.com
No I hadn't actually! Thanks!

Regards

/Håkan

Reply all
Reply to author
Forward
0 new messages