Reputation:
Case: I have a void method that inserts data and does a bunch of stuff but recently I was given a requirement that this exposed method in the service needs to return a value
What options do I have? Pretty sure I need to refactor this but how or which approach?
[OperationContract]
[WebInvoke]
[TransactionFlow(TransactionFlowOption.Allowed)]
void InsertPerson(string Name, string LastName, string Income, string Address)
Method looks like this:
public void InsertPerson(string Name, string LastName, string Income, string Address)
{
try
{
Name = (Name == null) ? "" : Name ;
LastName = (LastName == null) ? "" : LastName ;
Income = (Income== null) ? "" : Income;
Address = (Address== null) ? "" : Address;
//bunch of Procedures, DataSets and Functions
}
catch(Exception ex)
{
StoredProcedures.InsertLog(ex.ToString());
}
}
Solution needed: let's say that the procedures calculate the income and determine that the person is not fit for a loan, I need to let know the caller in this same method that the person is not suitable for a loan say for example with some additional parameter called HighRisk
that returns 1 or 0
Edit:
Found a solution it was quite simple I just changed the void
to a List<T>
and made a class that returned the whole structure like so
List<Person> PersonInfo = new List<Person>();
DataSet ds = StoredProcedures.PersonSelect(Name, LastName);
if (ds.Tables[0].Rows.Count > 0)
{
Person PersonData = new Person();
PersonData.SuitableForLoan = ds.Tables[0].Rows[0]["SuitableForLoan"].ToString();
PersonInfo.Add(PersonData);
}
return PersonInfo;
edit 2: I don't understand why my question is being downvoted, I hate this place so much now I remember why I didn't want to make another account
Upvotes: 0
Views: 291
Reputation: 1062895
Well, the simple answer here would be: change the API - perhaps returning an enum instead of void
, to indicate the success or failure reason. Note that this is a breaking change, however, so you can't do that unless you are happy to deploy clients and servers at the same time to match this API. If you can't break the API, you'll have to add a new method with a return value, and migrate clients to use the new method - leaving the old method for older clients to use until they are updated.
You could also perhaps hack it in with headers of some kind, but that's a terrible idea in most cases and will only make things more confused than they already are.
Upvotes: 4