C# 合并类的有效方法
我有这门课:C# 合并类的有效方法,c#,reflection,C#,Reflection,我有这门课: public class CompetitionBindingViewModel { [Required] public string Name { get; set; } public string Description { get; set; } } public class CompetitionViewModel : CompetitionBindingViewModel { public int Id { get; set; }
public class CompetitionBindingViewModel
{
[Required]
public string Name { get; set; }
public string Description { get; set; }
}
public class CompetitionViewModel : CompetitionBindingViewModel
{
public int Id { get; set; }
public string Logo { get; set; }
public string BackgroundImage { get; set; }
public string ColourScheme { get; set; }
public bool Current { get; set; }
public CompetitionViewModel(Competition model)
{
this.Id = model.Id;
this.Name = model.Name;
this.Description = model.Description;
this.Logo = model.Logo;
this.BackgroundImage = model.BackgroundImage;
this.ColourScheme = model.ColourScheme;
this.Current = model.Current;
}
}
如您所见,这里有2个视图模型。一个是当第一次创建竞赛时(只需提供名称和说明)。在项目后期,我想分配徽标、背景图像和配色方案,但这些不是必需的。
我还有一个当前值,默认设置为False。
我有一个函数处理所有这些:
[Route("")]
public async Task<IHttpActionResult> Put(CompetitionViewModel model)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
try
{
var competition = await this.service.GetAsync(model.Id);
competition.Name = (string.IsNullOrEmpty(model.Name)) ? competition.Name : model.Name;
competition.Description = (string.IsNullOrEmpty(model.Description)) ? competition.Description : model.Description;
competition.Logo = (string.IsNullOrEmpty(model.Logo)) ? competition.Logo : model.Logo;
competition.BackgroundImage = (string.IsNullOrEmpty(model.BackgroundImage)) ? competition.BackgroundImage : model.BackgroundImage;
competition.ColourScheme = (string.IsNullOrEmpty(model.ColourScheme)) ? competition.ColourScheme : model.ColourScheme;
competition.Current = (model.Current == competition.Current) ? competition.Current : model.Current;
this.service.Update(competition);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
return await base.SaveChanges();
}
我试图做的是检查属性是否已更改。如果已更改,则获取新值,否则将其忽略
有人能帮我提供一个更好的解决方案吗?因为这种情况经常发生(我有其他类和函数做同样的事情,但具有不同的属性)
我更喜欢而不是使用库,因为我不喜欢有库,当库存在时使用一点点库来解决多种情况。你不能创建一个助手方法或什么吗?不是说它很漂亮或有任何错误检查,但这是有效的:
public static void UpdateValueIfChanged<TData,TValue>(TData data, Expression<Func<TData,TValue>> propFunc, TValue newValue)
{
var prop = (MemberExpression)propFunc.Body;
var propInfo = (PropertyInfo)prop.Member;
Func<TData, TValue> getFunc = propFunc.Compile();
TValue originalValue = getFunc(data);
if (!EqualityComparer<TValue>.Default.Equals(originalValue,newValue))
{
propInfo.SetMethod.Invoke(data, new object[] { newValue });
}
}
public static void UpdateValueIfChanged(TData数据、表达式propFunc、TValue newValue)
{
var prop=(MemberExpression)propFunc.Body;
var propInfo=(PropertyInfo)prop.Member;
Func getFunc=propFunc.Compile();
TValue originalValue=getFunc(数据);
如果(!EqualityComparer.Default.Equals(originalValue,newValue))
{
调用(数据,新对象[]{newValue});
}
}
您可以像
UpdateIfChanged(competition c=>c.Name,model.Name)
“我不喜欢使用库,因为我不喜欢有库”请停止使用.net framework,因为它是一个旨在解决多种情况的大型库。请停止使用Windows,因为它是一个旨在解决多种情况的操作系统。请停止使用Intel x86系列通用中央处理器,因为它旨在解决多种情况。请因此,请阅读HTTP规范,因为您完全滥用了将
关键字放在您要查找的补丁
。以及Web API库中的Delta
类。很抱歉,您不想使用库…感谢您的回复。它非常有用。我完全了解强制库。我不想要的是大量的库用于处理小任务,因为当它(希望)可以用单个类或函数修复时,它会变得巨大。至于滥用PUT,请看一看:您确实意识到,您已经在使用ASP.net Web API,这绝不是一个强制的库。
public static void UpdateValueIfChanged<TData,TValue>(TData data, Expression<Func<TData,TValue>> propFunc, TValue newValue)
{
var prop = (MemberExpression)propFunc.Body;
var propInfo = (PropertyInfo)prop.Member;
Func<TData, TValue> getFunc = propFunc.Compile();
TValue originalValue = getFunc(data);
if (!EqualityComparer<TValue>.Default.Equals(originalValue,newValue))
{
propInfo.SetMethod.Invoke(data, new object[] { newValue });
}
}