1

Currently I generate a singleton object for my site which is created from a flat file. The object is never changed by the application, it is purely used as reference by some of the functions on my site. It effectively describes the schema for another file type we use, similar to XML & XSD

The singleton object generated is fairly large (5000+ child objects, with up to 500 properties each) and it contains circular references as child objects can reference the parent object due to 2 way references.

This all works fine currently, however the first time the app loads, it takes over a minute to generate the singleton. Which is the reason I am using a singleton here, so I don't have to regenerate it every request. But it also means every time the app pool restarts, the first request takes well over a minute to load. Every consecutive request is fast once the object is in memory.

Seeing that the flat file rarely changes, I would like to find a good way to generate the object once and store the object in way that I can quickly retrieve it when needed.

I tried serializing the object to json, and storing it in the database, however due to the circular references Json.net fails or I end up losing information if I configure it to ignore the references when serializing.

Are there any better ways of handling such an object better, or am I stuck to using the singleton for now?

JanR
  • 5,964
  • 3
  • 22
  • 30

1 Answers1

0

Given the static nature of this object, serialization would one option.

The circular reference issue you mention with Json.Net can be easily remedied using appropriate JsonSerializerSettings (refer to this answer)

If speed is of the essence, than you may want to investigate other serialization options (netserializer claims to be one of the fastest).

Ultimately though, you should look to put the file / object structure into some sort of cache that sits outside of the app pool (Redis perhaps), or even load the flat file's data into a well designed database schema (i.e. parent - child relationships etc).

Creating a massive object graph is rather inefficient and will potentially create memory issues.

Community
  • 1
  • 1
SlightlyMoist
  • 862
  • 5
  • 7
  • Thanks, I have tried the serializer settings, to no avail, I either get Stackoverflow exceptions or it refuses to serialize, I will have a look at Netserializer, hopefully that might work. Last resort I might have to look at creating a database schema, will let you know how I go – JanR Apr 21 '16 at 03:53