Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ran Chen 40 posts 117 karma points
    May 05, 2017 @ 13:58
    Ran Chen
    0

    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!

  • Craig Mayers 164 posts 508 karma points
    May 17, 2017 @ 15:20
    Craig Mayers
    0

    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:

    Dictionary<TKey, TValue>
    

    OR in your caseDictionary<int, string> and then you can pass this around your application.

    Logic will be specific to your project.

    Craig

  • Ran Chen 40 posts 117 karma points
    May 18, 2017 @ 08:39
    Ran Chen
    0

    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:

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

  • linehammer 2 posts 72 karma points
    Mar 05, 2020 @ 10:24
    linehammer
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft