Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC service layer input output data

I'm following the repository pattern with service layers in my project. For each view I'm going to create a viewmodel.

What I'm confused is that, should the service layer directly access domain objects and returns them to the controller, or should I use DTOs. If I should use DTOs, where to put them in the project architecture?

Thank you.

like image 633
SherleyDev Avatar asked Nov 01 '13 23:11

SherleyDev


People also ask

Should service layer return entity or DTO?

The service layer only accepts data in the form of DTOs. Any data returned to the controller layer will be in the form of DTOs.

Should you always use dto?

Should we always use DTOs for communication with service layer? Yes, you have to return DTO by your service layer as you have talk to your repository in service layer with domain model members and map them to DTO and return to the MVC controller and vice versa.

How fetch data from database and display in table in ASP NET MVC?

From the Add New Item window, select ADO.NET Entity Data Model and set its Name as NorthwindModel and then click Add. Then the Entity Data Model Wizard will open up where you need to select EF Designer database option. Now the wizard will ask you to connect and configure the Connection String to the database.

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.


1 Answers

Service layer is responsible for mapping (converting) Dto objects and Domain objects by implementing proper business logic.

Your DTO objects should be used in controllers and services.

DTO's are transfered between Controller and Service, on the other hand Domain objects are transfered between Service and Repository

Controller does not know about Domain and Repository does not know about DTO. Service knows both DTO and Domain and converts them each other with business rules like a car between driver and road, like stackoverflow between you and me, like everything, abstraction...

Following code is an example. Consider each namespace is a package.

namespace Controllers
{
    using Services;
    using DataTransferObjects;

    public class CoffeeController
    {
        public ICoffeeService CoffeeService { get; set; }

        public JsonResult GetCoffee(GetCoffeeInDto inDto)
        {
            var result = CoffeeService.GetCoffee(inDto);
            return JsonResult(result);
        }

        public JsonResult SaveCoffee(SaveCoffeeInDto inDto)
        {
            var outDto = CoffeeService.SaveCoffee(inDto);
            return JsonResult(outDto);
        }
    }
}

namespace Services
{
    using DataTransferObjects;
    public interface ICoffeeService
    {
        GetCoffeeOutDto GetCoffee(GetCoffeeInDto inDto);
        SaveCoffeeOutDto SaveCoffee(SaveCoffeeInDto inDto);
    }
}

namespace Services.Impl
{
    using Services;
    using Repository;
    using DataTransferObjects;
    using Domain;

    public class CoffeeService : ICoffeeService
    {
        public ICoffeeRepository CoffeeRepository { get; set; }
        public GetCoffeeOutDto GetCoffee(GetCoffeeInDto inDto)
        {
            var entity = CoffeeRepository.Get(inDto.Id);
            return new GetCoffeeOutDto {Id = entity.Id, Name = entity.Name};
        }

        public SaveCoffeeOutDto SaveCoffee(SaveCoffeeInDto inDto)
        {
            var entity = new CoffeeEntity {Name = inDto.Name};
            CoffeeRepository.Save(entity);
            return new SaveCoffeeOutDto {Id = entity.Id};
        }
    }
}

namespace Repository
{
    using Domain;
    public interface ICoffeeRepository
    {
        CoffeeEntity Get(int id);
        void Save(CoffeeEntity coffeeEntity);
    }
}

namespace Repository.Impl
{
    using Repository;
    using Domain;

    public class CoffeeRepository:ICoffeeRepository
    {
        public CoffeeEntity Get(int id)
        {
            //get entity from db
            throw new System.NotImplementedException();
        }

        public void Save(CoffeeEntity coffeeEntity)
        {
            //insert entity into db
            throw new System.NotImplementedException();
        }
    }
}

namespace DataTransferObjects
{
    public class SaveCoffeeInDto
    {
        public string Name { get; set; }
    }

    public class SaveCoffeeOutDto
    {
        public int Id { get; set; }
    }

    public class GetCoffeeInDto
    {
        public int Id { get; set; }
    }

    public class GetCoffeeOutDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

namespace Domain
{
    public class CoffeeEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
like image 172
mehmet mecek Avatar answered Oct 11 '22 10:10

mehmet mecek