Single Linq Context in WinForms application

A

Andrus

For winforms application with multiple related forms it is reasonable to
create Linq database context object in start of application.
Context object is released only when application exits.

So connection to database may remain open for all application run time.
For such a long time, some routers close connection. This causes Connection
is broken error in application.

How to fix this ?

Andrus.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

Generally speaking, a context is not meant to be open for the life of an
application. It implies that you want to keep track of all the objects that
were ever created, or ever will be created in the scope of your app, and
generally, that is way too broad.

Rather, when you have a specific group of operations that you are going
to perform, you should create a new instance of the context, use it, and
then dispose of it. Your app is not one giant context, rather, you have
multiple sub-functions which really are their own contexts. You need to
identify these and then work from there.

Also, I seriously doubt that the context is keeping the database
connections open. That's just horrible practice. While I am not positive,
I am guessing that there is some connection pooling going on and that those
connections are what are actually being dropped. The actual connections in
code are closed correctly. Keeping them open would provide no benefits to
the context.
 
M

Marc Gravell

Also, I seriously doubt that the context is keeping the
database connections open.
Actually, based on the other problems Andrus has seen with DbLinq
(i.e. not LINQ-to-SQL), it wouldn't surprise me in the least...

Marc
 
A

Andrus

Nicholas,
Generally speaking, a context is not meant to be open for the life of
an application. It implies that you want to keep track of all the objects
that were ever created, or ever will be created in the scope of your app,
and generally, that is way too broad.

Rather, when you have a specific group of operations that you are going
to perform, you should create a new instance of the context, use it, and
then dispose of it. Your app is not one giant context, rather, you have
multiple sub-functions which really are their own contexts. You need to
identify these and then work from there.

I'm creating multi form winforms application working with server over
internet.
I need to cache data so that every form does not read data from database.

Only way in Linq to do this is use Context object.
So I need to use single global context in application as global cache.

However I have no idea how to edit data for some form.

Should I create new context for every form for data editing and keep this
context open until form is closed ?
If so how to refresh global context when changes to form temporary context
are submitted ?

Andrus.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

Caching the data on the app level is just too broad. Generally, if your
individual forms are going to handle a single operation (not a single data
operation, but it is a logical context), then you should load the data when
the form opens and close the context when the form is closed (or saved,
depending on the workflow of your app).

Generally, you cache data which is not going to be modified very often.
That's the whole point of caching. If you were to keep a cache using the
context of all the items in the database, you are going to have a nightmare
when it comes to concurrency checking, since you would probably have to
re-load the data anyways anytime you open a new form (depending on the
frequency of changes).
 
A

Andrus

Nicholas,

In start of application and during work user can open approx 5-10 forms.
Those forms may be closed only when application finishes.

User edits data in some form data and press Save button.
User expects that other forms show the new data changed by other forms when
other forms are edited.

Lets say customer name is changed in customer form. New name must appear in
invoice, order etc. forms.
Using your recommendation I have 5 open contexts. Customer entity objects
are cached in other contexts. So customer name change does
not appear in other forms.

How to force one context changes to appear in all other contexts ?

Andrus.
Caching the data on the app level is just too broad. Generally, if
your individual forms are going to handle a single operation (not a single
data operation, but it is a logical context), then you should load the
data when the form opens and close the context when the form is closed (or
saved, depending on the workflow of your app).

Generally, you cache data which is not going to be modified very often.
That's the whole point of caching. If you were to keep a cache using the
context of all the items in the database, you are going to have a
nightmare when it comes to concurrency checking, since you would probably
have to re-load the data anyways anytime you open a new form (depending on
the frequency of changes).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrus said:
Nicholas,


I'm creating multi form winforms application working with server over
internet.
I need to cache data so that every form does not read data from database.

Only way in Linq to do this is use Context object.
So I need to use single global context in application as global cache.

However I have no idea how to edit data for some form.

Should I create new context for every form for data editing and keep this
context open until form is closed ?
If so how to refresh global context when changes to form temporary
context are submitted ?

Andrus.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

That's up to you to do. The Context class is not meant to be a caching
mechanism, but rather, a way of keeping track of changes that have occured
between operations on the data. It's not meant to carry that change state
over to the next operation.

Even if it was, it has no way of knowing when a change is made by the
database by some other means.

So when you make a change on any of your forms, you need to send a
notification to the other forms to indicate that a change has taken place.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrus said:
Nicholas,

In start of application and during work user can open approx 5-10 forms.
Those forms may be closed only when application finishes.

User edits data in some form data and press Save button.
User expects that other forms show the new data changed by other forms
when other forms are edited.

Lets say customer name is changed in customer form. New name must appear
in invoice, order etc. forms.
Using your recommendation I have 5 open contexts. Customer entity objects
are cached in other contexts. So customer name change does
not appear in other forms.

How to force one context changes to appear in all other contexts ?

Andrus.
Caching the data on the app level is just too broad. Generally, if
your individual forms are going to handle a single operation (not a
single data operation, but it is a logical context), then you should load
the data when the form opens and close the context when the form is
closed (or saved, depending on the workflow of your app).

Generally, you cache data which is not going to be modified very
often. That's the whole point of caching. If you were to keep a cache
using the context of all the items in the database, you are going to have
a nightmare when it comes to concurrency checking, since you would
probably have to re-load the data anyways anytime you open a new form
(depending on the frequency of changes).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrus said:
Nicholas,

Generally speaking, a context is not meant to be open for the life
of an application. It implies that you want to keep track of all the
objects that were ever created, or ever will be created in the scope of
your app, and generally, that is way too broad.

Rather, when you have a specific group of operations that you are
going to perform, you should create a new instance of the context, use
it, and then dispose of it. Your app is not one giant context, rather,
you have multiple sub-functions which really are their own contexts.
You need to identify these and then work from there.

I'm creating multi form winforms application working with server over
internet.
I need to cache data so that every form does not read data from
database.

Only way in Linq to do this is use Context object.
So I need to use single global context in application as global cache.

However I have no idea how to edit data for some form.

Should I create new context for every form for data editing and keep
this context open until form is closed ?
If so how to refresh global context when changes to form temporary
context are submitted ?

Andrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top