0

i'm doing a little project for a friend's business with C# Wpf and Entity Framework and im having some problems.

Here is what i've done so far: enter image description here

I'm going to explain a little bit the diagram. The program is separated by tabs in clients, users, stock, etc. Every part is sub-separated, for example for clients is separated in View clients, Add client, etc.. Each separation is made with a different Xaml Control and the context to that control is set in the parent xaml to a property object existing the parent UI class.

So, there is a Main Xaml Window with context set to General UI Class in that main xaml window there is a control of Clients Xaml with context set to Client General UI Class.

And so on, in clients xaml there is a control of View Clients Xaml with context set to View Clients UI Class and a control of Add client Xaml with context set to Add Client UI Class

In View Clients UI Class is an element of Client Wrapper Class which is the current selected client to show with the client properties binded to the form controls. And there is also a list of Client Wrapper Class to show and search all existing clients.

Same concept for Add Client UI Class

Base Db Client Class is the class generated by Entity Framework to manage the entity in the database. This class is wrapped with Client Wrapper Class for purposes of data managment (some properties return complex data) but more important for WPF binding since Client Wrapper Class implements PropertyChangedEventHandler.

Now that we have the basics of the structure lets talk about the problems.

  • Client Wrapper Class has an element of Base Db Client Class and im not sure how to retrive the data of that element, if copy it or just return the properties of Base Db Client Class

  • When I retrive a list of Client Wrapper Class to store in View Clients UI Class relationship properties from clients like created by or status are disposed and the binding throws an error when try to display those properies in the form.

Sorry for not being more explicit and the poor quality of the diagram.

I hope you can point me in the right direction

Thank you all!!

Lolrapa
  • 183
  • 1
  • 9

1 Answers1

0

I would suggest using some design patterns when dealing with WPF and EF. The main patter you should know about is MVVM. Maybe you can use a ready made Framework for the purpose.

The UI logic is implemented in the ViewModel but you can decide how you would like to deal with the data (the Model).

  1. You can reference your data in the ViewModel and load, modify and save that when neccessary using Commands. For the mapping you can use AutoMapper if your ViewModel properties follow the same structure as your Model.

  2. Or you can generate observable proxy classes from the EF model using a T4 Template. This options is feasible if your application is not too complex and you never detach the entities from the EF context. Then you can publish your Model as a single property and bind the data via that in the View. Only the properties acomplish UI logic would be included in the ViewModel.

  3. You can implement or generate intermediate Model classes for your entities and map them back and forth. This method can be used when you should serialize and send your data from and to the client. This method also gives you the opportunity to separate UI behavior that is specific to the data or the view.

I would suggest you to implement different classes for read only views of the data such as listing and use them in Linq queries when retrieving a bigger amount of rows.

In order to deal with navigation properties and collections your have several options:

  1. You can turn off Lazy Loading on EF and load the requested part of the object graph using Explicit loading

  2. You can use Lazy loading but you should not detach your entities from the EF context.

When you update your entities:

  1. You can use GraphDiff. It is no longer maintained, but the current version can be built against EF6 and fesible for most situations. This approach uses detached entities.

  2. Or you can use your detached entities and try to attach them directly to EF and update. This one is risky because you do not have too much control in your saving logic about what is going to be created or deleted.

  3. The other option is to check every navigation property and collection in your saving logic and map the appropriate changes back to the entity level or attach/insert every nested data before updating the entity. This one has the most control but needs more work as well.

I hope I could help,

Community
  • 1
  • 1
Daniel Leiszen
  • 1,827
  • 20
  • 39