function call & architecture recommendations

  • Thread starter Thread starter TS
  • Start date Start date
T

TS

From my presentation layer, I call a validation method in my business layer
that i pass a custom class to that holds all parameters. I am currently also
passing an error message by reference so that the calling code can have this
string set to an error message inside this method.

I'm wondering 2 things:
1. Should i put the errorMessage param as a property on args and pass the
whole thing as reference?

2. Is there a more elegant approach to handling validation that occurs on
this separate business layer?

thanks a lot!
public bool CanReg(RegValidationArgs args, ref string errorMessage)
{
// Call method to validate isn't already registered if this is a new
record
if( this.IsNew )
{ // make sure isn't already registered
if( IsAlreadyRegistered(this.ClassId, this.PartId) == true )
{
errorMessage = AlreadyRegisteredMsg;
return false;
}
}

Funding faFunding= Funding.GetFunding(args.AgentId, args.SourceId ,
args.Date);
// If one isn't found, then the Funding can't be used
if( faFunding == null )
{
errorMessage = CannotUseFundingSourceMsg;
return false;
}
}

}
 
Create one class called Registration. Use it for storing the values,
validating their contents, and committing the transaction. That one class,
and its underlying database connections, is you business layer.

X = new Registration()
X.FundingSource = ???
X.LastName = ??
X.FirstName = ??
try
X.Commit();
catch ex ValidationException
//return ex.Message to the user
end try

***************

The point here is that your presentation and business layers don't have to
be physically separated. As long as you can look at the code and say, this
class holds my business rules and that form interacts with the user, you've
done your job.
 
this is how i have it. The method on my business layer i was referring to is
in my registration class. My main question is how to appropriately pass back
the error messages from the registration class to the UI for display. How i
have it is that i pass all needed params to the method in a class that hold
all of them, then i also send a parameter by reference so that the
registration class can set its value so the calling code in the UI now has
the error message.

I just don't like having all the parameters housed in the RegValidationArgs
class and then have to add another parameter by reference. I don't know if
i should include the extra parameter inside the RegValidationArgs class as
well, or to do a different approach alltogether.

thanks for your input
 
I would suggest merging the RegValidationArgs into the class that has CanReg
method. As fort passing the human-readable error message by ref, that sounds
good to me.
 
Thanks fro Jonathan's quick response!

Hi TS,

I think it's fine to pass a string back to the caller by reference.
However, there are also some other ways. For example, you can thrown an
exception if the validation fails and include the error message in the
exception message. In the call code, you just catch this exception and
display it. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top