michaellperry / Assisticant

MIT License
36 stars 19 forks source link

Assisticant with Entity framework #21

Open ebadola opened 7 years ago

ebadola commented 7 years ago

I want know how i can use entity framework with Assisticant, when i should pass business logic to Model and can i load data on ViewModel, and i want know is there any disorder?

michaellperry commented 7 years ago

Great question. I typically use the Memento Pattern when using Assisticant with Entity Framework or a web API.

I will have three different layers, represented by different sets of classes:

The memento layer is a great place for Entity Framework Code First classes. If I'm doing a client-side application, it's the JSON serializable Web API objects.

Execute EF queries from a service. The service will store the results of the query into the model. Since the model is observable, the view will update when the data is loaded. I will often have Observable fields in the service just for keeping track of things like busy indicators and the last exception, so that you can bind those to the UI, too.

The shape of the database is often slightly different from the shape of the model. For one thing, the model represents just the subset that the user is currently working on. For another, For another, it will tend more toward object-oriented patterns than relational ones. Keeping the model separate from the mementos gives each layer the freedom it needs to do the job for which it is intended.

I'll leave this issue open as a reminder to create an EF example. Thanks!

ebadola commented 7 years ago

I'm looking for your EF example. Thanks!

username-not-important commented 7 years ago

I still cannot follow how I can map my domain classes to models with observable fields! Can you please explain this?

michaellperry commented 7 years ago

Sure. The model is the heart of the application. You'll want to create model classes to represent the objects in your domain. All fields of the model classes should be Observable or Observable, instead of just int or string. Create properties of type int or string that get and set the Value of those observables.

The mementos, on the other hand, represent the records in the database. They will have the same data, but might be organized differently. When you load something from the database, EF will return these objects. Copy the data into the model objects.

Hope this is more clear.

username-not-important commented 7 years ago

I Tried this and It works! 👍 Thank you.