Link to home
Start Free TrialLog in
Avatar of Marc Davis
Marc DavisFlag for United States of America

asked on

Validation of fields

I have a question regarding validation of fields provided from a WCF web service.

Let's say that the web service has 6 fields:

MemberFirstName
MemberLastName
MemberNumber
AptType
City
Referral


Now, I want to check that when a Referral is provided the MemberFirtName *and* MemberLastName are also provided. When a City is provided the AptType *or* a MemberLastName is provided.

What is the best way to achieve something like that?


This is in C# with VS 2015.

Any information would be greatly appreciated.
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

when a Referral is provided
Do you mean FROM the WCF service?
Avatar of Marc Davis

ASKER

I know I can make some things required on the web service on the data contract but for the most part no, it's not from the web service. It would be after the information is already provided from the web service.

I hope that helps.
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I understand you correctly, then you have an architectural problem. Cause you have mixed two methods with different data objects into one method with one data object.
Eddie Shipman, can you clarify a bit and do you have an example? I know I could use a brute force with an IF to check for if the firstname is provided the lastname field must also be provided an visa-versa but it's there a better/different way?


ste5an, I'm not sure I'm following you on that. How is/would it be an architectural problem? It's validation. Be it in MVC, webforms, GUI, etc. It's data being provided with dependencies to be met. It does not justify creating a full-blown rules engine for that small amount of fields. I know I could do it with brute force but is there a better way?
I would like to see the data, if possible.
Your description sounds like one method with 6 parameters, you need - after rereading - 3 methods:

- Method1 Referral, MemberFirtName, MemberLastName
- Method2 Referral, City, AptType
- Method3 Referral, City, MemberLastName

I think you're using a god-object (anti-pattern). And the problems arrive as "validation" problem.
This violates the single responsibility principle (SRP, SOLID)  and the separation of concerns principle. The normal approach would be using the command pattern. Thus 3 methods in your WCF service.

But as I already said, without further context or even knowing your actual code, this is a just a guess.
Where is a better example. What I am trying to do is bring this down to one IF statement.

if (!(string.IsNullOrEmpty(req.ReferFrom)))
                {
                    if ((!(string.IsNullOrEmpty(req.MemberFirstName)) && (string.IsNullOrEmpty(req.MemberLastName)) || (string.IsNullOrEmpty(req.MemberFirstName)) && !(string.IsNullOrEmpty(req.MemberLastName))))
                    {
                        {
                            res.RespStatus.RespCode = 1;
                            res.RespStatus.RespMessage = "If the ReferFrom is provided and either the firstname, lastname or DOB is provided they must all be provided.";
                            return res;
                        }
                    }
                    if ((req.DOB.HasValue) == false)
                    {
                        res.RespStatus.RespCode = 1;
                        res.RespStatus.RespMessage = "If the ReferFrom is provided and either the firstname, lastname or DOB is provided they must all be provided.";
                        return res;
                    }
                }

Open in new window


Make sense?

I do have a couple other scenario's but for the most part its similar.
Thanks, I ended up resorting to the IF's.

Thanks,