none
what is the use of dbproviderfactory calsses why we need them RRS feed

  • Question

  • any one who can explain better than msdn learning?

    better than this

    "Represents a set of methods for creating instances of the System.Data.SqlClient provider's implementation of the data source classes."!

    what is the importance of  them being singletons?

    i mean what is the difference between

    Mynotsingleton  notsing1=new Mynotsinglton()
    Mynotsingleton  notsing2=new Mynotsinglton()
    
    and
    
    DbProviderFactory  sing1=SingletonFactory.instance
    DbProviderFactory  sing2=SingletonFactory.instance

    in both cases i am having instances and i can use them why they say you can have only one  instance of a singlton:

    confusing to me


    • Edited by kobosh Tuesday, May 21, 2013 11:58 PM
    Tuesday, May 21, 2013 11:57 PM

Answers

  • In addition to chilberto's reply, I would add that in general a Factory Pattern facilitates the creation of new objects without the need to create a lot of different constructors for different configurations of an instance. It can also allow a base type to create instances of derived types based upon the input parameters of the factory method.

    Consider a base class called Animal that has derived classes of Mammal, Bird, and Fish.

    A factory method on animal might be:

    Animal.CreateNew(legs As Integer, wings As Integer, fins As Integer) As Animal

    Calling this CreateNew method might return an instance of Mammal, Bird, or Fish depending on the values supplied to the arguments:

    Public Class Form1
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim a As Animal = Animal.Create(0, 1, 0)
            Debug.Print(a.GetType.Name) 'prints "Bird"
        End Sub
    End Class
    
    Public Class Animal
        Public Shared Function Create(legs As Integer, wings As Integer, fins As Integer) As Animal
            If legs > 0 Then
                Return New Mammal
            ElseIf wings > 0 Then
                Return New Bird
            ElseIf fins > 0 Then
                Return New Fish
            End If
        End Function
    End Class
    
    Public Class Mammal
        Inherits Animal
    End Class
    
    Public Class Bird
        Inherits Animal
    End Class
    
    Public Class Fish
        Inherits Animal
    End Class

    Does that help?


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 22, 2013 11:12 PM
    Moderator
  • Hi Kobosh,

    >>is this going to work with     Oracle

    No, this is dedicated for MS Sql server. 

    For oracle, you need this: http://msdn.microsoft.com/en-us/magazine/system.data.oracleclient.oracleclientfactory.instance.aspx  

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 22, 2013 4:08 PM
    Moderator
  • Hello Kobosh,

    To answer your original question of what is the use of dbproviderfactory:  It is to provide an abstraction between the functionality of the dbprovider and its implementation. 

    For example, all extensions of the dbproviderfactory needs to support the CreateCommand method.  This could be SQL Server, Oracle or a custom one that is created for a particular business need (e.g., a dbprovider to a legacy system). 

    Strictly speaking, an implementation does not have to be singleton. 

    Also, in your example, the difference is sing1 and sing2 actually point to the same instance where notsing1 and notsing2 are two separate instances. 

    Let me know if this does not make sense or answers your question.


    Jeff

    Wednesday, May 22, 2013 9:54 PM

All replies

  • DB Provider Factory helps you to write provider independent code in ADO.NET. It means, your code will work irrespective of database provider you are using (sql server, MySQL, oracle etc.).

    And why Singleton, well, once you get used to DB Provider Factory model, you will understand it's significance.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, May 22, 2013 5:40 AM
  • where is the independence here

     DbProviderFactory fct  = SqlClientFactory.Instance;

    here I am specifying sql  as the provider ; is this going to work with     Oracle?

    Wednesday, May 22, 2013 12:56 PM
  • Hi Kobosh,

    >>is this going to work with     Oracle

    No, this is dedicated for MS Sql server. 

    For oracle, you need this: http://msdn.microsoft.com/en-us/magazine/system.data.oracleclient.oracleclientfactory.instance.aspx  

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 22, 2013 4:08 PM
    Moderator
  • Hello Kobosh,

    To answer your original question of what is the use of dbproviderfactory:  It is to provide an abstraction between the functionality of the dbprovider and its implementation. 

    For example, all extensions of the dbproviderfactory needs to support the CreateCommand method.  This could be SQL Server, Oracle or a custom one that is created for a particular business need (e.g., a dbprovider to a legacy system). 

    Strictly speaking, an implementation does not have to be singleton. 

    Also, in your example, the difference is sing1 and sing2 actually point to the same instance where notsing1 and notsing2 are two separate instances. 

    Let me know if this does not make sense or answers your question.


    Jeff

    Wednesday, May 22, 2013 9:54 PM
  • In addition to chilberto's reply, I would add that in general a Factory Pattern facilitates the creation of new objects without the need to create a lot of different constructors for different configurations of an instance. It can also allow a base type to create instances of derived types based upon the input parameters of the factory method.

    Consider a base class called Animal that has derived classes of Mammal, Bird, and Fish.

    A factory method on animal might be:

    Animal.CreateNew(legs As Integer, wings As Integer, fins As Integer) As Animal

    Calling this CreateNew method might return an instance of Mammal, Bird, or Fish depending on the values supplied to the arguments:

    Public Class Form1
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim a As Animal = Animal.Create(0, 1, 0)
            Debug.Print(a.GetType.Name) 'prints "Bird"
        End Sub
    End Class
    
    Public Class Animal
        Public Shared Function Create(legs As Integer, wings As Integer, fins As Integer) As Animal
            If legs > 0 Then
                Return New Mammal
            ElseIf wings > 0 Then
                Return New Bird
            ElseIf fins > 0 Then
                Return New Fish
            End If
        End Function
    End Class
    
    Public Class Mammal
        Inherits Animal
    End Class
    
    Public Class Bird
        Inherits Animal
    End Class
    
    Public Class Fish
        Inherits Animal
    End Class

    Does that help?


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 22, 2013 11:12 PM
    Moderator
  • thanks it clarifies a bit; but still i dont see any advantage from economical point of view for using a factory; i mean you are going to write the same number of lines of code: for command text, params, connection etc 
    Friday, May 24, 2013 2:05 PM
  • thanks as I have answered to reed there is no economical advantage in terms of lines of code I am going to write; also if I need to change code from one provider to another I need to go over old code and  make changes on every line

    Friday, May 24, 2013 2:09 PM
  • thanks as I have answered to reed there is no economical advantage in terms of lines of code I am going to write; also if I need to change code from one provider to another I need to go over old code and  make changes on every line

    That doesn't really have anything to do with the Factory Pattern.

    The number of lines of code is very arbitrary, to the point of being meaningless.  With lambdas and LINQ and line break characters you can get many statements on one line in any number of ways - or use continuation characters to break one line into multiple lines.  The number of lines of source code tends to be purely a personal preference.

    If you need to make a change to existing code, then yes, you would need to update any subsequent code affected by the change.  But again, none of that is really what the factory pattern is about.

    -EDIT-

    At the end of the day, as with most any pattern, if it doesn't fit your requirements or you simply don't like it, you don't use it.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Friday, May 24, 2013 4:56 PM
    Moderator