Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Static Repository?

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?

like image 547
Seriphos Avatar asked Dec 18 '12 14:12

Seriphos


3 Answers

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

  • Output Caching
  • Application Caching
  • Database caching

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.

like image 94
Nexus23 Avatar answered Oct 06 '22 13:10

Nexus23


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

like image 22
mojo722 Avatar answered Oct 06 '22 11:10

mojo722


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.).

like image 2
Paul Tyng Avatar answered Oct 06 '22 12:10

Paul Tyng