cAMPy cAMPy - 2 months ago 6x
C# Question

Do I need WCF layer in the backend layer of my WPF application?

I am creating my first stand alone desktop WPF application using Entity Framework. Do I need a WCF layer to access database? Is this a bad practise if I just call DBContext directly from ViewModels?



The short answer is: it depends!

The long answer

It depends on your use case you need to implement. If you need to add another layer of abstraction -the WCF layer- to hide your OR/M away you can do it. However if your strategy is easy enough like a standalone WPF application I wouldn't bother making a WCF layer. You can simply access the IDBContext in your application, but keep in mind to not tightly couple your viewmodels with EF.

Always worth try keeping the concerns separate!

And these concerns are:

  • Data- or Persistence (EF) Models that are used to map your database to your OO models
  • ViewModels that are supporting your Views with data to show
  • Mapping of your Persistence and ViewModels

This way you can achieve a lightweight setup that aims for better separation and better testing ability.

Further Extensibility

Later on your development path, when you arrive at a point where you need to add an infrastructural concern like a WCF layer which could serve as a public API -or an entry point- to your shared database access, you can easily add it as a new project, put its classes behind interfaces (these are the only ones you will have as reference added to your WPF project) and let this project have the exact implementations.