ASP.NET MVC5数据库实体的通用转换方法

时间:2014-03-14 21:18:19

标签: asp.net-mvc generics asp.net-mvc-5

对于数据库实体,我有两种方法可以获得所选语言的翻译(如果有的话)或默认语言翻译。

public string GetName(int? LanguageId, int? DefaultLanguageId)
{
    string retval = "";
    var textInSelectedLanguage = this.CategoryTexts.Where(w => w.LanguageId == LanguageId).SingleOrDefault();
    if (textInSelectedLanguage == null)
    {
        retval = this.CategoryTexts.Where(w => w.LanguageId == DefaultLanguageId).SingleOrDefault().Name;
    }
    else
    {
        retval = textInSelectedLanguage.Name;
    }
    return retval;
}

public string GetDescription(int? LanguageId, int? DefaultLanguageId)
{
    string retval = "";
    var textInSelectedLanguage = this.CategoryTexts.Where(w => w.LanguageId == LanguageId).SingleOrDefault();
    if (textInSelectedLanguage == null)
    {
        retval = this.CategoryTexts.Where(w => w.LanguageId == DefaultLanguageId).SingleOrDefault().Description;
    }
    else
    {
        retval = textInSelectedLanguage.Description;
    }
    return retval;
}

正如您所看到的,它们非常相似。如果我有更多属性要翻译,这不是一个好的实现。其他翻译的行为类似。

如何将此代码减少为一种方法?

我尝试使用反射,但我没有任何结果。

...后来

我将代码缩减为一个方法,该方法返回一个实体实例,其中包含所选语言或默认语言的所有属性:

public CategoryText GetTranslation(int? DesiredLanguageId, int? DefaultLanguageId)
{
    CategoryText retval = null;
    var textInSelectedLanguage = this.CategoryTexts.Where(w => w.LanguageId == DesiredLanguageId).SingleOrDefault();
    if (textInSelectedLanguage == null)
    {
        retval = this.CategoryTexts.Where(w => w.LanguageId == DefaultLanguageId).SingleOrDefault();
    }
    else
    {
        retval = textInSelectedLanguage;
    }
    return retval;
}

我认为通过尝试找到用任何其他DbSet数据库实体替换CategoryTexts Dbset的方法,可以很容易地使这种方法变得通用。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

假设你有一个合适的基类,你可以写它: -

public T GetTranslation<T>(DbSet<T> set, int? DesiredLanguageId, int? DefaultLanguageId)
    where T:SomeBaseClassThatHasPropertyLanguageId
{
    return 
       set.SingleOrDefault(w => w.LanguageId == DesiredLanguageId) ??
       set.SingleOrDefault(w => w.LanguageId == DefaultLanguageId);
}

答案 1 :(得分:0)

我以某种方式解决了这个问题。

我有一个国际化类,它使用以下方法:

public static T GetDbEntityTranslation<T>(ITranslatable Entity)
{
    return (T)Entity.GetTranslation<T>(GetDefaultLanguage().Id, GetChosenLanguage().Id);
}

ITranslatable接口:

public interface ITranslatable
{
    T GetTranslation<T>(int? DesiredLanguageId, int? DefaultLanguageId);
}

我的类别类:

public partial class Category : ITranslatable
{
    private LSEntities db = new LSEntities();

    public T GetTranslation<T>(int? DesiredLanguageId, int? DefaultLanguageId)
    {
        CategoryText retval = null;
        retval = db.CategoryTexts.Where(w => w.LanguageId == DesiredLanguageId && w.CategoryId == this.Id).SingleOrDefault()
            ?? this.CategoryTexts.Where(w => w.LanguageId == DefaultLanguageId && w.CategoryId == this.Id).SingleOrDefault()
            ?? this.CategoryTexts.Where(w => w.CategoryId == this.Id).FirstOrDefault();
        if (retval == null)
            throw new Exception("No translation found for this object");
        return (T)(object)retval;
    }
}

问题是我需要根据存储在ID课程中的类别Category来获取我的翻译。我的翻译在CategoryTexts属性中搜索。