I have the below code example where I want to prepend some text on to the front of the source param which could be of type ISite
or IFactory
used in a MVC drop down list via a MoreLinq extension. This function is used in a controller action that returns the list serialised as JSON so that it can be loaded dynamically using some dropdown cascading, otherwise I would have done this separately in a view model.
I was wondering if there is a way to be able to do something similar to the below without having to create a concrete ListItem class, my example shows how I am using as IListItem
but I am aware this won't compile.
At the moment my controller has no concept of any of my model's concrete classes and I kind of wanted to keep it that way and I'm not sure if I definately need to make an instance of a ListItem to make this work or if anyone has another suggestion?
My knowledge of Covariance and Contravariance in Generics is limited, in case that is of importance here? Thanks
public interface IListItem
{
int Id { get; set; }
string Name { get; set; }
}
public interface ISite : IListItem
{
int CountryId { get; set; }
}
public interface IFactory : IListItem
{
int SiteId { get; set; }
}
public interface IResource
{
int Id { get; set; }
string Name { get; set; }
int ContentID { get; set; }
string Text { get; set; }
int LanguageID { get; set; }
string LanguageCode { get; set; }
int Priority { get; set; }
}
private IEnumerable<IListItem> PrependSelectionResource(IEnumerable<IListItem> source, string languageCode)
{
if(source == null || source.Count() == 1)
return source; // don't bother prepending the relevant resource in these cases
try
{
// will throw an exception if languageCode is null or blank
var resource = _resourceRepository.GetByNameAndLanguageCode(
"Prompt_PleaseSelect",
languageCode);
if(resource == null)
return source;
// prepend the "Please Select" resource to the beginning of the source
// using MoreLinq extension
return source.Prepend(new {
Id = 0,
Name = resource.Text ?? ""
} as IListItem);
}
catch {
return source;
}
}