The right way to use Linq with WPF

I am looking for a good guide on the correct method of using Linq to Sql with WPF.

Most manuals relate only to basic principles, for example, how to display data from a database, but none of them found how to save back to the database. Can you answer or point me to a guide that can answer these questions.

I have a separate Data project because the same data will also be used on the web page, so I have a repository method. This means that I have a separate class that uses a DataContext, and there are methods like GetAllCompanies () and GetCompanyById (int id).

1) Where are the collections, is it best to return as IQueryable or do I need to return the list?

Inside the WPF project, I saw recommendations about wrapping a collection in an ObservabgleCollection.

2) Why should I use ObservableCollection and should I use it even with Linq / IQueryable

Some properties of linq objects must be editable in the application, so I set them in two-way mode. This will change the object in the observed set.

3) Is the object in the ObservableCollection still an instance of the original linq object and, therefore, this change is reflected in the database (when signatures are called)

I need to have some save method in the repository. But when should I call it? What happens if someone edits a field, but decides not to save it, moves to another object and edits it, and then click "Save." Does the original change not save? When it no longer remembers changes to the linq object object. Do I have an instance of the Datacontext class in each method, so it loses scope.

4) When and how to call the SubmitChanges method

5) Should I have a DataContext as a member variable of a repository class or method variable

To add a new row, I have to create a new object in the event (β€œnew” button click) and then add it to the database using the repo method.

6) When I add an object to the database, there will not be a new object in the ObservableCollection. Somehow update.

7) I will not reuse the editing window when creating a new one, but I'm not sure how to dynamically change the link to the selected item from the list to this new object. Any examples you can specify.

+4
source share
1 answer

Many questions! I will answer a few.

1) Use IQueryable when you want to execute a LINQ query. It is also preferable to use the keyword 'var' instead of specifying a type.

2) ObservableCollection provides a notification mechanism whenever the number of elements in the collection changes. In this way, the list control can be updated every time the collection changes.

NOTE. You still need to implement the INotifyPropertyChanged interface to notify property changes for individual objects in the collection.

3) I recommend not storing LINQ objects directly in the collection. The reason is that if you want to propagate the changes to the database, you must leave open the DataContext from which this object was created. This keeps the connection open and not recommended.

4) Whenever you want to make changes to the database, create a new DataContext, select the objects you want to change, change their properties and call the SubmitChanges () method. After that close DataContext.

5) As I said, create a new instance of the DataContext whenever you want to select objects from the database or send the changes back to the database.

+1
source

Source: https://habr.com/ru/post/1312872/


All Articles