none
Unity: pass an existing model object RRS feed

  • Question

  • I'm slowly learning how to use Unity for dependency injection but one piece has me stumped and wondering if I've understood any of it!

     

    In a winforms application I have a search bar to find a customer. Depending on the type of policy the customer has will determine which Form is opened as a result. I'm also trying to stick to some kind of MVP pattern so essential I have a SearchPresenter with a SearchView. The user puts a policy number in the textbox and clicks search which causes the SearchPresenter to call some services in order to find a customer. If none are found it passes this back to the view to let the user know. If it finds a result, I now want to pass this on to a 'CustomerPresenter' and 'CustomerView' (i.e. the new form). But this is where I'm stuck.

     

    How do I inform the CustomerPresenter what the customer object (the model) is that I've found? The winfrom is an MDI container that will allow the user to open multiple customers at a time so I cannot create a lifetime object. Everything I've tried results in a new customer object being created in the CustomerPresenter when I really want to pass the existing customer from SearchPresenter over to the CustomerPresenter.

     

    Is there a way to do this or have I missunderstood the usage of a unitycontainer? Should I be creating sub containers for each 'customer' or is this taking me further off track?

     

    Any help or pointers in the right direction would be most appreciated. 

    EDIT: After spending the morning looking into this I'm now thinking an 'Abstract Factory' is what I need. My understanding now is Unity is used to create an object graph at the start of the application lifetime. Because i need to create objects based on user interaction these cannot be known at the start, but I can register factories to create these objects at a later time. 

    So my question now is

    a) Am I correct in saying for the example above i should have a factory registered in the container for creating a CustomerPresenter and passing in the customer model via the factory object rather than resolving it. (this seems to fit with a lot I've read about your objects being unaware of the container). 

    b) Are there any good official explanations of using Func() to create a factory from an interface without having to code the concrete factory. Any VB.net examples would be good! There seems to be nothing on MSDN I can find about this feature. 


    • Edited by j_dhoward Wednesday, September 23, 2015 11:42 AM
    Wednesday, September 23, 2015 9:16 AM

All replies

  • Hi,

    Is there a way to do this or have I missunderstood the usage of a unitycontainer? Should I be creating sub containers for each 'customer' or is this taking me further off track?

    https://msdn.microsoft.com/en-us/library/ff648211.aspx?f=255&MSPPError=-2147217396

    You could check this.

    And I suggest you sharing some codes with us, If we have the project, we could help you better.

    Thursday, September 24, 2015 6:48 AM
  • Thanks, I had read that whole book and having spent the whole day looking into this I think I've got a clue as to what I'm doing wrong but would still appreciate help.

     

    If I'm now clear on how to use Unity then you would register all objects with the container at the start of the applications lifetime. However, in the example I gave you wouldn't know exactly what objects are needed and you wouldn't want to create them until such a time the user may request them. This seems to be where a factory/abstract factory comes into use. This factory would be registered into the entity and injected into any objects that would need to create other objects.

     

    I'd love to give some code but my current project is massive and I can't get my head round this enough to write a small example. Also, I think I'm struggling because all the examples are so small and don't show the whole picture.  Even the examples on the MSDN link you gave are considered 'wrong' usage if you go by what a number of other say when used in the real world - many say you should use a factory injected into other objects rather than allowing all objects access to the container and its resolve method?

    Thursday, September 24, 2015 8:06 AM