﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Daniel commented on Handling entities validations in RavenDB</title><description>The dinner was loaded from database and edited, but some business validation over it throws exception.
This exception is handled at the Application Level, and because of this, the end of the request calls de SaveChanges.
I used the memento pattern to restore the state of the entity at the context of the exception handling to prevent an update at the SaveChanges.
Maybe there is a better way to handle this scenario. For example, using a TransactionScope around this operation. In this case, RavenDB could have a VoteCommit and VoteRollback to control SaveChanges (similar to ActiveRecord.TransactionScope)
</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment18</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment18</guid><pubDate>Sat, 10 Nov 2012 11:15:15 GMT</pubDate></item><item><title>Ayende Rahien commented on Handling entities validations in RavenDB</title><description>Daniel,
What do you need a RestoreState feature? Just don't save the diner</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment17</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment17</guid><pubDate>Sat, 10 Nov 2012 10:17:43 GMT</pubDate></item><item><title>Daniel commented on Handling entities validations in RavenDB</title><description>Sure. Post a comment here because didn't find an approach to the business validation context.

In the case that a existing Dinner is edited and then validated before SaveChanges, I think that we could have a "RestoreState" feature (something like memento pattern).

Don't you think that could be useful?</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment16</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment16</guid><pubDate>Fri, 09 Nov 2012 09:42:18 GMT</pubDate></item><item><title>Ayende Rahien commented on Handling entities validations in RavenDB</title><description>Daniel,
There is generally a distinction between business style validation (you can't order more than 5 items unless you have a Gold membership, or you can't schedule an event twice) vs. data validation (field is not null, age is valid, etc).
This post talks about data validation, not business validation.</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment15</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment15</guid><pubDate>Fri, 09 Nov 2012 02:16:32 GMT</pubDate></item><item><title>Daniel commented on Handling entities validations in RavenDB</title><description>But there are other types of validations that involves other object instances at some the execution context. Let's suppose that in a Nerd Dinner like app, you cannot schedule ther same dinner twice.

In this case, we cannot "register" the validation, because it's in a business service.

Thoughts?</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment14</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment14</guid><pubDate>Fri, 09 Nov 2012 00:57:51 GMT</pubDate></item><item><title>Ayende Rahien commented on Handling entities validations in RavenDB</title><description>Uwe,
It is there, it isn't on the IDocumentStore interface, but it is on the DocumentStore impl.</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment13</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment13</guid><pubDate>Tue, 06 Nov 2012 09:11:05 GMT</pubDate></item><item><title>Uwe commented on Handling entities validations in RavenDB</title><description>store.RegisterListener(validationListener); ???

I have no such RegisterListener. Maybe this is for a newer version? My version is 960</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment12</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment12</guid><pubDate>Tue, 06 Nov 2012 09:00:36 GMT</pubDate></item><item><title>Ayende Rahien commented on Handling entities validations in RavenDB</title><description>David,
If an exception is raised from the BeforeStore method, SaveChanges will NOT happen.</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment11</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment11</guid><pubDate>Thu, 11 Oct 2012 00:12:15 GMT</pubDate></item><item><title>Ayende Rahien commented on Handling entities validations in RavenDB</title><description>Rangoric,
Certainly, if you noted, I added code to handle typing explicitly.</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment10</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment10</guid><pubDate>Thu, 11 Oct 2012 00:11:32 GMT</pubDate></item><item><title>Ayende Rahien commented on Handling entities validations in RavenDB</title><description>Ugo,
Yes</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment9</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment9</guid><pubDate>Thu, 11 Oct 2012 00:10:53 GMT</pubDate></item><item><title>Duckie commented on Handling entities validations in RavenDB</title><description>The example code is for Data Annotations, and the validated class is:

    public class User : IPersistValidated
    {
        [Required]
        public string Username { get; set; }
        [Required]
        public PasswordString Password { get; set; } // just class so manage hashing
    }
</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment8</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment8</guid><pubDate>Wed, 10 Oct 2012 20:40:37 GMT</pubDate></item><item><title>Duckie commented on Handling entities validations in RavenDB</title><description>David: The exception is thrown at SaveChanges(), so nothing is stored.</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment7</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment7</guid><pubDate>Wed, 10 Oct 2012 20:38:36 GMT</pubDate></item><item><title>Duckie commented on Handling entities validations in RavenDB</title><description>This works fine. The interface is just to ensure that objects that do not need validation are skipped. 

    public interface IPersistValidated
    {}

    public class ValidationListener : IDocumentStoreListener
    {
        public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
        {
            if (entityInstance is IPersistValidated)
            {
                var context = new ValidationContext(entityInstance, null, null);

                Validator.ValidateObject(entityInstance, context);
            }
            return true;
        }

        public void AfterStore(string key, object entityInstance, RavenJObject metadata)
        {
        }
    }

Beware, this is a 1 minute experiment.</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment6</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment6</guid><pubDate>Wed, 10 Oct 2012 20:33:01 GMT</pubDate></item><item><title>David Boike commented on Handling entities validations in RavenDB</title><description>This is great! I've finished a RavenDB project and had no idea this feature existed!

It does raise one question for me though.

I could see implementing a custom user validation exception that would store all the different issues with a model, so you could easily catch that exception and translate it to a ValidationSummary type of control in a web application.

However, if you're using a one session per request model, and you trap the exception, the SaveChanges() is still going to occur. How do you deal with that? Does the Listener just make it so that the update to that model never took place, but all the other activity on the pages proceeds normally?</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment5</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment5</guid><pubDate>Wed, 10 Oct 2012 14:15:42 GMT</pubDate></item><item><title>Rangoric commented on Handling entities validations in RavenDB</title><description>Oh there goes another chunk of code. I've been manually validating things with Data Annotations, this lets me get rid of that step altogether.

However, is it possible to register a generic validator? I have a single Validator already that takes any object and checks the data annotations.</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment4</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment4</guid><pubDate>Wed, 10 Oct 2012 13:20:41 GMT</pubDate></item><item><title>Alexander Nyquist commented on Handling entities validations in RavenDB</title><description>@ugo Yes, check out IPreInsertEventListener and IPreUpdateEventListener.</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment3</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment3</guid><pubDate>Wed, 10 Oct 2012 12:14:27 GMT</pubDate></item><item><title>Apostol commented on Handling entities validations in RavenDB</title><description>And there is also the question of whether "validation by exception" is a proper way of doing validation...</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment2</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment2</guid><pubDate>Wed, 10 Oct 2012 11:44:09 GMT</pubDate></item><item><title>ugo commented on Handling entities validations in RavenDB</title><description>exists same easy way to use nhibernate validator?</description><link>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment1</link><guid>http://ayende.com/159042/handling-entities-validations-in-ravendb#comment1</guid><pubDate>Wed, 10 Oct 2012 10:22:58 GMT</pubDate></item></channel></rss>