first time on stackoverflow + ninject (IoC's)

I have a situation in which I have Business Objects Implemented in a way that they have Models in them... i.e.

Public Class Whatever
Implements IWhatEver
      Public Property Id as Integer
      Public Property Name as String

      Public Function SetWhatEver() as Whatever
           'Do Whatever Settings
      End Function
End Class

I am using ninject for DI (Dependency Injection) however the issue was that I couldn't use an Interface as a model being passed in an action, therefore I am trying to make a custom modelbinder and want to use the bindingContext.ModelType to pass to ninject and ninject to resolve it for me and so I can do my binding with metadata

Public Overrides Function BindModel(controllerContext As ControllerContext, bindingContext As ModelBindingContext) As Object

        Dim modelType = ninjectPleaseResolve(bindingContext.ModelType)
        Dim metaData = ModelMetadataProviders.Current.GetMetadataForType(Nothing, modelType)

        bindingContext.ModelMetadata = metadata

    Return MyBase.BindModel(controllerContext, bindingContext)
End Function

I hope this makes sense... I have tried looking for answers BTW and nothing online is making sense to me, so please can you explain in simple terms..

EDIT

I am adding the controller code below to give you a better understanding of what I am trying to do.. I don't want to use the Whatever class instead I want to use the IWhatever in the controller for my processing... please see below an example...

Public Class MainController
Inherits System.Web.Mvc.Controller

Dim repository As IWhatever

Public Sub New(pWhatever As IWhatever)
    repository = pWhatever
End Sub


Function Index(myValues As IWhatever) As ActionResult

      'So I can process these values to my liking...
      repository.SetWhatEver(myValues)

      ' and then perhaps other functions like...
      repository.Save()

      Return View()
End Function

I hope this makes a little sense now..

有帮助吗?

解决方案

the issue was that I couldn't use an Interface as a model being passed in an action

You shouldn't pass in services through the action method. You should pass services in through the constructor. This way your container can build up the controller and all related objects for you and this way you don't have to write a custom model binder.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top