开发者

Proper way to use Linq with WPF

开发者 https://www.devze.com 2023-01-03 18:04 出处:网络
I\'m looking for a good guide into the right method of using Linq to Sql together with WPF. Most guides only go into the bare basics like how to show data from a database but noone I found goes into

I'm looking for a good guide into the right method of using Linq to Sql together with WPF.

Most guides only go into the bare basics like how to show data from a database but noone I found goes into how to save back to the database. Can you answer or point out to me a guide that can answer these questions.

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

1) Where there are collections is it best to return as a IQueryable or should I return a list?

Inside the WPF project I have seen reccomendations to wrap the collection in a ObservabgleCollection.

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

Some properties of the linq entities should be editable in the app so I set them to two-way mode. That would change the object in the observableCollection.

3) Is the object in the ObservableCollection still a instance of the original linq entity and so is the change reflected in the database ( when submitchanges is called )

I should have somekind of save method in the repository. But when should I call it? What happens if some开发者_运维百科one edits a field but decides not to save it, goes to another object and edits it and then press save. Doesn't the original change also save? When does it not remember the changes to a linq entity object anymore. Should I instance the Datacontext class in each method so it loses scope when done.

4) When and how to call the SubmitChanges method

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

To add a new row I should create a new object in a event ( "new" button push ) and then add it to the database using a repo method.

6) When I add the object to the database there will be no new object in the ObservableCollection. Do I refresh somehow.

7) I wan't to reuse the edit window when creating new but not sure how to dynamically changing from referencing selected item from a listview to this new object. Any examples you can point out.


A lot of questions! I will answer a few.

1) Use IQueryable whenever you wish to perform a LINQ query on it. Use of 'var' keyword instead of specifying a type is also preferred.

2) ObservableCollection provides notification mechanism whenever the number of items in the collection changes. That way a list control can refresh itself whenever a collection changes.

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

3) I recommend not storing LINQ entities directly into the collection. Reason is if you want to propagate changes back to database, you must keep the DataContext open from which that entity was created. That keeps the connection open and is not advised.

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消