Karthik
Karthik

Reputation: 127

ASP.NET MVC - Service Layer - Business Layer - Data Layer (EF) - SQL DB :: Data Transfer?

I am planning to create 3 tier application using ASP.NET MVC as UI tier, WCF as Business tier and SQL DB as the database.

My Business tier would split as Service Layer (WCF), Business Layer (Business logic, Business Model), Data Layer (Entity Framework - DB first).

Although the above mentioned approach is common I would still like to get it confirmed if this is a good design ?

My real question is about the data transfer between the layers ( UI (MVC) - Service (WCF) - Business - Data layer)

Lets say I have a GET operation initiated to retrieve Account and its Traits from database. In database its represented as two different tables.

Within Datalayer I want to write a method to get these two tables records for a given
AccountNumber using EF.

My Business Model has a two classes called Account and AccountTraits. The AccountTraits's object collection is aggregated into Account class.

like,

public class Account
{
    string AccountNumber;
    List<AccountTraits> Traits;
}

Now, If I want to populate these Domain objects with its data I can use the EF query something like the below in my data layer.

public IEnumerable<Account> GetAccountAndItsTraits(string AccountNumber)
{
var query = from a in db.Accounts
            select new Accounts() {
            AccountName = a.AccountName,
            Traits = from t in a.AccountTraits
            ....

return query;
}
  1. Is this the rite approach to populate the Business model POCO from Data layer (EF) ?

  2. Now, after applying some business logic I would like to return these collection to the UI. How do I transfer these to the Service layer and how do I give it to the UI Model ?

  3. Should I have to define DataContract in WCF with exact class definition as Business model and then "copy the data" to it and give it to the UI ? Then the UI should get the data from the WCF proxy objects and "copy to its Presentation Model" (which is again should have its own Accounts and AccountTraits - may be with some extra fields)?

It would be great if you can throw some lights on these topics.

Thanks!

Upvotes: 5

Views: 3824

Answers (1)

AaronLS
AaronLS

Reputation: 38367

I would absolutely not use WCF purely on the basis of architecture, unless you specifically needed features it provided. I.e. certain transports/protocals to support certain business to business needs. MVC has WebAPI now which is basically same stuff migrated from WCF and made much easier to use, and might meet your architectural needs with much less headache.

Additionally, I would not even use WebAPI unless I was exposing a something that I know for sure will be used outside of my application. If this service layer will only be used by this MVC application, then I would do away with it. You can still have a business layer+data layer and maintain separation of concerns without it. That's just my own opinion.

Usually I make use of View Models, which are models tailored for the view. They allow me to refactor either the View or the database independently and only have to change the data transformation code. I.e. database field changed might only require a tweak in the query, and the ViewModel stays the same, thus all views using that view model stay the same. Pretty much the same way you are querying and transforming the entities from the query into a new Account except I name mine AccountVM for view model(the model specifically for the view)

I personally call these types of data query/transformation methods from my controller. The controller then just passes the AccountVM or collection to the View(vm) where vm is the name of the variable holding the AccountVM or collection.

This is how I get the data from DB to View. The EF entity classes and VM classes are usually pretty similar anyhow, and sometimes identical. I feel like a middle business entity class would be too similar to one or the other to really warrant the extra class and extra copying.

So to answer a couple of your questions in my opinion: 1 Yes, basically I think this is a good approach.

2 This GetAccountAndItsTraits is called from the Controller and the View is based on your business poco(or what I call ViewModel). So the return from the business layer matches what your model expects. I wouldn't have a service layer here. However, I might instead have some methods dedicated purely to converting EF entities to View Models, which would happen after the business model function is called. In other words, the business model would return EF entities, and then mapping functions would map those to whatever view model is needed in that case, since you could have lots of View Models and Views that utilize that same business method. I use EF Code First, so my entity classes are already pretty close to being POCOs, so it doesn't offend me for them to leak into other layers.

Upvotes: 3

Related Questions