"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!
If you want to validate when the property is set, you need to use non-auto properties (i.e. , manually defined get and set methods).
If you want to validate when the property is set, you need to use non-auto properties (i.e. , manually defined get and set methods). But another way to validate is to have the validation logic separate from the domain object.
Class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Organization { get; set; } } interface IValidator { bool Validate(T t); } class CustomerValidator : IValidator { public bool Validate(Customer t) { // validation logic } } Then, you can say Customer customer = // populate customer var validator = new CustomerValidator(); if(!validator. Validate(customer)) { // head splode } This is the approach I prefer: A Customer should not responsible for validating its own data, that is another responsibility and therefore should live elsewhere. Different situations call for different validation logic for the same domain object.
I don't agree, even if your validation fails, your customer data is invalid. What's to stop it from writing garbage to the database if you mess up external validation? – Blindy May 25 at 15:58 @Blindy: Your persistence layer should be wired up with validators that don't persist if the validation fails.
– Jason May 25 at 15:59 @Blindy Customer shouldn't be responsible for writing to the database. That would be another responsibility that Customer shouldn't have. – Phil May 25 at 16:02 I understand the "Single Responsibility Principle" (SRP) but I always wonder where the 'cut-off' should be.
Meaning, you could take it too far...like creating a class to set properties (for you). I'm not saying anything about your example...I just seem to find myself asking 'How far should I go in applying SRP'? ...maybe I should ask this as a question?
– Prisoner ZERO May 26 at 12:34 @Prisoner ZERO: I think that's the good basis for another question, but you'll need to come up with a specific example to make it better defined. – StriplingWarrior May 26 at 14:55.
What you have now are called "auto-properties," and only perform a simple "get/set". In order to customize the behavior of the get or set, you will need to convert the properties to field-backed properties: private string _firstName; public string FirstName { get {return _firstName;} set { Validate(_firstName); _firstName = value; } } Note that I changed String to string and capitalized the property name, in following accepted C# naming best practices.
I wouldn't add validation in the setter at all. Rather, I would create a function called validate instead...that way all your validation code is in one spot rather scattered throughout your setters.
You have to use the full property syntax for this.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.