Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

Posted

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

Posted

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.

  • *Experts*
Posted

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.

:)

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...