I'm writing my first C# Windows Store app and am learning C# from scratch. I'm trying to implement MVVM as I understand it and object orientated patterns, obviously though I'm expecting to be doing it wrong so looking for pointers as to what I should improve.
Below is a method that gets called when a user either clicks a 'Search' XAML control, or hits return in the search textbox control.
The comments should explain the code. I feel like I probably have too much code in for a event method so please advise how I would restructure it.
private void SubmitSearch(object sender, RoutedEventArgs e)
{
//New Validation object
ViewModels.Validation validation = new ViewModels.Validation();
// Capture user entered term and check is valid
// submit appropriate message.
var searchValue = searchTerm.Text.ToString();
dynamic validateInput = validation.inputNullCheck(searchValue);
int? errorCode = validateInput.ErrorCode;
if (errorCode == 0)
{
SubmitAction(searchValue);
}
else
{
DisplayTerms(validateInput);
}
}
My SubmitAction method:
public async Task<object> SubmitAction(string searchValue)
{
// Query webservice/database through Model and return response
dynamic response = await new ViewModels.Search().QueryRequest(searchValue);
DisplayTerms(response);
//This is here only because I needed the method to be async, which in turn requires a
// value to be returned. Ideally it would be async and end after DisplayTerms(response);
return response;
}
DisplayTerms method:
private void DisplayTerms(object value)
{
ListView termsList = termsListContainer;
dynamic searchResponse = value;
int count = searchResponse.Count;
// This will eventually be a loop through the returned object
// it is hardcoded at the moment because I am debugging something.
termsList.Items.Add(searchResponse[0].TermName);
}
Validation class:
class Validation
{
// Temporarily 0 = success, 1 = error
public int? ErrorCode { get; set; }
public string ErrorName { get; set; }
public string ErrorMessage { get; set; }
//Constructor
public Validation ()
{
}
// Check if user input was empty
public object inputNullCheck(string input)
{
if (input != "")
{
this.ErrorCode = 0;
this.ErrorName = "Success";
this.ErrorMessage = "Valid input received";
return this;
}
else
{
this.ErrorCode = 1;
this.ErrorName = "Input Empty";
this.ErrorMessage = "You have not input anything.";
return this;
}
}
}
dynamic
instead of a concrete type? \$\endgroup\$ – Jeroen Vannevel Dec 11 '14 at 18:12