How to transfer data between different layers in a project?

I am working on an ASP.NET MVC project that uses an existing database and hence I am using EF DB First for that. I have created three different projects, one for UI, one for business layer and one for data access layer.

I have created my EF edmx file in dataaccess layer using EF DB First so, all the models are created automatically in the DAL Project based on the table structure. I am using Repository and UnitOfWork Pattern to allow access to data from the service layer. I created interfaces in the Data Access Layer itself. Now, I am passing the models to the service layer whenever a method in DAL is called from the service layer and then creating a viewmodel in the service layer by projecting the received data using select (x => new Viewmodel{ subject = x.Subject} And I am passing this viewmodel to the request made by a controller action. Even the view uses this Viewmodel that I created in Business layer.

However, recently after studying articles I realized that I am doing it wrong. That, I must be using DTO to transfer data between layers. And that I must have business entities in Business Layer, View model in UI Layer and Models in DAL. In which case, my Viewmodel for a page will determine its contents. Business entities will be the business poco classes and will contain properties only related to a particular entity. And models can contain the columns from the table structure.

However, what will the DTO properties be? And will I be using DTO in all three layers to pass data? Also, I learned that I must create interfaces in Business Layer and not DAL or Infrastructure layer for things like Logging, Exception Handling etc, if I will do that, then will I be required to use IoC Container by Default? Otherwise, How do I reference the interfaces in DAL and then call the DAL methods in Service Layer.

Where does Automapper fits into this?

submitted by /u/wannabeIndLeader
[link] [comments]

Leave a Reply