Search code examples
wpfentity-frameworkarchitecturebusiness-application

What is the best architecture for a business application using WPF & EF?


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.


Solution

  • 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.