简体   繁体   中英

How to expose 3 endpoints in C# Webservices?

I want to expose 3 endpoints("Add to coin", "Get total amount of coins", "Reset coins"), but i am struggling if this implementation should be done on the controller side because i have an Interface, need some help and this is my logic so far.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoinJarAPI.Interface
{
    interface ICoinJar
    {
        void AddCoin(ICoin coin);
        decimal GetTotalAmount();
        void Reset();
    }

    public interface ICoin
    {
        decimal Amount { get; set; }
        decimal Volume { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using CoinJarAPI.Models;

namespace CoinJarAPI.Controllers
{
    public class CoinJarController : ApiController
    {
        // GET: api/CoinJar
        public IEnumerable<CoinJarModel> Get()
        {
            var coinJarList = new List<CoinJarModel>();
            for (int i = 0; i < 10; i++)
            {
                var coinjarModel = new CoinJarModel
                {
                    // volume, Amount, GetTotalAmount.
                };
            }
            return coinJarList;
        }

        // GET: api/CoinJar/5
        public string Get(int id)
        {
            return "value";
        }

    }
}

Depends on what you want to follow. If your implementation is pure 100% rest then:

CoinJarController endpoints

You should define the following:

  1. Get which would coincide with // GET: api/CoinJar
  2. Get with id which would coincide with // GET: api/CoinJar/5
  3. Post, which would coincide with // POST: api/CoinJar/ and it should be used to insert new CoinJar items
  4. Put, which would coincide with // PUT: api/CoinJar/ and it should be used to update CoinJar items
  5. Delete, which would coincide with // DELETE: api/CoinJar/ and it should be used to delete CoinJar items

Now, depending on wether or not a coin should be considered a resource on it's own, then you could declare the following

  1. GetCoin which would be the // GET: api/CoinJar/{id}/Coin/
  2. GetCoin which would be the // GET: api/CoinJar/{id}/Coin/{CoinId}
  3. PostCoin which would be the // POST: api/CoinJar/{id}/Coin/{CoinId} which would insert a new coin in the CoinJar etc

You can also use the PUT version of the CoinJar to just update (AddCoin/Reset etc) the coins in one go. It all depends on your needs.

Controllers for just interfacing

All your logic and interfaces, should be used on another layer, the business one. This way you can implement your interfaces and use them accordingly from your controllers or any other kind of client.

Check these excellent api guidelines from Microsoft for more design information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM