12

I have a couple of entities which will pretty much never change (i.e. a list of Countries, Regions etc.) And it got me thinking... would a static repository make sense for these entities in addition to the normal repositories? I say in addition, because these entities will still interact with other entities, and I still want them under a single Unit of Work so they share the same context.

However, I'm sure there is a better way to go about it. I'm new to ASP.NET and the .NET Framework, but is there a way to define application wide data that will be used throughout the life of the program? Is that the best way to go about it?

Or is it better to just define a static class with collections of these entities and I can just grab these from there when I need it?

jrummell
  • 42,637
  • 17
  • 112
  • 171
Seriphos
  • 369
  • 1
  • 2
  • 16

3 Answers3

8

You can use various storing (State Management) techniques. Follow below options for more information.

From sound of it, the two entities you are trying to store will not be memory intensive, so Application cache will be best bet here.

Community
  • 1
  • 1
Nexus23
  • 6,195
  • 9
  • 50
  • 67
  • Thanks! Indeed, those entities won't be memory intensive. Going to use System.Web.HttpContext.Current.Cache to store a dictionary. Another question: when would be a good time to store it? During Application start in Global.asax? – Seriphos Dec 18 '12 at 16:11
  • 1
    Yes, that can be one place to store the data or in your controller code as well, because Application cache object is available throughout application. – Nexus23 Dec 18 '12 at 16:47
  • Now that I think about it, it makes more sense in the controller.. at least for my purposes. Thank you. – Seriphos Dec 19 '12 at 12:57
3

Why not just use a caching mechanism? I've used memchache successfully in situations like this.

mojo722
  • 151
  • 4
  • Hmmm, would you be willing to point me to an article and/or a blog which discusses this mechanism? – Seriphos Dec 18 '12 at 15:08
  • This guy has a pretty good post on setting it up and using it. It is actually pretty easy to use. http://www.deanhume.com/Home/BlogPost/memcached-for-c----a-walkthrough/62 – mojo722 Dec 18 '12 at 15:18
  • 1
    ASP.NET AppFabric caching is a similar thing to memcached but from Microsoft and available on Azure, so maybe better depending on your hosting environment. – Paul Tyng Dec 18 '12 at 15:38
2

I think Nexus23's answer covers the majority of your options. But I find for some data like this (US States, Cities) sometimes its best to not maintain a list at all as its just something for you to deal with in the future. Usability wise as well having a list of 190+ countries (the UN and the US recognize different lists) is not ideal for a user to scroll through.

The user of the application most likely will know whats correct better than you anyway unless you plan on updating with the geopolitical changes of the world.

If you need this list for functions like address validation or geolocation its better to defer to a service that is meant for that instead of trying to reroll internally (Google Maps API, etc.).

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
  • "Usability wise as well having a list of 190+ countries (the UN and the US recognize different lists) is not ideal for a user to scroll through." Have to totally disagree here. GB is the country code for the UK, but if you leave it to the user they'll try to enter UK or lots of other incorrect data, and then you can't even tell that they're people in the same country. – Brian White Dec 18 '12 at 16:13
  • Yeah I wouldn't let it be free form without validation, but the usability argument is [well documented](http://www.useit.com/alertbox/annoyances.html), for validation use the google maps API or something that can also normalize, and then you don't need to follow the UN RSS feed and get country updates in to your list. – Paul Tyng Dec 18 '12 at 18:28
  • Whether you use a drop down or an autocomplete is up to you. But enforcing a referential constraint in the database is a necessary last step to keep your data sane, and google maps isn't available there. Maybe it's just been a while since I didn't have to deal with geopolitical changes. – Brian White Dec 21 '12 at 18:08