2

I am creating a Asp.Net MVC Rest Api. There are two things that I have. One is I have a user and other is booking. Results are returned according to the user type. If User has access to city A means he can see bookings for city A only. I want to search bookings based on 3 parameters

  • City
  • Location
  • Date OR
  • BookingId Either top 3 or the bookingId alone + user parameters.

As the user object is dependent on the booking how should be my RestApi model Class designed.

Should it be like

public class Booking : User{}
OR
public class Booking{ public User user { get;set; } }

User Details are required for searching the bookings. User refers to the person who is logged in and using the system and searching the bookings.

RestApi method looks like the following below:

    [HttpPost]
    public HttpResponseMessage GetBookings(Booking booking)
    {}

I want to know the advantages and disadvantages of the approach and suggest me if there is any better way possible.

Based on discussions[edit]

How logical it will be If I accept another parameter for user in my api.Client who is calling my Api has to pass it as a parameter.Now code looks like this?

[HttpPost]
public HttpResponseMessage GetBookings(Booking booking,User user)
{}
7
  • 1
    I personally prefer second approach, because that explains the relation.
    – Bharadwaj
    Dec 19, 2017 at 11:03
  • 2
    The first one wouldn't even be correct. A Booking is not a subtype of User, at least it shouldn't be.
    – nbokmans
    Dec 19, 2017 at 11:04
  • 1
    I prefer one "User" has "Bookings" - not "Booking" has "User". Would theoretically work but not logical. Dec 19, 2017 at 11:05
  • 2
    Is a Booking a specific type of User? No, a Booking has an User Dec 19, 2017 at 11:05
  • 1
    If this notion is on WebAPI and not a Post Method in MVC... then POST verb/endpoint cannot contain more than one param? use "[FromBody] someViewModel"? Dec 19, 2017 at 11:53

2 Answers 2

5

Logically speaking, a Booking is not a User, we can't say that a booking is a user in every day life, so you can't use inheritance.

If you think about it, a Booking doesn't belong to a specific User and a User doesn't have specific Bookings. When thought about it like this, we can see that there isn't really a relationship between the two, all the users can see all the bookings, the only constraint is whether the user actually has the right to see all of them or just a part of them.

A more clean method would be to use the User's type and get all the Bookings depending on that (and some other criteria if needed, like city, location, etc..). So the way I would do it is to not make a relationship between the two classes and just work with the real relationship, since every User can only see Bookings specified by its type.

1
  • Not that your answer is incorrect but logically speaking an Employee is a Person but you would not necessarily use inheritance unless it makes sense within the domain. In some domains it makes sense while in other domains it would be a big mistake. In other words you cannot always model based on real world. Dec 19, 2017 at 11:37
2

Rather than going into the theoretical element, in which @Haitam has done a wonderful job. I'll focus on implementation specifics

Now rightly or wrongly, I'd architect the logic in the manner of:

//Lets seal the class to prevent inheritance unless you want to conform to O/C SOLID Principle
public sealed class User
{
    public int Id {get;set;}
    public IEnumerable<Booking> Bookings {get;set;}
}

Now on the assumption that the User could possibly contain "Bookings"

//Lets take it up a notch and leave behind implementation specifics and fully encapsulate
//With DI/IoC in mind
private IBookingService _bookingService;
public SomeController(IBookingService bookingService)
{
    _bookingService = bookingService;
}

[HttpGet]
public async Task<HttpResponseMessage> GetUserBookingsAsync(int userId)
{
   return await _bookingService.GetUserBookingsAsync(userId);
}

Apart from the odd construct of Booking has-a User, I'd prefer to make it a little more logical and also avoid inheritance because there is no relationship, in a pragmatic sense.

Let User be User with optional Bookings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.