Service level returning only models

Should the service layer return only model objects? There are a few posts about this on the Internet ( here and here are some SO posts), but none of them have a good example.

Everything I see looks something like this:

Services should only care about the problem domain, not what gives the results. Return values โ€‹โ€‹must be expressed in terms of domain objects, not representations.

I feel that something is missing here.

Look at the example below ... Suppose I want to return a list of all the films, but I need a flag boolean- something like hasLike- to show if I liked it before. How is it possible to return only models from the service level?

In short ... how can I return meta informationfrom the service level after this approach? Is it possible?

Model

public class Person
{
    public int                     PersonID        { get; set; }
    public string                  Name            { get; set; }
    public ICollection<Movie>      FavoriteMovies  { get; set; }
    public ICollection<MovieLikes> Likes           { get; set; }
}

public class Movie
{
    public int      MovieID     { get; set; }
    public string   Name        { get; set; }
    public string   Description { get; set; }
}

public class MovieLike
{
    public int              MovieLikeID      { get; set; }
    public int              PersonID         { get; set; }
    public int              MovieID          { get; set; }
    public DateTimeOffset   Date             { get; set; }
}

Service

public class MovieService : IMovieService
{
    public Movie Get(int id)
    {

    }

    public Movie GetByName(string name)
    {

    }

    public IEnumerable<Movie> GetAll()
    {
        return unit.DbSet<Movie>();
    }
}
+4
source share
1 answer

You can divide your service into requests and commands.

Query operations return a smooth, denormalized representation of your data that is easily consumed by your customers. Command operations accept commands containing only the information necessary to execute the command.

, , , , . , , HasLike, -, ( -). , , ; HasLikes true false , .

, , (Person, Movie, MovieLike), , / ; , . :

 public IEnumerable<MovieSummary> GetAll()
 {
     return unit.DbSet<Movie>();
 }

 // All properties needed in summarized representation
 public class MovieSummary
 {
     public int MovieID { get; set; }
     public string   Name { get; set; }
     public string   Description { get; set; }
     public bool HasLike { get; set; }
     // Other calculated properties
 }

...

 public MovieDetails Get(int id)
 {

 }

 // All properties needed in detailled representation
 public class MovieDetails
 {
     public int MovieID { get; set; }
     public string   Name { get; set; }
     public string   Description { get; set; }
     public ICollection<MovieLikes> Likes { get; set; }
     // more
 } 

, , .

+1

Source: https://habr.com/ru/post/1534512/


All Articles