Click here to Skip to main content
15,887,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a base web API project which does the main operations and I have custom/bespoke web API project. The custom project does extra functions(bespoke) additional to the existing functions done by the base API.
How do I basically convert an API project to a plugin and access it from base API mainly at the controller level?

What I have tried:

I have tried to develop a sample project to experiment the proof of concept.
Here I have a base API project and custom API project in same solution.
In the base API: I have connected a simple Database to it through EF and created an interface with a add method to insert data to a table. I have implemented it a controller.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PluginTestProject.Interfaces
{
    interface IEmployee
    {
        bool AddEmployee();       
    }
}

I have a controller which implements this interface:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CustomeAPI.Controllers
{
    public class CustomEmployeeController : IEmployee
    {
        // GET: CustomEmployee
        public bool AddEmployee()
        {
            try
            {
                var employee = new tblEmployee();
                employee.firstName = "John";
                employee.lastName = "Snow";
                employee.address = "address test";

                using (var context = new PluginTestDBEntities())
                {
                    context.tblEmployee.Add(employee);
                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}

This all works fine. But what I need is below:
In the Custom API:
1. I need to convert it to a plugin and access it from the base project.
I should be able to tell which controller (base controller or custom controller) to look up.
Firstly, is it possible and if so please help me how to achieve it?

Please help as I am new to web API and plugins.
Thanks in advance!
Posted
Updated 12-Jul-17 5:41am
v2
Comments
F-ES Sitecore 12-Jul-17 4:43am    
Maybe look into dependency injection (google "web api dependency injection"). That should let you configure which controller is used, allowing you to substitute one for another. If you can't get the controllers themselves selectable under DI you might need to move the code that does the work into a service project and rather than doing the work in the controller you do it in the service and configure which service to use using DI.
Saranya Mahesh 12-Jul-17 11:44am    
I am Sorry. Just after going through about dependency injection in Google, I realised that I have framed my query wrong. Please see the updated question.
Ahmad F Hassan 8-Aug-17 5:58am    
in your base API, do you want to use an existing API as a plugin (chain calls)? or do you want to be able to create small APIs as a dll and reference it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900