I have a code base I want to use for both an ASP.NET MVC and a WPF/MVVM front end. The business objects are implement as interfaces and the business logic uses these interfaces for passing data.
Say I have a property on my interface that implements IEnumerable. Is it possible to have different versions of this interface use different implementations of IEnumerable? An example of what I am trying to accomplish:
class Reporting
{
public bool RunReport(IReportParams Params);
}
interface IReportParams
{
IEnumerable<Guid> SelectedItems { get; }
IEnumerable<StatusEnum> SelectedStatuses { get; }
}
class MvcReportParams : IReportParams
{
public List<Guid> SelectedItems { get; set; }
public List<StatusEnum> SelectedStatuses { get; set; }
}
class WpfReportParams : IReportParams
{
public ObservableCollection<Guid> SelectedItems { get; set; }
public ObservableCollection<StatusEnum> SelectedStatuses { get; set; }
}
Can this be done?
Edit: I should also have asked "how", because when I try this, I get errors similar to this:
'MvcReportParams' does not implement interface member 'IReportParams.SelectedStatuses'. 'MvcReportParams.SelectedStatuses' cannot implement 'IReportParams.SelectedStatuses' because it does not have the matching return type of 'System.Collections.Generic.IEnumerable'
ObservableCollection and List both absolutely implement IEnumerable, and so this does not seem to make sense to me.
Last Edit
Well, someone posted the answer, but they deleted it for some reason, so I can't mark it as the solution. Here's what I ended up doing:
interface IReportParams
{
IEnumerable<Guid> SelectedItems { get; }
IEnumerable<StatusEnum> SelectedStatuses { get; }
}
class MvcReportParams : IReportParams
{
// Properties that the modelbinder dims and sets up
public List<Guid> SelectedItems { get; set; }
public List<StatusEnum> SelectedStatuses { get; set; }
// Explicityly implement my interface
IEnumerable<Guid> IReportParams.SelectedItems
{
get { return SelectedItems; }
}
IEnumerable<StatusEnum> IReportParams.SelectedStatuses
{
get { return SelectedStatuses; }
}
}
class WpfReportParams : IReportParams
{
// Properties that my view dims and modifies
public ObservableCollection<Guid> SelectedItems { get; set; }
public ObservableCollection<StatusEnum> SelectedStatuses { get; set; }
// Explicityly implement my interface
IEnumerable<Guid> IReportParams.SelectedItems
{
get { return SelectedItems; }
}
IEnumerable<StatusEnum> IReportParams.SelectedStatuses
{
get { return SelectedStatuses; }
}
}
It's more lines of boilerplate code to write, but I like being more explicit anyways.