在抽象对象集合中更新对象的简洁方法
Clean way for updating object in a collection of abstract objects
当我在我的模型中开发带有本地化对象的 asp net core + ef core 2.0 时,我采用了以下 link 中提供的解决方案来本地化我的对象 link.
我现在正在尝试找到一种干净的方法来在控制器中收到更新的对象时更新我的翻译集合。
目前我有一个步骤模型 class 是这样定义的:
public class Step
{
//Native properties
public Guid ID { get; set; }
public string Name { get; set; }
public int Order { get; set; }
public string ScriptBlock { get; set; }
//Parent Step Navigation property
public Nullable<Guid> ParentStepID { get; set; }
public virtual Step ParentStep { get; set; }
//Collection of sub steps
public virtual ICollection<Step> SubSteps { get; set; }
//MUI Properties
public TranslationCollection<StepTranslation> Translations { get; set; }
public string Description { get; set; }
//{
// get { return Translations[CultureInfo.CurrentCulture].Description; }
// set { Translations[CultureInfo.CurrentCulture].Description = value; }
//}
public Step()
{
//ID = Guid.NewGuid();
Translations = new TranslationCollection<StepTranslation>();
}
}
public class StepTranslation : Translation<StepTranslation>
{
public Guid StepTranslationId { get; set; }
public string Description { get; set; }
public StepTranslation()
{
StepTranslationId = Guid.NewGuid();
}
}
Translation 和 translationCollection 与 link
中的相同
public class TranslationCollection<T> : Collection<T> where T : Translation<T>, new()
{
public T this[CultureInfo culture]
{
// indexer
}
public T this[string culture]
{
//indexer
}
public bool HasCulture(string culture)
{
return this.Any(x => x.CultureName == culture);
}
public bool HasCulture(CultureInfo culture)
{
return this.Any(x => x.CultureName == culture.Name);
}
}
public abstract class Translation<T> where T : Translation<T>, new()
{
public Guid Id { get; set; }
public string CultureName { get; set; }
protected Translation()
{
Id = Guid.NewGuid();
}
public bool HasProperty(string name)
{
return this.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Any(p => p.Name == name);
}
}
我在此示例中的问题是如何正确处理我的步进控制器的 PUT 方法和描述 属性。当它接收到要更新的 Step 对象时(这是通过本机 c# 客户端完成的),只有 Step 的字符串 Description 属性 可能是 created/updated/unchanged。所以我必须 update/create/do 没有关于正确文化翻译的描述。
我的第一个猜测是在 TranslationCollection class 中添加一个方法,我可以在其中传递文化、要更新或不更新的 属性 的名称(在本例中为描述)和描述的值。
但由于 TranslationCollection 是抽象对象的集合,即使这是个好主意,如果可能的话,我也不会。
如果有人对此有任何建议(希望我足够清楚)那就太好了!
终于回答了我自己的问题,也很简单。
只需像这样使用索引器:
myobject.Translations[userLang].Name = value;
当我在我的模型中开发带有本地化对象的 asp net core + ef core 2.0 时,我采用了以下 link 中提供的解决方案来本地化我的对象 link.
我现在正在尝试找到一种干净的方法来在控制器中收到更新的对象时更新我的翻译集合。
目前我有一个步骤模型 class 是这样定义的:
public class Step
{
//Native properties
public Guid ID { get; set; }
public string Name { get; set; }
public int Order { get; set; }
public string ScriptBlock { get; set; }
//Parent Step Navigation property
public Nullable<Guid> ParentStepID { get; set; }
public virtual Step ParentStep { get; set; }
//Collection of sub steps
public virtual ICollection<Step> SubSteps { get; set; }
//MUI Properties
public TranslationCollection<StepTranslation> Translations { get; set; }
public string Description { get; set; }
//{
// get { return Translations[CultureInfo.CurrentCulture].Description; }
// set { Translations[CultureInfo.CurrentCulture].Description = value; }
//}
public Step()
{
//ID = Guid.NewGuid();
Translations = new TranslationCollection<StepTranslation>();
}
}
public class StepTranslation : Translation<StepTranslation>
{
public Guid StepTranslationId { get; set; }
public string Description { get; set; }
public StepTranslation()
{
StepTranslationId = Guid.NewGuid();
}
}
Translation 和 translationCollection 与 link
中的相同public class TranslationCollection<T> : Collection<T> where T : Translation<T>, new()
{
public T this[CultureInfo culture]
{
// indexer
}
public T this[string culture]
{
//indexer
}
public bool HasCulture(string culture)
{
return this.Any(x => x.CultureName == culture);
}
public bool HasCulture(CultureInfo culture)
{
return this.Any(x => x.CultureName == culture.Name);
}
}
public abstract class Translation<T> where T : Translation<T>, new()
{
public Guid Id { get; set; }
public string CultureName { get; set; }
protected Translation()
{
Id = Guid.NewGuid();
}
public bool HasProperty(string name)
{
return this.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Any(p => p.Name == name);
}
}
我在此示例中的问题是如何正确处理我的步进控制器的 PUT 方法和描述 属性。当它接收到要更新的 Step 对象时(这是通过本机 c# 客户端完成的),只有 Step 的字符串 Description 属性 可能是 created/updated/unchanged。所以我必须 update/create/do 没有关于正确文化翻译的描述。
我的第一个猜测是在 TranslationCollection class 中添加一个方法,我可以在其中传递文化、要更新或不更新的 属性 的名称(在本例中为描述)和描述的值。
但由于 TranslationCollection 是抽象对象的集合,即使这是个好主意,如果可能的话,我也不会。
如果有人对此有任何建议(希望我足够清楚)那就太好了!
终于回答了我自己的问题,也很简单。
只需像这样使用索引器: myobject.Translations[userLang].Name = value;