Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. Newbie (but sort of advanced) Dependency Injection questions...

Newbie (but sort of advanced) Dependency Injection questions...

Scheduled Pinned Locked Moved WPF
questionvisual-studiowpfgame-devregex
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SledgeHammer01
    wrote on last edited by
    #1

    I use an MVVM / custom control library that I wrote myself. Right now it works like this at a high level: * all view models are derived from ViewModelBase * I have an attached Type property called ViewModel that gets specified something like: . . My library creates an instance of local:Test and sets the Window.DataContext equal to that instance. * At this point, all ViewModels must have a default parameterless constructor * ViewModelBase also has a static ServiceLocator I have not really used Dependency Injection before, but it is my understanding that my current code looks like this: public class Test : ViewModelBase { public Test() { ServiceLocator.GetService(); ServiceLocator.GetService(); } } vs. Dependency Injection which looks something like: public class Test : ViewModelBase { public Test(IFoo1 foo1, IFoo2 foo2) { } } at some point early in the start up, you are supposed to register the interface <-> implementation class mappings? So if I want to add DI support to my MVVM lib, it is my understanding that the ViewLocator needs to go through the params in the constructor and match up the interfaces with the classes that you registered. I am **NOT** interested in using a DI lib that somebody else wrote like Unity, etc. I am interested in knowing how they work and implementing my own. Seems like the requirement is that you have a single public constructor? Some libs I have seen add a custom attribute like [DependencyInjection] or [Ninject] to the one constructor that you want the DI to happen on? What is the best approach? Are these libs using reflection to go through the constructor params? Or some other cool mechanism for which I am unaware? I assume there is some kind of caching going on? Like, it only needs to go through the reflection once and then caches the interfaces and the order of which they are passed in? Last question for now... seems like ServiceLocator, you pass in the type and a class instance to register, but in DI, you pass in an interface type and a class instance type?

    A J 2 Replies Last reply
    0
    • S SledgeHammer01

      I use an MVVM / custom control library that I wrote myself. Right now it works like this at a high level: * all view models are derived from ViewModelBase * I have an attached Type property called ViewModel that gets specified something like: . . My library creates an instance of local:Test and sets the Window.DataContext equal to that instance. * At this point, all ViewModels must have a default parameterless constructor * ViewModelBase also has a static ServiceLocator I have not really used Dependency Injection before, but it is my understanding that my current code looks like this: public class Test : ViewModelBase { public Test() { ServiceLocator.GetService(); ServiceLocator.GetService(); } } vs. Dependency Injection which looks something like: public class Test : ViewModelBase { public Test(IFoo1 foo1, IFoo2 foo2) { } } at some point early in the start up, you are supposed to register the interface <-> implementation class mappings? So if I want to add DI support to my MVVM lib, it is my understanding that the ViewLocator needs to go through the params in the constructor and match up the interfaces with the classes that you registered. I am **NOT** interested in using a DI lib that somebody else wrote like Unity, etc. I am interested in knowing how they work and implementing my own. Seems like the requirement is that you have a single public constructor? Some libs I have seen add a custom attribute like [DependencyInjection] or [Ninject] to the one constructor that you want the DI to happen on? What is the best approach? Are these libs using reflection to go through the constructor params? Or some other cool mechanism for which I am unaware? I assume there is some kind of caching going on? Like, it only needs to go through the reflection once and then caches the interfaces and the order of which they are passed in? Last question for now... seems like ServiceLocator, you pass in the type and a class instance to register, but in DI, you pass in an interface type and a class instance type?

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      I thought Unity was open source - why dont you have a look at the source code itself?

      The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

      S 1 Reply Last reply
      0
      • S SledgeHammer01

        I use an MVVM / custom control library that I wrote myself. Right now it works like this at a high level: * all view models are derived from ViewModelBase * I have an attached Type property called ViewModel that gets specified something like: . . My library creates an instance of local:Test and sets the Window.DataContext equal to that instance. * At this point, all ViewModels must have a default parameterless constructor * ViewModelBase also has a static ServiceLocator I have not really used Dependency Injection before, but it is my understanding that my current code looks like this: public class Test : ViewModelBase { public Test() { ServiceLocator.GetService(); ServiceLocator.GetService(); } } vs. Dependency Injection which looks something like: public class Test : ViewModelBase { public Test(IFoo1 foo1, IFoo2 foo2) { } } at some point early in the start up, you are supposed to register the interface <-> implementation class mappings? So if I want to add DI support to my MVVM lib, it is my understanding that the ViewLocator needs to go through the params in the constructor and match up the interfaces with the classes that you registered. I am **NOT** interested in using a DI lib that somebody else wrote like Unity, etc. I am interested in knowing how they work and implementing my own. Seems like the requirement is that you have a single public constructor? Some libs I have seen add a custom attribute like [DependencyInjection] or [Ninject] to the one constructor that you want the DI to happen on? What is the best approach? Are these libs using reflection to go through the constructor params? Or some other cool mechanism for which I am unaware? I assume there is some kind of caching going on? Like, it only needs to go through the reflection once and then caches the interfaces and the order of which they are passed in? Last question for now... seems like ServiceLocator, you pass in the type and a class instance to register, but in DI, you pass in an interface type and a class instance type?

        J Offline
        J Offline
        James McConville
        wrote on last edited by
        #3

        hey, I just started taking a look at this myself. Try the following - takes you through building your own (if very simple IOC).. http://weblogs.asp.net/sfeldman/archive/2008/02/14/understanding-ioc-container.aspx[^] Hope that helps.

        S 1 Reply Last reply
        0
        • J James McConville

          hey, I just started taking a look at this myself. Try the following - takes you through building your own (if very simple IOC).. http://weblogs.asp.net/sfeldman/archive/2008/02/14/understanding-ioc-container.aspx[^] Hope that helps.

          S Offline
          S Offline
          SledgeHammer01
          wrote on last edited by
          #4

          Jesus... I can barely read that article :-D. The English on it is so poor! The guy is just randomly making up words as he goes along it seems. Anyways, thanks for the link... it seems to be LEADING to DI, but its not DI. He does something like this: public Gadget() : this(Container.Instance.GetImplementerOf()) { } thats kind of manual DI which is not really DI IMO. Its more Service Locator. EDIT: I also saw part 2 of his blog where he just added XML support to the container. Still not really DI since he is still manually resolving the dependencies which is more Service Locator as I mentioned above. I mean, the pattern is sort of DI with the interfaces in the constructor, but then he uses manual resolution whereas true DI is automatic.

          J 1 Reply Last reply
          0
          • A Abhinav S

            I thought Unity was open source - why dont you have a look at the source code itself?

            The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

            S Offline
            S Offline
            SledgeHammer01
            wrote on last edited by
            #5

            Thanks, I took a look at the code. Its still not very clear how it works. It doesn't seem to use reflection for example to determine parameters. Seems like it uses some other unknown (to me) mechanism.

            1 Reply Last reply
            0
            • S SledgeHammer01

              Jesus... I can barely read that article :-D. The English on it is so poor! The guy is just randomly making up words as he goes along it seems. Anyways, thanks for the link... it seems to be LEADING to DI, but its not DI. He does something like this: public Gadget() : this(Container.Instance.GetImplementerOf()) { } thats kind of manual DI which is not really DI IMO. Its more Service Locator. EDIT: I also saw part 2 of his blog where he just added XML support to the container. Still not really DI since he is still manually resolving the dependencies which is more Service Locator as I mentioned above. I mean, the pattern is sort of DI with the interfaces in the constructor, but then he uses manual resolution whereas true DI is automatic.

              J Offline
              J Offline
              James McConville
              wrote on last edited by
              #6

              Yeah, I Agree, that example he uses is a bit dodgy but I can understand why he didn't want to write an entire DI engine for a beginners tutorial. Anyway, Like I say, I found it was an ok place to start before moving on to other stuff.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups