Putting WPF MVVM and ADO.Net Entity Framework - wpf

I am trying to create a WPF project with MVVM and ADO.Net Entity Framework.
I do have a few questions though.
Do i create the Entity Framework Model in the Model Folder of the MVVM Design?
Or do i have to create a new project for each item of the MVVM Model(i.e Views Project as
a WPF Project,Model Project as a class libraray, ViewModel project as a class library)?

It doesn't really matter where you put stuff, but yes I usually put all my EF or Linq-Sql stuff in a Model folder as they will usually house all of my model classes anyway and it keeps it all together. It is a bit of a grey area though, because it's also doing DataLayer stuff, so just bear that in mind too.
Regarding your 2nd point, no. The Model, View Models and Views should all be in the same project.
Take a look at this for some guidance on MVVM.

Do i create the Entity Framework Model in the Model Folder of the MVVM Design?
I usually create a separate project for the model which is storage ignorant (using POCOs) and also usage ignorant.
Or do i have to create a new project for each item of the MVVM Model(i.e Views Project as a WPF Project,Model Project as a class libraray, ViewModel project as a class library)?
If you are planning on having more than one type of view-model per model (e.g. one for WPF, one for a service, one for another service), then separate your view-models into their own assembly.
If your views only represent one model layer, then you can start out your views and view-models in the same assembly, then separate them if you get to a situation that you have multiple models under the same view (each with its own view-model).
You should also consider if you want the ADO.NET's model to be your main model (upon which you base your view-models), or if you want to treat it as a sort of view-model for storing (where the storage service is a sort of view).

Related

Implement MVVM With Entity Framework

