Re: where do I put global data?
From:
Date: 01/13/05
- Next message: glenn: "mapping network drive"
- Previous message:
: "Re: ActiveSync provider" - In reply to: Darren Shaffer: "Re: where do I put global data?"
- Next in thread: Jan Yeh_eMVP: "Re: where do I put global data?"
- Reply: Jan Yeh_eMVP: "Re: where do I put global data?"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 13 Jan 2005 08:16:49 -0500
But if you have a global class with static properties and methods to access
information then those methods (and a static constructor) are inherently
singletons and you never have to have an instance of the class. In what
ways is the Singleton pattern better for this (I really want to know, that's
not sarcasm)?
-Chris
"Darren Shaffer" <darrenshaffer@discussions.microsoft.com> wrote in message
news:OTJRtXP%23EHA.2608@TK2MSFTNGP10.phx.gbl...
>I encourage CF developers to create re-usable singleton classes that
>encapsulate logic
> and state that is typically required across many forms in a Windows Mobile
> applications. Most of my CF apps
> contain a Globals singleton, a DatabaseManager singleton, a MapPointWS
> singleton, a SynchManager
> singleton, a Scanner singleton, etc that allow me to quickly add
> functionality to my CF projects that can be
> leveraged from all forms and other classes in the app.
>
> Regardless of what form you're on, you can then write code like this:
>
> string sWorker = Globals.GetInstance().WorkerID;
>
> The reason Ilya discouraged your initial idea is that good object-oriented
> software engineering respects
> the concepts of encapsulation and information hiding. You should only
> place those members in the Globals
> class that are *truly* global (i.e. needed across multiple forms in your
> application or across the lifetime
> of the execution of your app). Everything else should be contained in the
> appropriate scope
> and made private. I'd add that I've seen many CF apps where there is SQL
> CE, MapPoint, and
> Symbol Scanner code littered across every form in the app, much off it
> copied and pasted. It is much cleaner
> to expose interfaces to these "common components of mobility" in a single,
> reusable class. And I've found
> no better way to do that in the Compact Framework than the singleton
> pattern.
>
> If you are unfamiliar with the singleton OO design pattern, here's how to
> implement it:
>
> public class Globals
> {
> private static Globals instance;
>
> // note that the members below are truly global across the app
> private int _workerID;
> private int _eventId;
> private int _leadsUploaded = 0;
> private int _leadsEntered = 0;
> private bool _isInLeadEditMode;
> private string _password;
> private string _userID;
> private string _eventName;
> private string _lastSynch;
>
> public Globals()
> {
> }
>
> // singleton pattern
> public static Globals GetInstance()
> {
> if ( instance == null )
> {
> instance = new Globals();
> }
> return instance;
> }
>
> --Darren Shaffer
> Principal Architect
> Connected Innovation
> www.connectedinnovation.com
>
>
>
> "briforge" <briforge@discussions.microsoft.com> wrote in message
> news:92169D6B-BB3E-4CE9-8905-8A6BB8282F97@microsoft.com...
>>I wish you could edit posts here, since the more I thought about this, I
>> would have re-phrased it.
>>
>> I assume you can simply set public variables for a form, and access them
>> that way. So I think I'm confused by how forms are managed, and
>> therefore
>> how a form would access public data from another form. I guess I could
>> simply pass the data I want to access to each form as it is displayed,
>> but
>> that seems kludgy.
>>
>> "briforge" wrote:
>>
>>> I'm working on my first C# .NET app for PPC, coming from Palm
>>> development
>>> with c++. So far I've got a simple app with a couple forms. I want to
>>> store
>>> data in a class I've created which is accessible from all the different
>>> forms. In C++ I did that with global variables. How do I do that in
>>> C#.NET?
>
>
- Next message: glenn: "mapping network drive"
- Previous message:
: "Re: ActiveSync provider" - In reply to: Darren Shaffer: "Re: where do I put global data?"
- Next in thread: Jan Yeh_eMVP: "Re: where do I put global data?"
- Reply: Jan Yeh_eMVP: "Re: where do I put global data?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|