Architectural question regarding Interfaces implementation and extensibility
I've created a sample app, just to test and try out some of wpf's capabilities. I was basically trying out the databinding in wpf, and did the rest of stuff more or less quickly.
Basically, i have an Interface with a method that performs some background work, and then returns a list of objects the interfaced looks like this :
public interface IWorker<T>
{
IEnumerable<T> DoWork();
}
I created an implementation of that interface, and using Unity and a little bit of initialization, i inject it into my viewmodel. I'll skip for clarity the DI, i'll go directly to the viewmodel. Offer is a type of object, Offers is an ObservableCollection bound to a grid through a property of the MainViewModel.
public MainViewModel(IWorker<Offer> worker)
{
InitViewModel();
_currentDispatcher = Dispatcher.CurrentDispatcher;
Action<Offer> addOFfer = (item) => { Offers.Add(item); };
Action<IWorker<Offer>> createOffers = (createOffers ) =>
{
foreach (var item in createOffers.DoWork())
{
_currentDispatcher.BeginInvoke(DispatcherPriority.DataBind, addOFfer, item);
}
};
DoWorkCommand = new RelayCommand(() =>
{
_currentDispatcher.Invoke(DispatcherPriority.Background, createOffers, worker);
});
}
So far so good. I have a command bound to a button, that when we click on it, the button calls the worker, which created a bun开发者_Python百科ch of objects, and puts them in the observablecollection, hence the databinding is automatic. This works fine My question is.... Now i want to create a NEW implementation of the IWorker interface, and be able to use it from the same view, through a different command (like having Button1 call the SpecificWorker1, and Button2 call the SpecificWorker2 implementations, respectively). Is there a way to do this without having to change a lot the architecture? if so, what should be the changes? is it not possible, or not a good approach to have in a view more than one service implementation at the same time?
精彩评论