Using Factory with DryIoc and getting object by string RRS feed

  • Question

  • I am just testing invertion control using DryIoc library. Please check above to get the picture.I have that factory class:

     'FactoryConnections using IoC Dry
        Public Class FactoryConnections
            Private Shared ReadOnly Container As Container
            Shared Sub New()
                Container = New Container()
                With Container
                    .Register(Of ISftp, Sftp)
                    .Register(Of ISaop, Soap)
                    '.Register(Of IFtp, Ftp)
                    .Register(Of IFtp, Ftp)(serviceKey:= "some string value")
                End With
            End Sub
            Public Shared Function ResolveService(Of T As IProtocol)(value As Iprotocol) As T
                Return Container.Resolve(Of T)
            End Function
        End Class

    Just shortlyaccordinig to pattern:

    A ->  Y  (A uses Y)
    A -> Interface B -> Y (in this case A doesn't know about Y)

    we can read my solution the same:

    A -> IFtp -> Ftp

    Therefore instead of getting directly object like: Ftp, Sftp or Soap code should ask factory to get one of them based on the interface therefore calling point doesn't know exactly what concrete object is using but it knows who implements that - that's at elast my understanding. Therefore i could ask calling them via interfaces. This is what i am trying to do however from string value (project related requirment).

    Dim myProtocol As String = "IFtp"

    And as mentioned having that i want to get proper object from my factory in this case i should get New instance of Ftp right? Note that parent interface for IFtp, ISoap, ISftp is IProtocol. I was trying like this:

    Dim myProtocol As String = "IFtp"
    Dim itemprotocol As type = Type.GetType(myProtocol)
    Dim realprotocolobject = FactoryConnections.ResolveService(TypeOf(itemprotocol)) 

    and also tried like this:

    Dim myProtocol As String = "IFtp"
    Dim realprotocolobject = FactoryConnections.ResolveService(Of IProtocol)(myProtocol)

    What's wrong, What am i doing wrong?

    One more thing: what is the diffrence between those lines? (just saw this serviceKey somwhere..)

    .Register(Of IFtp, Ftp)
    .Register(Of IFtp, Ftp)(serviceKey:= "some string value") 
    Saturday, February 17, 2018 8:27 PM

All replies

  • Hi JimmyJimm,

    I cannot run your code, what are Container, IProtocol? can you please provide entire demo that can produce the same issue here.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, February 20, 2018 1:28 PM
  • Hi i will put more code when back home. Please expect my message soon.
    Wednesday, February 21, 2018 8:30 PM