NOODB: Properties as Interfaces

To date, all of the examples have been with concrete classes. A MOVIE REVIEW has a MOVIE object and an AUTHOR object.

But, what if the property types are interfaces, not concrete classes?

In this example: MovieReview3 implements IMovieReview3. There are two object properties: IAuthor3 and IMovie3.

On the wiki todo list, I speculated that this would work without any changes. Obviously I didn’t think about it, because it didn’t work. I had to make 2 key changes.

Multiple Properties

The TypeRegistration builder was ignoring interfaces with a comment that it would have to be revisited. Tonight, I had to revisit. The problem is that the builder parses the type being registered and all its related types. If the properties are defined in the interface and the implementation, then it picks up the properties twice: one set from the implementation and one set from the interface. The quick work-around was to not parse the interfaces.

I changed it to not ignore interfaces. Now, it ignores properties in a class that are defined in either a base class or an interface. (the base class part isn’t tested yet). So, MOVIE and AUTHOR are associated to IMovieReview3, not MovieReview3. Now, there’s only one copy.

Xml Building

The server extracts the db from the database and builds an xml document that the deserializer expects. The construction of that xml was based on the object type metadata. The metadata says that the properties are interfaces, to it would come through as TYPE=IAUTHOR3. The deserializer would say “okey-doke”, and try to instantiate an instance of IAUTHOR3. Here’s the thing… you can instantiate an interface due to that pesky “it just doesn’t make sense” logic. You can only instantiate concrete classes.

To correct this, I:

  1. updated the DataProperty object. Added a ReferencedObjectTypeId property.
  2. updated the SqlObjectDataContext to update the new property.
  3. Changed the server to use ReferenecedObjectTypeId rather than the id from the meta data (which represents the interface).

The DataProperty object is now a 3 property bag. The Object Data Context populates this object with just the 3 pieces of information that NOODB needs to put everything together.

The Test

                IMovieReview3 review = new MovieReview3
                {
                    Author = new Author3a { Name = "Jay", Title = "hack" },
                    Comments = "swell",
                    Id = Guid.NewGuid(),
                    Movie = new Movie3 { Director = "director", Rating = "rating", Title = "title", Id = Guid.NewGuid() }
                };
                client.Save(review);

                IMovieReview3 fromDb = client.GetObjects<MovieReview3>()[0];
                Assert.IsInstanceOfType(fromDb.Movie, typeof(Movie3));
                Assert.IsInstanceOfType(fromDb.Author, typeof(Author3a));
                Assert.AreEqual("Jay", fromDb.Author.Name);
                Assert.AreEqual("hack", fromDb.Author.Title);
                Assert.AreEqual("director", fromDb.Movie.Director);

Author is an IAuthor3.

Movie is an IMovie3.

The test shows that the types that come out are the same types that went in, even though the property has no knowledge of the concrete class type.

I have to test the same functionality with abstract classes. After tonight’s changes, i think it will work, but I’ve said that before. Guess I’ll have to test it.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: