我有一个实体类,其中有一堆非空字符串属性。如果我尝试设置这些为空一个,我得到一个ConstraintException和消息“这个属性不能被设置为空值。”确定EF实体中ConstraintException的来源
没有跟踪变量设置为分配前的各属性的名称,有没有一种方法可以确定哪个属性分配导致异常被抛出?
我有一个实体类,其中有一堆非空字符串属性。如果我尝试设置这些为空一个,我得到一个ConstraintException和消息“这个属性不能被设置为空值。”确定EF实体中ConstraintException的来源
没有跟踪变量设置为分配前的各属性的名称,有没有一种方法可以确定哪个属性分配导致异常被抛出?
在您的POCO类中,您可以使用Checker模式进行此验证。使用构造函数注入不可空属性注入。
public class Product:Entity<long>
{
public Product(string description, string productNo, double? unitPrice, Category category)
{
Check.Require(unitPrice.HasValue, "Unit price can not be null in Product");
Check.Require(!string.IsNullOrEmpty(description),
"Description can not be null in Product");
Check.Require(!string.IsNullOrEmpty(productNo),
"Product number can not be null in Product");
Check.Require(category != null, "Category can not be null in Product");
UnitPrice = unitPrice.Value;
Description = description;
ProductNumber = productNo;
Category = category;
}
/// <summary>
/// Gets or sets the product id.
/// </summary>
/// <value>
/// The product id.
/// </value>
public int ProductId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the category id.
/// </summary>
/// <value>
/// The category id.
/// </value>
public long CategoryId { get; set; }
/// <summary>
/// Gets or sets the category.
/// </summary>
/// <value>
/// The category.
/// </value>
public Category Category { get; set; }
/// <summary>
/// Gets or sets the unit price.
/// </summary>
/// <value>
/// The unit price.
/// </value>
public double UnitPrice
{
get;
set;
}
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public String Description
{
get;
set;
}
/// <summary>
/// Gets or sets the product number.
/// </summary>
/// <value>
/// The product number.
/// </value>
public string ProductNumber
{
get;
set;
}
}
}
public class Check
{
/// <summary>
/// Requires the specified assertion.
/// </summary>
/// <param name="assertion">if set to <c>true</c> [assertion].</param>
/// <param name="message">The message.</param>
public static void Require(bool assertion, string message)
{
if(!assertion)
{
throw new PreConditionException(message);
}
}
/// <summary>
/// Ensures the specified assertion.
/// </summary>
/// <param name="assertion">if set to <c>true</c> [assertion].</param>
/// <param name="message">The message.</param>
public static void Ensure(bool assertion, string message)
{
if (!assertion)
{
throw new PostConditionException(message);
}
}
}
/// <summary>
/// Exception raised when a precondition fails.
/// </summary>
public class PreConditionException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="PreConditionException"/> class.
/// </summary>
/// <param name="message">The message.</param>
public PreConditionException(string message):base(message)
{
}
}
/// <summary>
/// Exception raised when a postcondition fails.
/// </summary>
public class PostConditionException:Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="PostConditionException"/> class.
/// </summary>
/// <param name="message">The message.</param>
public PostConditionException(string message):base(message)
{
}
}
您可以访问堆栈跟踪?它会像'MyEntity.set_MyNotNullableProperty42'这样的东西告诉你哪个属性被设置为null。
我可以访问堆栈跟踪,这可以在调试时解决大部分问题,但是在生产中我希望能够返回模型上的属性错误并通知用户缺少哪些属性。逐帧解析堆栈跟踪或字符串并不是提取所需信息的非常可靠的方法。 – ProfK 2011-07-29 16:00:12
+1为一个坚实的概念答案,也许不是我所期待的。我希望能够在工作应用程序中不访问太多“外部”代码的情况下确定错误来源。 – ProfK 2011-07-29 08:56:23