0

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..

inN0Cent
  • 363
  • 2
  • 18

1 Answers1

0

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.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • I have already done that, the issue is that the same service contains my model.. i.e. properties.. we can say its a DomainModel... I am wanting to use the same to get / map values from the views coming to the controllers.. I hope it makes sense.. otherwise the contructor method is working fine and ninject is injecting perfectly and I am using the service... I need a solution to pass values to the action without having to rewrite another model.. – inN0Cent Aug 15 '13 at 11:43
  • @inN0Cent: Unfortunately you don't make sense to me. Could you update your question and add more information about your problem? – Steven Aug 15 '13 at 11:51
  • The problem here is that you are mixing data and behavior. Don't do that. Dependencies that are injected through the constructor are meant for services (classes with behavior and no data). Data structures (runtime values) are passed to methods (such as action methods and methods on services). These structures should contain no business logic and only simple logic and data. Seperate these responsibilities in their own classes and your problem goes away. – Steven Aug 15 '13 at 12:18
  • so you mean I can have a separate ViewModel that implements the iWhatever and then I can pass it to the service? I mean that would be a better idea, and it would be separate as well? – inN0Cent Aug 15 '13 at 12:25
  • You should have 1. A view model that contains the data for the MVC View to display. 2. A domain entity that contains the data to be saved (could be placed as property on the view model). 3. a repository that contains the logic for saving the domain entity. – Steven Aug 15 '13 at 12:31