Trying to find out the best way to tackle this issue:
Assuming I need one or internal dictionary (like the c# key-value dictionaries), for example a dictionary that stores a list of people names and their ids.
I will need an API for adding / removing / editing and searching an item by ID and of course I will need these items to be stored in the db so they could be used in all pages.
for example I will create a service that reads an external list of people and their IDs and populate this "dictionary"
something like Dictionary.AddValue(100, "John Doe") accordingly.
On my views Razor pages I could use something like @Dictionary.GetValue(100) which will generate "John Doe" html string.
At first I was thinking of using Umbraco Dictionary but it seems that its API is very limited.
I think you are mixing up Umbraco's Dictionary with a normal c# Dictionary...
Umbraco's Dictionary is for making your site multi-lingual (backend & frontend). You can change labels and button text etc. to a different language based on Culture and translations you have set up in the Dictionary.
See the following article for more information on this:
I am aware the Umbraco Dictionary main purpose is for giving multilingual support.
What I was thinking is reusing this mechanism in order to have its advantages: core support, persistence in all view pages, etc .
Apparently its API is quite limited.
My current solution for having a global dictionary that can be accessed from all pages and views:
Creating a new Document type with a text field to store the
dictionary values in json format.
Creating a static method to get an
instance of the dictionary
.
public static Dictionary<string, string> GetNamesDictionary
This method tries to read a Dictionary object from HttpRuntime.Cache
If the cache is empty, it initializes the Dictionary object - reading the json value, parsing it into key/values pairs and populating the Dictionary.
That's how I have a global dictionary which is stored in Umbraco database.
Try IDictionary ...the difference is that Dictionary is concrete implementation while IDictionary is just a contract, abstraction. If IDictionary is a "more generic" type than Dictionary then it makes sense to use the more generic type in declaring variables. It is a good idea to abstract yourself from an implementation, by that I mean programming against an interface rather than a concrete implementation.
Building and using a custom dictionary
Trying to find out the best way to tackle this issue:
Assuming I need one or internal dictionary (like the c# key-value dictionaries), for example a dictionary that stores a list of people names and their ids.
I will need an API for adding / removing / editing and searching an item by ID and of course I will need these items to be stored in the db so they could be used in all pages.
for example I will create a service that reads an external list of people and their IDs and populate this "dictionary" something like
Dictionary.AddValue(100, "John Doe")
accordingly.On my views Razor pages I could use something like
@Dictionary.GetValue(100)
which will generate "John Doe" html string.At first I was thinking of using Umbraco Dictionary but it seems that its API is very limited.
Anyone got any direction ?
Thanks!
Hi Ran,
I think you are mixing up Umbraco's Dictionary with a normal c# Dictionary...
Umbraco's Dictionary is for making your site multi-lingual (backend & frontend). You can change labels and button text etc. to a different language based on Culture and translations you have set up in the Dictionary.
See the following article for more information on this:
https://24days.in/umbraco-cms/2013/the-dictionary-secrets/
I believe you want a custom dictionary:
OR in your case
Dictionary<int, string>
and then you can pass this around your application.Logic will be specific to your project.
Craig
Hi Craig, Thanks for your comment.
I am aware the Umbraco Dictionary main purpose is for giving multilingual support.
What I was thinking is reusing this mechanism in order to have its advantages: core support, persistence in all view pages, etc . Apparently its API is quite limited.
My current solution for having a global dictionary that can be accessed from all pages and views:
.
This method tries to read a Dictionary object from
HttpRuntime.Cache
If the cache is empty, it initializes the Dictionary object - reading the json value, parsing it into key/values pairs and populating the Dictionary.That's how I have a global dictionary which is stored in Umbraco database.
Try IDictionary ...the difference is that Dictionary is concrete implementation while IDictionary is just a contract, abstraction. If IDictionary is a "more generic" type than Dictionary then it makes sense to use the more generic type in declaring variables. It is a good idea to abstract yourself from an implementation, by that I mean programming against an interface rather than a concrete implementation.
is working on a reply...