Next, we will add a controller, which will derive from our custom BaseController class.
The following is the code of ServiceRequestController, which contains an HTTP POST method to save the service requests of the tenant:
[LoggingActionFilter]
[Route("api/[controller]")]
public class ServiceRequestController : BaseController
{
IServiceRequestManager _manager;
ILogger<ServiceRequestController> _logger;
public ServiceRequestController(IServiceRequestManager
manager, ILogger<ServiceRequestController> logger) :
base(manager, logger)
{
_manager = manager;
_logger = logger;
}
[HttpGet]
public IEnumerable<TenantServiceRequest> GetTenantsRequests()
{
return _manager.GetAllTenantServiceRequests();
}
[TransactionActionFilter()]
[HttpPost]
public void Post(ServiceRequest serviceRequest)
{
try
{
_manager.Create(serviceRequest);
}
catch (Exception ex)
{
throw LogException(ex);
}
}
}
Given next is our Startup class, which registers all the dependencies in the ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<IDbFactory, DbFactory>();
services.AddScoped<DataContext>();
services.AddScoped<IRepository, Repository>();
services.AddScoped<IServiceRequestManager,
ServiceRequestManager>();
services.AddScoped<ITenantManager, TenantManager>();
services.AddScoped<BusinessManagerFactory>();
// Add framework services.
services.AddMvc();
}
And here is the Configure method:
public void Configure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Information);
loggerFactory.AddDebug();
app.UseMvc();
}