I have a method and its parameter list has grown too long. So I figured I should configure an object and pass that to the method instead. In doing this however, I lose the ability to demand particular values are supplied to the method. i.e. the callee
might forget to set the property on the parameter object.
How can I force properties on an object to be set before sending the object to method as a parameter?
this._exceptionList =
new System.Collections.ArrayList();
if (this.FirstName ==
null) this._exceptionList.Add("First name is missing");
if (this.LastName ==
null) this._exceptionList.Add("Last name is missing");
return this._exceptionList;
}
}
public bool IsValid
{
get { return
this.ExceptionList.Count == 0; }
}
#endregion
}
That way you can just call the bool property 'IsValid' on any object.
I copied and pasted and removed some unimportant stuff, so forgive me if some of the fields and properties do not match.
If an object requires certain properties to be set before it is valid, values for those properties should be passed into the constructor for that object.
+1 on using a constructor to enforce fields to be set. Also, consider checking for the fields in your method and throwing an ArgumentNullException if missing.
If an object requires certain properties to be set before it is valid, values for those properties should be passed into the constructor for that object.
WhatThe12
Contributor
3416 Points
984 Posts
Method Parameters
Aug 31, 2007 11:42 AM|LINK
Hi,
I have a method and its parameter list has grown too long. So I figured I should configure an object and pass that to the method instead. In doing this however, I lose the ability to demand particular values are supplied to the method. i.e. the callee might forget to set the property on the parameter object.
How can I force properties on an object to be set before sending the object to method as a parameter?
Cheers, WT.
agent_smith
Contributor
2030 Points
573 Posts
Re: Method Parameters
Aug 31, 2007 12:49 PM|LINK
You could encapulate the business logic for required properties within the custom class you described.
Create an interface called 'IObjectValidator'
public interface IObjectValidator{
System.Collections.
IList ExceptionList{get;} bool IsValid{get;}}
Then your business object:
[
XmlRoot("Person")] [Serializable] public class Person : IObjectValidator{
#region fields private System.Collections.ArrayList _exceptionList; private Guid _id; private string _employeeId; private string _prefix; private string _firstName; private string _middleName; private string _lastName; private string _suffix; private DateTime _dateOfBirth; #region methods public Person(){
}
#endregion public Guid Id{
get{return this._id;}set{this._id = value;}}
public string EmployeeId{
get{
return this._employeeId;}
set{
this._employeeId = value;}
}
public string FirstName{
get{
return this._firstName;}
set{
this._firstName = value;}
}
public string MiddleName{
get{
return this._middleName;}
set{
this._middleName = value;}
}
public string LastName{
get{
return this._lastName;}
set{
this._lastName = value;}
}
public string FullName{
get{
if (this._lastName != null && this._firstName != null){
return this._firstName + " " + this._lastName;}
return string.Empty;}
}
public string Prefix{
get{
return this._prefix;}
set{
this._prefix = value;}
}
public string Suffix{
get{
return this._suffix;}
set{
this._suffix = value;}
}
public DateTime DateOfBirth{
get{
return this._dateOfBirth;}
set{
this._dateOfBirth = value;}
}
public string LastNameFirstName{
get{
string returnvalue = string.Empty;if(this.LastName != null || this.FirstName != null){
returnvalue = this.LastName + ", " + this.FirstName;}
return returnvalue;}
}
#endregion
#region IObjectValidator Memberspublic System.Collections.IList ExceptionList{
get{
this._exceptionList = new System.Collections.ArrayList(); if (this.FirstName == null) this._exceptionList.Add("First name is missing"); if (this.LastName == null) this._exceptionList.Add("Last name is missing"); return this._exceptionList;}
}
public bool IsValid{
get { return this.ExceptionList.Count == 0; }}
#endregion
}
That way you can just call the bool property 'IsValid' on any object.
I copied and pasted and removed some unimportant stuff, so forgive me if some of the fields and properties do not match.
AGENT_SMITH
WhatThe12
Contributor
3416 Points
984 Posts
Re: Method Parameters
Aug 31, 2007 01:02 PM|LINK
Very nice. Thankyou.
(They ought to get the tabbing on code snippets sorted out on here.)
TGnat
Participant
1376 Points
279 Posts
Re: Method Parameters
Aug 31, 2007 01:25 PM|LINK
If an object requires certain properties to be set before it is valid, values for those properties should be passed into the constructor for that object.
ALFKI
Member
448 Points
98 Posts
Re: Method Parameters
Aug 31, 2007 05:27 PM|LINK
+1 on using a constructor to enforce fields to be set. Also, consider checking for the fields in your method and throwing an ArgumentNullException if missing.
WhatThe12
Contributor
3416 Points
984 Posts
Re: Method Parameters
Aug 31, 2007 10:42 PM|LINK
Which would get me back where I started.....
</div></div>mkamoski
Contributor
5694 Points
1565 Posts
Re: Method Parameters
Sep 03, 2007 01:02 AM|LINK
Make sure every contructor has parameters for all values necessary to instantiate a valid object.
That way, the object cannot be instantiated in a bad state.
One could also use some default values, if possible.
Use lazy load and/or other validation in the getters/setters to maintain valid state.
Etc.
HTH.
WhatThe12
Contributor
3416 Points
984 Posts
Re: Method Parameters
Sep 03, 2007 08:10 AM|LINK
But that would result in a long parameter list for the constructor method which is what I'm trying to avoid.
JontyMC
Member
394 Points
144 Posts
Re: Method Parameters
Sep 03, 2007 09:05 AM|LINK
Not necessarily. Just create a "MethodParams" object (there's probably a better name) and create default values in its constructor.
public class MethodParams { private string arg1; private bool arg2; public MethodParams() { // Set default values arg1 = "default value"; arg2 = true; } ..... }WhatThe12
Contributor
3416 Points
984 Posts
Re: Method Parameters
Sep 03, 2007 09:19 AM|LINK
What if a default value is no better than no value?