开发者

Where to put validation for changing an object's children?

I am using ASP.NET MVC 2 with nhibernate. I have this Sales class. Sales has many Payments. Sales' payments should not be modified once the sales' status becomes confirmed. I need suggestions on how to enforce this.

I'm stumped on several things when trying to put this validations:

  • For adding and deleting payments:
    • I can create AddPayment and DeletePayment methods in Sales, but everybody must remember to use these instead of adding and deleting the payments collection directly
    • I don't want to hide the payments collection because nhibernate needs it, also this collection is used in other parts of the software
  • For modifying existing payments:
    • I don't think I should put the validation in the Payments setters because nhibernate needs to access the setters.
  • Should I throw exception? There's a discussion about the disadvantages of throwing exception to prevent object entering invalid state. In my case object state after modification may still be valid, but I want to blo开发者_如何学编程ck the modification. Is throwing exception reasonable in this case? What are the alternatives?

Should I enforce this in the controller actions?


You can hide the collection by making it protected or even private. NHibernate will still find it. Alternatively you could have the collection getter return an immutable collection when the sales status is the restricted value.

private ISet<Payment> _payments;

public virtual ISet<Payment> Payments
{
    get
    {
        if (Status == SalesStatus.Confirmed)
            return new ImmutableSet<Payment>(_payments);

        return _payments;
    }
    private set { _payments = value; }
}

Putting validation rules in the setters is also fine. You can tell NHibernate to access the backing field directly (you may have to add a backing field if you're currently using auto properties).

<property name="_name" access="field"/>
<property name="Description" access="nosetter.camelcase-underscore"/>

Edit for the additional question...

I don't tend to throw exceptions until right before a save. Throwing them earlier, like in a property setter, can be annoying to UI developers who have to go back to the user with just one error at a time. I do mostly MVC applications too, so I've been using the System.ComponentModel.DataAnnotations validation attributes of late. While limited in some respects, they work well with MVC for the model checks at the browser and in the controller. If you use those, however, I recommend creating a custom interceptor to check them all right before a save. The interceptor is where I will throw an exception if anything is awry.


There are several options:

  • One is to have a trigger in the database, then you would be 100% sure
  • Your suggestion of AddPayment and DeletePayment, combined with some code review is probably the best way to go.


I would introduce two new projects (layers) in your solution:

  • a model layer
  • a service (or business) layer

Your model layer should consist of interfaces and supporting types:

public interface ISales
{
    IEnumerable<IPayment> GetPayments();
}

public enum PaymentStatus
{
    Unknown,
    Confirmed
}

public interface IPayment
{
    // your public properties
    PaymentStatus Status { get; set; }
}

You should move your NHibernate classes into the service layer and hide them with internal. Your NHibernate classes implement the model interfaces:

internal class Sales : ISales
{
    public IEnumerable<IPayment> GetPayments()
    {
        // your implementation
    }
}

internal class Payment : IPayment
{
    // your public properties
    public PaymentStatus Status { get; set; }
}

public class SalesService
{
    public ISales FindByKey(int key)
    {
        // your implementation
    }

    public void AddPayment(ISales sales, IPayment payment)
    {
        // throw exception if validation fails
    }

    public void DeletePayment(ISales sales, IPayment payment)
    {
        // throw exception if validation fails
    }
}

public class PaymentService
{
    public IPayment FindByKey(int key)
    {
        // your implementation
    }
}

Since the classes Sales and Payment are hidden, your controllers are foced to use the service classes:

public class SalesController : Controller
{
    private readonly SalesService salesService;
    private readonly PaymentService paymentService;

    public SalesController()
    {
        salesService = new SalesService();
        paymentService = new PaymentService();
    }

    public ActionResult AddPayment(int salesId, int paymentId)
    {
        var sales = salesService.FindByKey(salesId);
        var payment = paymentService.FindByKey(paymentId);

        salesService.AddPayment(sales, payment);

        return RedirectToAction("Index");
    }
}

You should also consider using an IoC container like autofac or Ninject.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