我有一个类产品,我想验证促销字段并根据价值更改价格。我已经在使用数据注释遵循可靠原则的最佳方法是什么?
` 使用系统;使用 System.ComponentModel.DataAnnotations;
命名空间 RefactoringTest.ProductService.Entities { public class Product : BaseEntity { [DisplayFormat(ConvertEmptyStringToNull = false)] [Required(AllowEmptyStrings = false, ErrorMessage = "ProductName is required.")] public string ProductName { get; 放; }
[Required(AllowEmptyStrings = false, ErrorMessage = "ProductDescription is required.")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string ProductDescription { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than 0")]
public int Quantity { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than 0")]
public decimal Price { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "BrandName is required.")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string BrandName { get; set; }
public string Promotion { get; set; }
public ProductBrand ProductBrand { get; set; }
public int ProductBrandId { get; set; }
public void CalculatePriceAfterDiscount()
{
switch (Promotion)
{
case "5PERCENTOFF":
Price = Price - Price * 0.05m;
break;
case "10PERCENTOFF":
Price = Price - Price * 0.1m;
break;
case "20PERCENTOFF":
Price = Price - Price * 0.2m;
break;
default:
if (!string.IsNullOrEmpty(Promotion))
throw new ArgumentException("Invalid promotion specified");
break;
}
}
}
}
}`
还可以在课堂上使用计算价格方法还是应该在服务中使用?