Mark Erasmus
Mark Erasmus

Reputation: 2405

How do you separate views, models and controllers into separate projects when using WCF?

Background

I have a project employing n-tier architecture that exposes the business and data tiers using a service facade (WCF) to the presentation layer (ASP.NET MVC). The presentation layer is separated into concerns using separate class libraries i.e.,

Controllers call respective repositories which in turn call the service layer. The service layer uses data transfer objects (DTO) to facilitate the transport of business objects in a serialized format (i.e. flattened business objects) over the wire.

Problem

In the presentation layer I need to map the DTOs to their respective viewmodel objects. This works fine when I have one MVC project containing all views, viewmodels, controllers and repositories. But I want to maintain the class library separation. This introduces a problem because the service reference is added to only one of those projects (Repositories). i.e. the other projects won't recognize the DTO types that are defined in the service reference contract because the service contract won't be added.

For example, the ViewModels project won't be able to map the DTOs to ViewModels because the DTO data types won't be recognised. How would I be able to maintain this separation in the presentation layer if only the datatypes were accessible via the service contract?

Upvotes: 1

Views: 925

Answers (3)

Mark Erasmus
Mark Erasmus

Reputation: 2405

I've basically kept the same architecture and kept the service reference added to the Repositories project. Instead of having a extension methods helper class that handles DTO to ViewModel mapping in the ViewModels project I have added this to the Repositories project since this is the only class that needs to be aware of DTO data types. Seemed like a decent trade off.

Upvotes: 0

Lamloumi Afif
Lamloumi Afif

Reputation: 9081

In the ASP.NET MVC architecture, the Model(M) is not exactly the business layer , it seems like View Model :

Exemple

these class may be in the business Layer :

public class Student
{
}

public class Course
{
}

Sometimes we need to combine between the model class so , in the Model (in asp.net MVC application) we can find this class :

public class Education 
{
List<Etudiant> etudiants = new List<Etudiant>();
List<Course> courses = new List<Course>();
}

It's a good practise to maintain the separation between the model part ( BLL layer) from the UI Layer ( asp.net MVC application for example)

Upvotes: 0

Batavia
Batavia

Reputation: 2497

the easy/best solution seems make the DTO's a separate project (a class library) and reference those from both your service layer and presentation layer.

update: that said if they always get their data having a reference to your service layer doesn't seem that bad (still not ideal of course) if they always get their data through the service layer (a.k.a they are already dependent on your service layer regardless if they have a service reference link)

Upvotes: 1

Related Questions