Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended Pattern for Lazy-loading Portions of Object Graph from Cache

Tags:

c#

.net

ninject-2

I'm using memcache behind a web app to minimize the hits to our SQL database. I'm storing C# objects into this cache by marking them with SerializableAttribute. We make heavy use of dependency injection via Ninject in our app.

Some of these objects are large, and I'd like to break them up. However, they come from a single stored procedure call (i.e. one stored procedure call gets cooked into the full object graph), and I'd like to be able to break these objects up and lazy-load specific subgraphs from the cache separately rather than load the entire object graph into memory all at once.

What are some patterns that would help me accomplish this?

like image 830
FMM Avatar asked Feb 08 '12 20:02

FMM


1 Answers

As far as patterns go, I'd say the one large complex object that's built from a single stored procedure is suspect. I'm not sure if your caching is a requirement or just the current state of its implementation.

The pattern that I'm used to is a type of repository pattern, using operations that fill specific contracts. And those operations house one or many datasources that call stored procedures in the database that will be used to build ONE of those sub-graphs you speak of. With that said, if you're going to lazy load data from a database, then I can only assume that many of the object members are not used much of the time which furthers my point - break that object up.

enter image description here

A couple things about it:

  • It can be chatty if the entire object is being used regularly
  • It is fully injectable via the Operations
  • The datasources contain the reader for the specific object, thus only performing ONE task (SOLID)
  • Can be modified to use Entity Framework, without too much fuss
  • Can be designed to implement an interface, making it more reusable
  • Will require you to break up that proc into smaller, chewable pieces, which will likely only benefit you in the long run.
  • The complex object shown in this diagram really shouldn't exist if only parts of it are going to be used. Instead, consider segregating those objects. However, it really depends on how this object is being used.

UPDATE:

Using your cache as the repository, I would probably approach it like this:

enter image description here

So basically, you store the legacy object, but in your operations, you use them to build more relavent DTOs that are returned to the client.

like image 184
Sinaesthetic Avatar answered Oct 21 '22 22:10

Sinaesthetic