I've got a problem with designing good wrchitecture for my MVVM project. I came up with something like below, please read and tell me if this is wrong approach or ok: I will have some interface:
public interface ISomeStrategy
{
void LoadData();
void Search(string text);
IList<SomeObject> SomeObjectsList { get; set; }
}
And a class that implements this interface:
public class FirstStrategy: INotifyPropertyChanged, ISomeStrategy
{
public CompositePayslipItem(IDataService dataService)
{
DataService = dataService;
}
private IDataService DataService;
public IList<SomeObject> SomeObjectList{get; set;}
public async void LoadData()
{
SomeObjectList =await DataService.GetAll();
}
public async void Search(string text)
{
SomeObjectList =await DataService.GetByKey(text);
}
}
And ViewModel:
public void SomeViewModel
{
public SomeViewModel(IDataService dataService)
{
Strategy = new FirstStrategy(dataService);
Strategy.LoadData();
}
public ISomeStrategy Strategy {get; set;}
private Command<string> searchCommand;
public Command<string> SearchCommand => searchCommand?? (searchCommand= new Command(ExecuteSearchCommandAsync));
private async void ExecuteSearchCommandAsync(string text)
{
Strategy.Search(text);
}
}
As you can see all logics will be in "Strategy" classes which will be binded via ViewModel to View. What it gives to me? I can dynamicly change implementation at runtime. For example if I have a Employee and Manager, where logic for search and getting data is different I can just implement another class and change property ISomeStrategy Strategy without editing existing code for Employee ( no additional if's in a lot of methods). Unfortunetly there is a few problems with it:
- All bindings (except Commands) will be in class *Factory which can be misleading
- In most cases one VM will have one Strategy implementation so there will be a lot of code for nothing. On the other hand, in the future client can demand another role (implemenation) and it will be easier (just implement another class - no editing old code).
What do you think about it? Or maybe you using another patterns for business logic?
EDIT: Xaml part:
<ContentPage
x:Class="Test.SomePage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<StackLayout>
<SearchBar
HeightRequest="50"
SearchCommand="{Binding SearchCommand}" />
<ListView
x:Name="formatsList"
Margin="0"
ItemsSource="{Binding Strategy.SomeObjectList}">
</StackLayout>
</ContentPage>