0 Replies - 277 Views - Last Post: 17 February 2018 - 01:29 PM Rate Topic: -----

#1 nighttrain   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 485
  • Joined: 22-September 10

Using Factory with DryIoc and getting object by string

Posted 17 February 2018 - 01:29 PM

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") 


Is This A Good Question/Topic? 0
  • +

Page 1 of 1