سؤال

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.

هل كانت مفيدة؟

المحلول

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; }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top