Hi All,
It's been a while since I've posted here but I'm having a bit of a mare and can't for the life of me remember what I'm doing wrong. I'm pretty sure it's fairly simple so hopefully you can help.
I have the following (approximated example):
public interface IFoo<T>
{
List<T> Bars {get;set;}
}
public interface IBar
{
}
public class Foo : IFoo<Bar>
{
List<Bar> Bars {get;set;}
}
public class Bar : IBar
{
}
Then I have a helper class which looks a bit like this:
public interface IHelper
{
bool DoSomething(IFoo<IBar> model, string path);
}
public class Helper : IHelper
{
public bool DoSomething(IFoo<IBar> model, string path)
{
return true;
}
}
So, the issue I have is when I try to pass an instance of Foo to my helper method I get the error:
Argumet type 'Foo' is no assignable to parameter type 'IFoo<IBar>'
I'm not sure why I get this error because Foo inherits from IFoo and Bar inherits from IBar.
-------------
Additional Info
-------------
This is how the Foo class is initially getting populated. It's a form submission from a webpage. If I try to declare Foo to be
Foo: IFoo<IBar>
I get the error:
Cannot create an instance of an interface.
upon form submission. This is an MVC generated error. As a result Foo is defined as above using
IFoo<Bar>
[HttpPost]
public ActionResult RequestSubmission(Foo model)
{
if (ModelState.IsValid)
{
helper.DoSomething(model, Server.MapPath("/Assets/Template.docx"));
return Json(new {IsSuccess=true});
}
return PartialView("Form", model);
}
-------------
Bit of scope
-------------
This project is a basic MVC5 project with no authentication.
I have a controller that, using a poor mans DI controller, is having the helper class injected into it.
My MVC models inherit from the interfaces to allow them to be passed through.
So effectively Foo and Bar are data models.
This was all working before I tried to re-write it to use Interfaces, as tightly coupled I had no problems at all.
So, my question in summary:
Why do I get this error, and how can I resolve it?
If you need more information or clarification on anything just yell :-)
Hope you can help.