简体   繁体   English

在哪里/何时填充查找值?

[英]Where/when to populate lookup values?

I've tried googling for two days but can't seem to find an answer. 我已经尝试了两天的谷歌搜索,但似乎找不到答案。

I'd like the Category class to provide the description based on the id entered and return an error if the id is not valid. 我希望Category类根据输入的ID提供描述,如果ID无效,则返回错误。 Is this the best approach? 这是最好的方法吗?

public class Category
{
    private int _id;
    private string _desc;

    public Category(int id)
    {
        ID = id;
    }

    public int ID 
    {
        get 
        {
            return _id;
        }

        set 
        {
            _id = value;

            //_desc = value from data access layer or throw error if the ID is invalid               
        }
    }

    public string Description 
    {
        get 
        {
            return _desc;
        }       
    }
}

public class Person
{
    public int ID {get; set;}

    public Category Category {get; set;}
}

public class MyApp
{
    static void Main()
    {
        Person p = new Person();

        Category c = new Category(2);

        p.Category = c;
    }
}

Since there will be potentially several instances of the class Category, it would be a waste memory-wise to include the look up values in the class itself. 由于可能存在类Category的多个实例,因此在类本身中包含查找值可能会浪费内存。 Instead they should be accessed elsewhere. 相反,应该在其他位置访问它们。 For instance a static function in another class. 例如,另一个类中的静态函数。

public class CategoryHelper
{
    public static string GetCategoryDesc(int CatgeoryId)
    {
        ...access database to get description
    }
}

Which we could use in the Description getter in the Category class: 我们可以在Category类的Description getter中使用它:

public string Description 
{
    get 
    {
        return CategoryHelper.GetCategoryDesc(this.ID);
    }       
}

Now, since we have the GetCategoryDesc in a separate class we can now optimize it for performance. 现在,由于我们在单独的类中拥有GetCategoryDe​​sc,我们现在可以对其进行优化以提高性能。 For instance, if you are fairly certain that the values for the lookup won't change for the duration of the run you can cache the descriptions in memory to avoid DB trips. 例如,如果您确定运行期间查找的值不会改变,则可以将描述缓存在内存中以避免DB跳闸。 In the following code we only call the DB the first time the call is made and the the results are cached. 在下面的代码中,我们仅在首次调用时才调用DB,并且将结果缓存。 This is called "memoization". 这称为“记忆化”。

public class CategoryHelper
{
    Dictionary<int,string> cachedDesc; //Dictionary used to store the descriptions
    public static string GetCategoryDesc(int CatgeoryId)
    {
        if (cachedDesc==null) cachedDesc = new Dictionary<int,string>(); // Instatiate the dictionary the first time only
        if(cachedDesc.ContainsKey(CatgeoryId)) //We check to see if we have cached this value before
        {
            return cachedDesc[CatgeoryId];
        }
        else
        {
            var description = .... get value from DB
            cachedDesc.add(CatgeoryId, description); //Store the value for later use
            return description;
        }
    }
}

You can make this simpler and even more complex, and since it is isolated in its own function you will have to do little to no changes elsewhere. 您可以使它变得更简单甚至更复杂,并且由于它是独立存在的功能,因此您在其他地方几乎不需要做任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM