"Global" Dataset - Singleton

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hans Greif

    "Global" Dataset - Singleton

    Hallo,

    hello, i try to implement a global dataset - singleton based.

    On www.bakterienforum.de i found a singleton implementation:

    sealed class SingletonCounte r
    {
    public int Counter = 0;

    private SingletonCounte r(){}
    public static readonly SingletonCounte r Instance = new
    SingletonCounte r();
    }

    Can i use this for implementing the global dataset and how can i do this?

    Thank you in advance.

    hans
  • Simon Tamman

    #2
    Re: "Global&qu ot; Dataset - Singleton

    I usually recommend the following article on the Singleton pattern.



    However I don't think you are looking for a Singleton, you just want a
    static dataset, right?
    So you just need to declare:

    public class SomeClass
    {
    private static DataSet globalDataSet = new DataSet();

    public static DataSet GlobalDataSet
    {
    // TODO: Implement locking if using multiple threads
    get{return globalDataSet;}
    }
    }

    Then you can get access to the dataset by stating:

    SomeClass.Globa lDataSet

    However, a static dataset sounds absolutely hideous to me and I think you
    might be misunderstandin g the concepts of OOP if you are looking to use one.
    Singleton items are usually for objects or services that you only ever want
    1 of in existence, for example an object that handles the database or
    possibly uses a lot of resources (i'm sure there are loads of other good
    reasons too).
    Datasets are usually just currency that move between architectural tiers
    there is hardly ever reason to make static ones.
    Could I ask what exactly do you need this static dataset for?

    HTH

    Simon

    "Hans Greif" <uni@cubs.dewro te in message
    news:4mgn2bF63j reU1@individual .net...
    Hallo,
    >
    hello, i try to implement a global dataset - singleton based.
    >
    On www.bakterienforum.de i found a singleton implementation:
    >
    sealed class SingletonCounte r
    {
    public int Counter = 0;
    >
    private SingletonCounte r(){}
    public static readonly SingletonCounte r Instance = new
    SingletonCounte r();
    }
    >
    Can i use this for implementing the global dataset and how can i do this?
    >
    Thank you in advance.
    >
    hans

    Comment

    • Max Fox

      #3
      Re: &quot;Global&qu ot; Dataset - Singleton

      Am Sat, 09 Sep 2006 21:02:55 GMT schrieb Simon Tamman:

      well, thank you for your answer.
      I want to access a dataset threadsfae from different forms, so i though
      maybe the singleton approach will help.

      If the singleton nor the static is right, what would you suggest. It should
      be threadsafe for sure. Btw, the dataset should be inside the application,
      no external database and so on.

      Thank you in advance

      hans

      I usually recommend the following article on the Singleton pattern.
      >

      >
      However I don't think you are looking for a Singleton, you just want a
      static dataset, right?
      So you just need to declare:
      >
      public class SomeClass
      {
      private static DataSet globalDataSet = new DataSet();
      >
      public static DataSet GlobalDataSet
      {
      // TODO: Implement locking if using multiple threads
      get{return globalDataSet;}
      }
      }
      >
      Then you can get access to the dataset by stating:
      >
      SomeClass.Globa lDataSet
      >
      However, a static dataset sounds absolutely hideous to me and I think you
      might be misunderstandin g the concepts of OOP if you are looking to use
      one.
      Singleton items are usually for objects or services that you only ever
      want
      1 of in existence, for example an object that handles the database or
      possibly uses a lot of resources (i'm sure there are loads of other good
      reasons too).
      Datasets are usually just currency that move between architectural tiers
      there is hardly ever reason to make static ones.
      Could I ask what exactly do you need this static dataset for?
      >
      HTH
      >
      Simon
      >
      "Hans Greif" <uni@cubs.dewro te in message
      news:4mgn2bF63j reU1@individual .net...
      >Hallo,
      >>
      >hello, i try to implement a global dataset - singleton based.
      >>
      >On www.bakterienforum.de i found a singleton implementation:
      >>
      >sealed class SingletonCounte r
      >{
      > public int Counter = 0;
      >>
      > private SingletonCounte r(){}
      > public static readonly SingletonCounte r Instance = new
      >SingletonCount er();
      >}
      >>
      >Can i use this for implementing the global dataset and how can i do this?
      >>
      >Thank you in advance.
      >>
      >hans

      Comment

      • Ciaran O''Donnell

        #4
        RE: &quot;Global&qu ot; Dataset - Singleton

        I assume their is a potential need to synchronise access to the dataset as
        some threads are going to change values inside it. What exactly are the
        threads doing to the dataset and how up to date do they all need to be about
        changes. The answers to these questions will determine the complexity of the
        synchronisation code.

        Ciaran O'Donnell

        "Hans Greif" wrote:
        Hallo,
        >
        hello, i try to implement a global dataset - singleton based.
        >
        On www.bakterienforum.de i found a singleton implementation:
        >
        sealed class SingletonCounte r
        {
        public int Counter = 0;
        >
        private SingletonCounte r(){}
        public static readonly SingletonCounte r Instance = new
        SingletonCounte r();
        }
        >
        Can i use this for implementing the global dataset and how can i do this?
        >
        Thank you in advance.
        >
        hans
        >

        Comment

        • Mark Rae

          #5
          Re: &quot;Global&qu ot; Dataset - Singleton

          "Max Fox" <web@cubs.dewro te in message
          news:4mgq4aF65u u4U1@individual .net...
          Am Sat, 09 Sep 2006 21:02:55 GMT schrieb Simon Tamman:
          >
          well, thank you for your answer.
          I want to access a dataset threadsfae from different forms, so i though
          maybe the singleton approach will help.
          >
          If the singleton nor the static is right, what would you suggest. It
          should
          be threadsafe for sure. Btw, the dataset should be inside the application,
          no external database and so on.
          In which case, I'm a little puzzled...

          You want a static dataset, but you don't populate it initially from a
          database...?

          So where does the data come from?

          What sort of data is it?

          What do you subsequently use it for?


          Comment

          • Max Fox

            #6
            Re: &quot;Global&qu ot; Dataset - Singleton

            Am Sun, 10 Sep 2006 07:54:11 +0100 schrieb Mark Rae:
            "Max Fox" <web@cubs.dewro te in message
            news:4mgq4aF65u u4U1@individual .net...
            >
            >Am Sat, 09 Sep 2006 21:02:55 GMT schrieb Simon Tamman:
            >>
            >well, thank you for your answer.
            >I want to access a dataset threadsfae from different forms, so i though
            >maybe the singleton approach will help.
            >>
            >If the singleton nor the static is right, what would you suggest. It
            >should
            >be threadsafe for sure. Btw, the dataset should be inside the
            >application,
            >no external database and so on.
            >
            In which case, I'm a little puzzled...
            >
            You want a static dataset, but you don't populate it initially from a
            database...?
            >
            So where does the data come from?
            >
            What sort of data is it?
            >
            What do you subsequently use it for?
            Well, i do have an initially dataset which is empty and is filled during
            runtime with datas from a file. The Programm is something like a data
            converter.
            So what i have right now is a Dataset inside Form1 which contains the data.
            But i want to access the data also outside form1, from soem different
            classes. The one way is to declare the dataset as static, but that not
            good. So i'm looking for an implementation of the dataset which controls
            the access to it and is threadsafe.

            Am i completely wrong with my thoughts?

            Assuming someone wants to access the data within a dataset from different
            classes, do i have to pass the dataset each time as a parameter, is that
            what OOP means?

            Thank you in advance

            Hans


            Comment

            • Cor Ligthert [MVP]

              #7
              Re: &quot;Global&qu ot; Dataset - Singleton

              Hans,

              Just open in version 2003 a component and drag a dataset on that.

              In version 2005 you can click on top of Visual Studio to make your dataset.

              That both creates a dataset class.

              Creating a dataset static, is a nice example how in my idea static would not
              be used.

              However that is just my opinion,

              Cor


              "Hans Greif" <uni@cubs.desch reef in bericht
              news:4mgn2bF63j reU1@individual .net...
              Hallo,
              >
              hello, i try to implement a global dataset - singleton based.
              >
              On www.bakterienforum.de i found a singleton implementation:
              >
              sealed class SingletonCounte r
              {
              public int Counter = 0;
              >
              private SingletonCounte r(){}
              public static readonly SingletonCounte r Instance = new
              SingletonCounte r();
              }
              >
              Can i use this for implementing the global dataset and how can i do this?
              >
              Thank you in advance.
              >
              hans

              Comment

              • Mark Rae

                #8
                Re: &quot;Global&qu ot; Dataset - Singleton

                "Max Fox" <web@cubs.dewro te in message
                news:4mhv2gF66p mvU1@individual .net...
                The one way is to declare the dataset as static, but that not good.
                Why not?
                So i'm looking for an implementation of the dataset which controls
                the access to it and is threadsafe.
                >
                Am i completely wrong with my thoughts?
                It's difficult to say until you actually tell us what this dataset will
                subsequently be used for.

                Are you using it as the datasource for bound controls?

                Why does it have to be a dataset? Why not a generic?
                Assuming someone wants to access the data within a dataset from different
                classes, do i have to pass the dataset each time as a parameter, is that
                what OOP means?
                Nothing to do with object-orient(at)ed programming per se... I have a
                feeling getting yourself bogged down in nomenclature that you don't really
                understand rather than taking a step back, focussing on the problem, and
                coming up with a solution.

                You mention that the dataset is created when Form1 is loaded - does that
                mean that currently the dataset is created EVERY TIME Form1 is loaded? Is
                that the problem?


                Comment

                • Hans Greif

                  #9
                  Re: &quot;Global&qu ot; Dataset - Singleton

                  Am Sat, 9 Sep 2006 22:38:38 +0200 schrieb Hans Greif:
                  Hallo,
                  >
                  hello, i try to implement a global dataset - singleton based.
                  >
                  On www.bakterienforum.de i found a singleton implementation:
                  >
                  sealed class SingletonCounte r
                  {
                  public int Counter = 0;
                  >
                  private SingletonCounte r(){}
                  public static readonly SingletonCounte r Instance = new
                  SingletonCounte r();
                  }
                  >
                  Can i use this for implementing the global dataset and how can i do this?
                  >
                  Thank you in advance.
                  >
                  hans
                  Well, i got it. thank you

                  Comment

                  Working...