Skip to content

March 30, 2010

Entify is lazy by nature

They say that laziness is a virtue, not a deadly sin. When it comes to entity frameworks and programming in general, this is very true in many cases. Entify takes the lazy approach to solve the problem of storing entities into persistent store. When talking about software development, being lazy is not considered as a bad thing, unlike in our everyday life. Being lazy simply means that, you do what you need to do as late as possible. Don’t do anything before hand just in case, because usually its unnecessary work.

How lazy Entify really is?

As it turns out, very lazy! When the application that uses Entify is executed for the very first time, the database simply doesn’t exist yet. Database isn’t created until the entity context is created in the application for the first time. This action only creates an empty database file to the disk. There are no tables for entities at this point.

When the first entity is added to the context, Entify asks it’s internal type coordinator to prepare database for type of the received entity object. Type coordinator checks is the type already known and if not, it generates the tables needed to store the object into persistent store. Therefore, Entify database never contains tables just sitting and waiting for entities to show up. Instead, tables are created only when they are really needed. In fact, Entify doesn’t even know what types application may give it to it.

So, the database is generated on the fly as needed, but how about fetching entities from the entity context? Entities are fetched from the entity context by creating Entity requests, which are object oriented queries. Programmer can also get all the entities of given type by calling GetAll<EntityType>(); method of the EntityContext instance. This returns a list of entity objects. But what if entities have other entities as relations? Then all the related entities are lazy loaded and cached when needed. Below is a short code snippet with comments to show how it works.

ShoppingCart cart = new ShoppingCart();

context.Add(cart); // Generates tables for cart type

Product p1 = new Product() { Name = "Movie" };
Product p2 = new Product() { Name = "Candy" };

context.Add(p1, p2);

cart.Products.Add(p1); // ShoppingCart has to-many
cart.Products.Add(p2); // relation to Product type.

context.Update(cart);

Now somewhere in the application we need to handle those objects, so let’s load them into memory from entity context.

ShoppingCart[] carts = context.GetAll<ShoppingCart>();
ShoppingCart ourCart = carts[0]; // Our cart is the first one

// This activates lazy load to database, because products
// are not in memory yet. From the application's point of view
// lazy loading is completely transparent.
List<Products> products = ourCart.Products; 

// Products were loaded and cached in previous statement.
// No lazy load here.
List<Products> sameProducts = ourCart.Products;

That’s all I got to say about laziness this time. Take care, and don’t be as lazy as Entify!

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments