0

I have a code that has 2 APIs for the user:

  1. Search API - returns 'shallow movies' results that each one has only few fields (e.g. id, title, subtitle and icon image).

  2. Drill down a specific result - The user sends an id of one of the results he got in the "Search API", then the program will fetch many more data about this result (from DB and other sources) and return 'detailed movies' result that includes both the data from the shallow model and the new data.

Because I don't want to fetch all the shallow result data again I am saving all the shallow results in cache and then in the drill down API, I fetch from cache and find the result that matched the id the user sent.

On the shallow result model, I have many fields (except what I wrote above) that shouldn't be returned in any of the API's and are used only for logs and some other uses. My problem is that I'm not sure what is the best way to model the shallow result model. I thought I can have the following Movie class that contains 2 inner classes:

  • Movie
    • MovieCache
    • MovieExtraData (all the fields that shouldn't be saved to cache)

It feels a little weird to me because in this way the Movie model becomes very specific to this flow and I may use it in many other flows too.

Sorry for the length, any suggestions? Thanks!

1 Answers1

0

As I understood from your description, MovieCache should not be exposed through API to users, however you want MovieExtraData to be shown to users through API.

If it is true, you can have two classes:

  • Movie will be used for interaction in data layer
  • MovieDto will be used for interaction with user. This class will have all desired properties to be shown for API

Data Transfer Object (dto) is a design pattern to transfer data between layers in an N-Tier application.

Read more about dto here in wiki

UPDATE

If your loggable class movie does not have the same behaviour with MovieSpecific, then you can create a separate class for logging.

public class Movie
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string SubTitle { get; set; }
}

public class MovieSpecific extends Movie
{
    public string AnotherSpecificData { get; set; }
}

public class MovieLogging extends Movie
{
    public string AnotherLoggingData { get; set; }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • @AviadBenShabat so just create `MovieDto` with these properties. This is an object which will store data that you need – StepUp Jul 05 '22 at 07:20
  • I'll try to be more clear, The user have 2 APIS: 1. getAllMovies - returns a list of objects describe a movie with id, title and subtitle. This api also store this list to cache. 2. getSpecificMovie(id) - fetch the list of movies from cache, search the movie that has the given id, enrich it with more data from DB and return it to the user. My question is which classes should I have, consider that I have more data that I collect in the first API (getAllMovies) for logs - where should I hold it if I dont want it to be saved to cache or be exposed through the api (only for logs). – Aviad Ben Shabat Jul 05 '22 at 07:27
  • @AviadBenShabat please, see my updated answer – StepUp Jul 05 '22 at 08:14