-1

I am new to asp.net API and started figuring out things out of online tutorials and to Stack Overflow as well.

I am into a case where I need to build An API for Member_Product table where the transaction of memberships and other products purchase is registered and in order to extract members with a certain kind of product I have to have engage another table called Product_type.

What I know is you can use one model for a single controller to build API around it. I am still confused about that as I have to use more than on in my case.

Please what is the best practice about that and how to properly implement it and if there is anything I have to read and I miss it out, it's kind of you to provide link about it.

I have used an empty controller and used both models inside with a single LINQ query. Below the code int the member controller:

private readonly dboFFEntities FitnessDbo = new dboFFEntities();
[HttpGet]
[Route("Members/activeMembers/")]
public int GetLiveMembers()
{
  using (FitnessDbo)
    {
        var LiveMemebersCount = (from mp in FitnessDbo.memberproductinfoes
                                         join pt in FitnessDbo.product_type   on mp.mepi_prodtype equals pt.prty_typeid.ToString()
                                         where pt.prty_name == "MEMBERSHIP" &&
                                         (mp.mepi_status == "1" || mp.mepi_status == "3" || mp.mepi_status == "6")
                                         select mp.mepi_memberid).Distinct().Count();
                return LiveMemebersCount;
            }
        }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ahmad
  • 23
  • 7

1 Answers1

0

The answer is DTO.

An API controller is not bound to a DB model or any other model. It represents one entity. There is no restriction/convention on how many tables a controller should access as long as the API design is good.

The Member_Product seems to be a Junction/Pivot table.

IMO you don't need a separate API controller for junction tables.

You can create an endpoint in the Members controller to return products for passed members.

For e.g.

  • api/products - Returns all the products in the DB (You may not want this)

  • api/products/{productId} - Returns single product with passed id.

  • api/members - Returns all the members (without products)

  • api/members/{memberId}/products - Return all the products for the passed member.

Ideally, you should not return DB models in response. Create DTOs that contains all the processed information required by the client.

For e.g. Product DTO should only contain required info about the Product it should not contain information that is for internal use and will not be used by the client. Member DTO should only contain member info. The DTO for Member_Product (api/members/{memberId}/products) endpoint should contain all the info about products + member.

There are many articles on this problem refer

Kishan Vaishnav
  • 2,273
  • 1
  • 17
  • 45
  • I have created my REStful API the way you have specified here, still going through the MVVM concept and you have enlightened me about DTO. thanks a lot for the valuable information. – ahmad Oct 30 '19 at 14:03