MVC 3 passes an object as an interface

I am currently working on an MVC 3 project, using Ninject as my DI, business objects are stored in a separate assembly. I encountered a problem with the controller parameters, when I send it back to CRUD operations, I get the error message "Unable to instantiate interface". I know that you cannot create an instance of the interface, but it seems to me that the only way around this is to use a custom mediator and pass in the FormCollection. This seems really messy, and I want to save as much type of specific code from the project as possible, so it's everywhere and Ninject for DI concrete. Not only does custom model binding seem messy - will I not lose my DataAnnotations?

Some code to describe what I have:

public ActionResult Create()
{
    // I'm thinking of using a factory pattern for this part
    var objectToCreate = new ConcereteType();
    return (objectToEdit);
}

[HttpPost]
public ActionResult Create(IRecord record)
{
    // check model and pass to repository
    if (ModelState.IsValue)
    {
        _repository.Create(record);
        return View();
    }

    return View(record);
}

Has anyone come across this before? How did you overcome it?

Thank!

+5
source share
3 answers

The data passed into the action of the controllers are simply holders for the values. They should not have any logic, so there is nothing to separate from it. You can use specific types (e.g. record) instead of an interface (IRecord)

+3
source

but it seems that the only way I can get around this is to use a custom connectivity device

. , , , .

- DataAnnotations?

, , . . , DataAnnotations. , .

+6

. Ninject , Index Controller.

:

public class HomeController : Controller
{
    private IRecord _record;

    public HomeController(IRecord record)
    {
        _record = record;
    }

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application. " +
                          _record .HelloWorld();

        return View();
    }
}

?

+2

All Articles