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