I'm just a starter and asking a simple question.. Just need a direction where to move.
Can someone explain in very few words the process of application development using WPF+MVVM pattern (using Catel) with database created with Entity Framework.
1. I create models (Code First) and generate database
2. I create View models via Catel base classes
3. I create Views with WPF
The question is: how to connect database with Catel View Models? Where to load DBContext and entities? Should I create repositories? Or maybe it's needed to create separate Models?
Is there a common way or maybe example of a small WPF application which uses database to store data?
Sorry if my question is slightly heretic - simply can't sort all the terms and approaches into a single system which I will follow during app creation...
Though there are technical discussions in the field what is best way, there are two commonly used scenarios:
1) Using repositories (Catel does support implementations of repositories for both EF5 and EF6)
2) Use a service that will handle the functions and call that in the view model like any other service.
Related
I am using Visual Studio 2015 with Entity Framework 6 and .NET 4.5.2 for a WinForms application. I am currently working on adding reports to my project. I thought about adding the classes which return DTO collections intended for reports to my DataServices section. I decided not to because all classes in my DataServices section currently use my DAL to provide CRUD operations through repositories. My classes for reports will only return read only DTO collections. Since my report DTO classes will not use the typical repository methods (i.e. Add, Update, Delete...) I decided to create a new folder in my project called ReportServices. I have therefore placed my report class named RequestReports (this is for my Request model entity and will return a RequestDetailDto collection) in that folder. Below is a picture of how I have things setup now:
I am not sure if the way I have it setup is the right thing to do. One other way might be to just scrap the idea of a reporting section and mix the reporting classes in with the other data services which use repositories. If I do this, then I would simply not implement the repository methods of add, update, delete for the report classes. I would only implement the get operations for the reporting classes of FindByID and FindAll. Is this really the way to go?
So my question is, "Where should the classes which retrieve information for reporting purposes be placed in a Visual Studio project of a layered design?"
Thanks in advance.
I recommend creating a separate project altogether for reporting. Keep the reporting model and data access separated from your main domain, don't use the same Repositories. Different purposes means different efficient ways of fetching data. It will also pave the way for the option of storing reporting data in a distinct database.
You might want to check out the CQRS approach, it brings a lot to the table on these issues.
Im very newbie to the .net world... I'm going across some tutorials for MVC, where in I could see all the tutorials are writing the Model classes then creating the database(may be used for tutorial sake). But I have the question for the real projects we dont generate the Models and then create database.. rather we do generate the Models according to the database.
So after so much googling and reading different blogs I came to know that we can use ADO.Net Entity Data Model to generate the DataContext. Also I came to know that we need not write the models manually because Entity Data Model generates all the Model classes according to the database.
Can anyone tell me that for real world projects do we use the ADO.Net Entity Data Model to generate the classes and we dont need to write any Model classes on our own.
Please correct if Im wrong...
Thanks in advance
Exactly. You can extract model classes out of your existing database.
For more on this subject, take a look at this blog post:
EF 4.2 Model & Database First Walkthrough
This is the important part in the post linked above:
"... if you are mapping to an existing database you would now select
‘Generate from database’, follow the prompts and then skip to step 4.
"
This is the article that'll show you a complete MVC app created using the Database First approach:
Building an MVC 3 App with Database First and Entity Framework 4.1
I'm confused about the architectures which we can use to develop a business application with WPF 4.0 and EF 4.0 technologies.
My first choice was a traditional N-tier architecture contains: UI, Business Logic Layer & Data Access Layer with a disconnected behavior.
In this way I create 3 project for each layer and another project for my Entities/DTOs (Each layer is an assembly). Each layer references only to it's upper and lower layers (That is: UI can see the BLL but can't see the DAL). But all layers have access to the Entity/DTOs assembly for communication purposes.
The problem starts when I want to create a simple CRUD form with a DataGrid for example. The BLL disposes the DataContext of the DAL when returns an Entity/DTO, this is the reason that forced me to use STEs. But yet there are several problems. For example I should call "StartTracking" method for each entity returned from BLL to the UI. In short, I don't sure about this pattern reliability or I think I have to forget about automatic handled CRUD forms.
I use the repository model in my DAL layer but when I search about the repository pattern I find it different. It seems that it's not bad to reference to both of the DAL/Repository and the BLL/Services(Not WCF nor WebServices) layers from the UI and thus we can have a connected environment (Without using STEs).
I see an example in which we can get a person from repository but do something on it using BLL or services:
UI CODE:
var person = new PersonRepository().GetPerson(10);
Bll.Salary.PaySalary(person);
-or-
var person = new PersonRepository().GetPerson(10);
Bll.Person.MarkAsAbsent(person);
Or something like that...
With this pattern we can send the Entities/DTOs to the UI in a connected way while the DataContext is alive.
I don't know if I understand the way of using the repository pattern in big projects. I think it's not clear to naming the BLL or services classes and methods in this way. More over the developers might be confused about where to use the repository methods or BLL/service methods or about where to create the methods (in repositories or BLL/service).
I prefer the N-Tier architecture using a good approach to track the Entities/DTOs changes automatically like STEs.
Would you please recommend the best pattern in such situations or/and reference me to some good books or documents about that.
I put together a sample app that may help with some of your questions. You can review the presentation notes and the sample via my blog post here:
http://blog.alner.net/archive/0001/01/01/wpf_ef_4_sig_presentation_2010.aspx
The sample shows using STEs and includes some helpers to make the Entity Framework STEs work better in a desktop client app.
Repositories are there to hide the details of how you get the data. The idea is that you could swap the implementation of a repository from one that uses a local database, to one that uses a remote web service without the upper layers knowing about it.
Maybe the article Architecture for WPF applications is any help for you.
You might have a look at the BookLibrary sample application of the WPF Application Framework (WAF) as well. It shows a WPF MVVM application together with the Entity Framework applying the described architecture.
Is there a way, given a database or some other source specification for an application, for an entire WPF or Silverlight / XAML application to be generated that emits best practices?
For example, assuming I have a well-normalized database (or some other kind of base specification for an app), is there a tool that can create a well-formed MVVM application. This would include...
XAML (Views) for all of the tables (list and edit) with no code-behind
ViewModels that emits properties extracted from the db schema along with commands or behaviors for all of your standard CRUD operations
Models built with the repository pattern (or some other db abstraction) with interfaces that emit the table design and relationships and a default implementation of your choice (sql, sql ce...whatever).
An ideal tool like this would only do a "first pass" of the application after which the developer would make all necessary changes and modifications.
I don't think there is any such tool but it seems like a tool like this should be possible.
Another related question, in the absence of such a tool (or even if there is one), what tool(s) are best used to get me from a database design (or some other kind of base specification for an app) to working app.
What STEP-BY-STEP process do you take to get from design to basic working application in a couple of hours?
I am just trying to figure all of these practices and patterns and I am getting a pretty good grasp of the pieces but not sure what workflow to use to make it all work together quickly but still allows unit testing of separate concerns.
Thanks in advance.
Seth
Not sure if this is what you are looking for, but you can use LightSwitch
From a process perspective, I build and test my ViewModels first. That allows a division of labor between designing and wiring up the UI and pulling data into the ViewModel.
I am developing an application in WPF using the MVVM pattern. If I have Student as a data model, for example, where could I add the business logic like adding new Students, deleting Students and updating Student info, calculating marks and so on?
Does all the business logics come under the model or the viewmodel?
If it is in the model how can we do that? Any example code please? I need full code for an example!
Business logic should be part of of your model, as all of that code should be reusable across different technology platforms. For example, you may want to reuse your models across a web site in ASP.NET MVC, and a desktop application in WPF, so it wouldn't make sense to have business logic in your view models.
View models should contain presentation logic which is specific to that particular application instance.
As for example code, this will be specific to the domain that you are modelling, so you would have to ask a more specific question about what you are trying to model.
I suggest you read PRISM documentation on this subject: Chapter 5: Implementing the MVVM Pattern.
Even if you don't plan to use PRISM, the article is very well written and details the pattern in Microsoft's technology terms.
I would trust very little of what Microsoft has to say on Patterns as their articles are often dated, or modified to support their own proprietary frameworks or products. Also I've found MS thoughts/approaches contradictory within their different teams and also within the generally accepted approach in software.
Not all of it is good and not all of it is bad basically.
To answer your question. I'd use a Service for Business Logic. I'd instantiate a reference to it in the ViewModel and I would not put any logic in either the ViewModel or the Model that did not have anything to do with their role. ie Presentation logic is okay dependingon what it is, it might be best placed in the ViewModel as it is highly tied to the View in question. Same goes for the model.
Keeping in mind these roles you will have a scaleable and testable application.