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
  1. Home
  2. General Programming
  3. C#
  4. Static or instance methods in Data Access Layer

Static or instance methods in Data Access Layer

Scheduled Pinned Locked Moved C#
discussionbeta-testingquestioncode-review
3 Posts 2 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.
  • R Offline
    R Offline
    roel_v
    wrote on last edited by
    #1

    Hi, I've been wondering about this for a long time, so I decided to post this question here to get some opinions. Suppose I have a Data Access Layer (DAL) with a CustomerData object. This CustomerData object has a GetCustomers() method which returns a DataSet (or a CustomerDataSet typed data set.) Assuming this method is entirely stateless (no member data accessed), which it really should be in a DAL, is it considered best practice to make this a static method or an instance method. I like the idea of a static method, so I can code: DataSet myCustomers = CustomerData.GetCustomers(); instead of CustomerData myCustomerData = new CustomerData(); DataSet myCustomers = CustomerData.GetCustomers(); Since there is never any instance data, I don't really see the use in using an instance of this class. However, I am sure their are other things to consider here, so I am looking forward to your feedback on this. Roel

    E 1 Reply Last reply
    0
    • R roel_v

      Hi, I've been wondering about this for a long time, so I decided to post this question here to get some opinions. Suppose I have a Data Access Layer (DAL) with a CustomerData object. This CustomerData object has a GetCustomers() method which returns a DataSet (or a CustomerDataSet typed data set.) Assuming this method is entirely stateless (no member data accessed), which it really should be in a DAL, is it considered best practice to make this a static method or an instance method. I like the idea of a static method, so I can code: DataSet myCustomers = CustomerData.GetCustomers(); instead of CustomerData myCustomerData = new CustomerData(); DataSet myCustomers = CustomerData.GetCustomers(); Since there is never any instance data, I don't really see the use in using an instance of this class. However, I am sure their are other things to consider here, so I am looking forward to your feedback on this. Roel

      E Offline
      E Offline
      Eduard Keilholz
      wrote on last edited by
      #2

      I don't think this is a good approach. Why not making your entire application static then? If you instanciate objects, you can clean them up neatly if not required. Your database connection object needs to be static which I experienced lots of problems with (for example using threads). I think there are multiple reasons not to make such functions static, but make you instanciate the class they're in ;-) Removing HTML code from a string or such functions are more qualified to be static

      .: I love it when a plan comes together :. http://www.zonderpunt.nl

      R 1 Reply Last reply
      0
      • E Eduard Keilholz

        I don't think this is a good approach. Why not making your entire application static then? If you instanciate objects, you can clean them up neatly if not required. Your database connection object needs to be static which I experienced lots of problems with (for example using threads). I think there are multiple reasons not to make such functions static, but make you instanciate the class they're in ;-) Removing HTML code from a string or such functions are more qualified to be static

        .: I love it when a plan comes together :. http://www.zonderpunt.nl

        R Offline
        R Offline
        roel_v
        wrote on last edited by
        #3

        Database connection does not necessarily have to be static, it all depends how you write the code. I am using Enterprise Library now... Database db = DatabaseFactory.CreateDatabase(); using (DbCommand dbCommand = db.GetStoredProcCommand("GetAccount")) { etc...

        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