I created database in management studio, a model in my project with the ADO.NET Entity Data model for the tables.
I want to use the MVVM pattern to build forms and update, delete, insert data in my forms.
Can you please give me some guidance how to map the models with my viewmodels. Any tutorials would be nice, I`ve already spend whole day looking for something but got confused at the end.
Is there a simple project with Entity Framework and MVVM.
This is a bit of a contentious subject but I personally don't view change notification as belonging in the exclusive domain of the view/view-model relationship, so I add INPC to my models as as well and expose them in their corresponding view model. This can be done by either injecting proxies into your repositories at runtime (e.g. Castle Dynamic Proxy) or by modifying the IL automatically at compile time (e.g. Fody).
From one noob to another - try searching for 'wpf mvvm entity framework example'. Here's what I found useful...
http://www.software-architects.com/devblog/2010/09/10/MVVM-Tutorial-from-Start-to-Finishhttp://social.technet.microsoft.com/wiki/contents/articles/28209.wpf-entity-framework-mvvm-walk-through-1.aspx
First you can use the repository pattern, to abstract your data access layer, so you viewmodels don't have a tight coupling to Entity Framework and remain easy to test.
Second, you can use an auto mapper like AutoMapper to map from your models to your ViewModels. However, you shouldn't use automappers to map from ViewModels to the View, so you'll have to manually create your model and pass it to your repository for insertion or updating.

New WPF app that uses MVVM and Database First?

I creating my first WPF application, and the goal of this is to basically be a data entry application for the database for CRUD operations (for example, creating a new customer in a new window, editing a customer, displaying a list of customers, etc). A database has been created, and I have thus created a Models folder to contain the .edmx file, dbcontext stuff, and partial classes generated from ADO Entity Data Model tool.
EDIT:
My question is, now, how do I go on with MVVM with my ViewModel and Models working together. All the tutorials I have seen show starting from scratch, and not using existing classes. For example, all tutorials would show creating a brand new Customer class, vs just going off the partial Customer class that was created for me.
I don't want to start over, so how do I follow these tutorials alongside the classes that were created for me through Entity Framework from my database?
In my opinion there is nothing wrong in Database-first approach, even it's quite good, because you would have general view at project (data side).
I would recommend you to start MVVM approach using Caliburn.Micro framework, it's easy, powerful and really nice.
Caliburn.Micro - getting started - part 1 - you can find there all 5 parts.
Caliburn.Micro - framework
MVVM does not specify where you start from, or how you deal with databases.
MVVM specifies you must get rid of the horrible code-behind hacks abundant in some useless dinosaur UI frameworks such as winforms and use DataBinding instead.
Therefore, just go ahead with your existing infrastructure, and make sure you don't manipulate or create UI elements in procedural code.

I wanna develop a WPF application with XAML on my desktop and then convert it as a windows phone application. Can I do that?

I am new to this. But I wanna develop a DESKTOP APPLICATION using WPF, XAML and then convert it into a windows phone application. But i dont have a clear idea about it... Can you please help
To implement MVVM, you typically create both the model and the view
model in a Portable Class Library project, because a Portable Class
Library project cannot reference a non-portable project. The model and
view model can be in the same project or in separate projects. If you
use separate projects, add a reference from the view model project to
the model project.
After you compile the model and view model projects, you reference
those assemblies in the app that contains the view. If the view
interacts only with the view model, you only have to reference the
assembly that contains the view model.
Inspiring from above, I believe that the user interface in WPF and WP cannot be the same. XAML would change a little. So,since you haven't begun, the approach could be building the core (Classes,Business logic, Functionalities...) using Portable Class Library project. and two UIs : one for WPF and the other for WP.

LOB App with Silverlight, WCF RIA, MVVM and EF 4?

I develop in WPF with EF (EDM designer entities).
I now want to move to Silverlight+RIA, and I am looking for a turorial, video or whatever that will guide me how-to to use Silverlight RIA with MVVM using EF as the model.
I have extensive knowledge of: .NET 4, WPF, XAML, DPs, DataTempaltes, EF 4.
I am familiar with Silverlight, WCF RIA and MVVM.
There are two things about LOB+MVVM I am encountering difficulties:
I am looking for a way to template my work, so I don't have to copy-paste the content of my ViewModels, what I mean by that is having a generic ViewModelBase that will handle a certain type of Model(s):
ViewModelBase where TContext : DomainContext, TModel : Entity`
Another difficulty is the overall composition of the UI; say I have a branch of master detail that gets complicated more and more all in one screen. I want all the parts to be divided into tiny Views each for its point. The problem is, I don't know how to expose the data for the inner views, say the main view's DataContext is set with MainViewModel, and there is a property Contact in the MainViewModel; how would you set the Contact view inside the MainView, setting the inner view (the ContactView)'s DataContext to ContactViewModel AND setting the ContactViewModel's Contact to the current Contact from the MainViewModel, what is the right technique??
NOTE: I am self-employeed, no teams and not other developers, so I don't see a reason splitting everything into modules, I don't mind if the whole project will reside in one project split into folders etc.
I always find Mike Taulty's blogs useful for this sort of thing.
So try this followed by this.
And on the MVVM side of things I use the MVVM Light Toolkit, also try watching this very good video by Jason Dolinger

WPF / MVVM - Where do the ViewModels go?

I am kind of new to the whole MVVM pattern, and am trying to wrap my head around it. What I am currently trying to figure out is: in a well structured solution where do the ViewModels live?
Currently my design looks something like this (sort of):
Application (The view)
DomainSpecificCode (ClassLibrary)
Gateways (ClassLibrary)
If I were to add on another type of view (for instance ASP.NET or Silverlight), where would be the best place for the ViewModels to exist?
ViewModels should go in the Application layer because they tend to be technology-specific.
For example you may want to databind a View attribute to a particular color based on the state of the ViewModel. However, Color is implemented by different types on Windows Forms, ASP.NET and WPF, so you wouldn't be able to reuse the ViewModel accross different technologies.
If you add new Applications, you must also provide new ViewModels.
Recently, I built a MVVM Desktop application that had 2 flavors:
WPF Document Base GUI
Console application
Both exe were using the same view models, one was WPF and the other one was not.
I was able to split my solution into the following projects (libraries/exe):
non-project related re-usable code (called Common)
project models + persistence
project view models
WPF application + views
Console application
It was amazingly easy to build the console application version just by using the View Models. The console application code had less than 200 lines of code, and was basically loading the ProjectViewModel and doing operations on it.
This article describes a concrete Architecture for WPF MVVM Applications.
Layers:
Presentation Layer: Views
Application Layer: ViewModels
Domain Layer: Domain specific code

Resources