vinodar Posted September 27, 2004 Posted September 27, 2004 I'm developing a multi form windows application in c#. All the forms uses some common objects like database connection etc. Is it okay to create new object(for eg:- open a database connection ) in each form or is there any other place (something like session in ASP.NET) where we can store an object and use in all forms. Looking forward for advice from those who have already encountered this situation. Thanks Vinod Quote
mocella Posted September 27, 2004 Posted September 27, 2004 You didn't mention so I'll ask if you're doing this as an MDI app, or just having a bunch of forms open on the desktop or what. Either way, you can deem one form as the driver (mdiParent if MDI) and have that form create the instances of your objects. Then you can create properties or pass references or several other methods to allow the "child" level screens to have access to those objects. Quote
vinodar Posted September 27, 2004 Author Posted September 27, 2004 Thanks for your reply. It's an MDI window application and I can open multiple windows from the main form. What confuses me is , when I start the application (at the time of login), I will open a database connection. This connection should be persistant during the entire application life cycle. In ASP.NET, we have lot of state management objects. Like to know whether we could store the object in some 'pplication context' and use it in each forms. Thanks Vinod Quote
mocella Posted September 27, 2004 Posted September 27, 2004 I don't think I'd leave a database connection open for the duration of an application's lifetime - just open and close it as you use it. You can keep your connection object alive in the mdi-parent form and perhaps create a public property that the mdi-child forms can reference for their data needs, or you can pass a reference of this connection object to the child forms at constructor time. I'm not sure if there's some context object like how ASP.Net does things - but with the mdi-parent you really don't need one - the mdi-parent can act as this "global object server" for all the mdi-child forms. Quote
*Experts* mutant Posted September 27, 2004 *Experts* Posted September 27, 2004 This might be just me, but I would create a separate class with all the needed objects exposed as static or instanced as needed, instead of making the MDI parent form a container for all that. :) Quote
mocella Posted September 27, 2004 Posted September 27, 2004 Perhaps the database connection object is a bad example here - since a static helper class can probably suit those needs as mutant points out. However, for a stateful object - like some datatables (assuming you're not caching) you need in various forms , or session information that is just stored in-memory while the app is running, etc, I'd just assume hold reference to this object in the mdi-Parent, as it is the main entry/exit point for forms in the app. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.