NOODB: Save When Nested

Suppose that you have a Movie Review object that contains an AUTHOR and a MOVIE.

When you write a review, you want to say “the author is Jay” and “the movie is highlander”. When you save the Movie Review, though, you just want to save the review, not the Movie or the Author. Those 2 things didn’t change; you’re just referencing them.

One way to handle that is to put some information on the relationship between the objects. “When the parent is saved, don’t save this child”. That may become necessary for future scenarios. But, for current scenarios, we can simplify it. If someone’s adding a new Author or Movie, then swell. Go ahead and add it. But, if it is just referenced by another object, then don’t save it. This rule is called “Save When Nested”. It defaults to 1; it will save. But, for the objects that’ll be nested, set it to zero.

Another factor is the properties of the child object. If the child saves when you save the parent, then the child has to be fully populated. In the case of AUTHOR, you would have to set the NAME (key) and the TITLE. Otherwise, title will get wiped out. We can’t have that.

Default Behavior – No Rule

        public void ShouldUpdateByDefault()

            IClient client = new Client(new Server(TestUtility.DefaultUnityContainer));

            client.Save(new Author { Name = "Jay", Title = "Some Random Guy" });
            client.Save(new Movie { Name = "Highlander", Director = "Russell Mulcahy" });
            MovieReview review = new MovieReview
                // just specify the key.
                // as is, it will overwrite the author. TITLE isn't specfied, so it will be overwritten with null.
                Author = new Author { Name = "Jay" },
                Movie = new Movie { Name = "Highlander" }                


            MovieReview fromDb = client.GetObjects<MovieReview>()[0];
            // todo: the values should be NULL, not EMPTY

            Assert.AreEqual("Jay", fromDb.Author.Name);
            Assert.AreEqual(string.Empty, fromDb.Author.Title);

            Assert.AreEqual("Highlander", fromDb.Movie.Name);
            Assert.AreEqual(String.Empty, fromDb.Movie.Director);

At the beginning, we create an author named JAY. When we save the movie review, we say we want to refer to that object. But, there it only specfies the name. So, when it saves, it’s going to overwrit the title with NULL. (Actually, empty space currently, but should be null). That’s not good for 2 reasons:

  1. We shouldn’t be saving at all; it’s wasteful. We only care about the reference.
  2. We don’t have all the information we need (title), so when we save, it’s wiped out.

Set the Behavior to Not Save When Nested

So, how do we treat these guys like referencees? Set the  “SaveWhenNested” property to FALSE.

            client.SetObjectRule<Author>("SaveWhenNested", "0");
            client.SetObjectRule<Movie>("SaveWhenNested", "0");

Now, you can assert to see that the values of the non-key properties have not been lost.

            Assert.AreEqual("Jay", fromDb.Author.Name);
            Assert.AreEqual("Some Random Guy", fromDb.Author.Title);

            Assert.AreEqual("Highlander", fromDb.Movie.Name);
            Assert.AreEqual("Russell Mulcahy", fromDb.Movie.Director);

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: