flexiblere Umsetzung des LendControllers durch Depedency-Injection

This commit is contained in:
Jonas Schönbach 2020-05-27 11:22:29 +02:00
parent d07c82f749
commit 1c8ec4b517
2 changed files with 14 additions and 3 deletions

View file

@ -13,13 +13,18 @@ namespace BuecherwurmAPI.Controllers
[ApiController]
public class LendController : ControllerBase
{
private readonly MockLendRepo _mockLendRepo = new MockLendRepo();
private ILendRepo _repository;
public LendController(ILendRepo repository)
{
_repository = repository;
}
//GET api/leihvorgang/
[HttpGet]
public ActionResult<IEnumerable<Lend>> GetAllLends()
{
var lends = _mockLendRepo.GetAllLends();
var lends = _repository.GetAllLends();
return Ok(lends);
}
@ -27,7 +32,7 @@ namespace BuecherwurmAPI.Controllers
[HttpGet("{id}")]
public ActionResult<Lend> GetLend(int id)
{
var lend = _mockLendRepo.GetLendById(id);
var lend = _repository.GetLendById(id);
return Ok(lend);
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BuecherwurmAPI.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
@ -26,6 +27,11 @@ namespace BuecherwurmAPI
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Adds a service that is created once per connection.
// It takes an interface and a specific implementation.
// That allows to swap the implementation easily.
services.AddScoped<ILendRepo, MockLendRepo>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.