views:

220

answers:

1

I have a date time property in a linq to sql model and I want to override this in a partial class.

I tried changing the inheritance modifier and then overiding in the partial class, but it didn't work.

Is this sort of thing possible? I'm developing in 3.5 framework and its and ASP.NET MVC application.

My override is:

public override DateTime CommissionStart { get { return this.CommissionStart; } set { if (CommissionEnd > CommissionStart) { throw new ApplicationException("Date exception"); } else { this.CommissionStart = value; } } }
+1  A: 

I think the real question is, "What's the right pattern for doing this type of validation?"

Instead of trying to override, implement the partial function OnCommissionStartChanging. From there, you can perform your validation and throw your exception. Oh, and don't use ApplicationException.

Dave Markle
That looks like a much nicer solution. I now want to replace my application exception code with code that will add an error to the model state. And this will then feed back to my controller, and then view. do you know how to do this?
littlechris
+1 Great information on ApplicationException.
JoshJordan
@littlechris: You could put a error code in your model object and then implement the OnValidate() partial method to do what you want, I think.
Dave Markle
Dave, do you have an example of this?
littlechris