Bei der Verarbeitung der Anfrage ist eine nicht behandelte Ausnahme aufgetreten.
InvalidOperationException: Der Dienst für den Typ „RESTfull.Infrastructure.Interfaces.IStudentRepository“ konnte beim Versuch, „RESTfull.API.Controllers.StudentController“ zu aktivieren, nicht aufgelöst werden.
Für Hilfe bei der Angabe, wo ich einen Fehler habe, wäre ich sehr dankbar!
Zu Bildungszwecken stelle ich Anträge auf Erfassung der Anwesenheit von Lehrveranstaltungen
Die Lösung besteht darin von drei Projekten API, Domäne, Infrastruktur.
Ich habe ein Datenmodell erstellt, die Migration konfiguriert, sie wird erfolgreich ausgeführt und erstellt vorerst leere Tabellen (dies ist über den SQL-Objektbrowser sichtbar)
Als nächstes I begann mit der Entwicklung von Repositories, Controllern und Schnittstellen.
Begonnen mit der Entität „Students“
Erstellte Dateien:
StudentRepository:
Code: Select all
using Microsoft.EntityFrameworkCore;
using RESTfull.Domain.Model;
using RESTfull.Infrastructure.Data;
using RESTfull.Infrastructure.Interfaces;
namespace RESTfull.Infrastructure.Repositories
{
public class StudentRepository : IStudentRepository
{
private readonly Context _context;
public StudentRepository(Context context)
{
_context = context;
}
public async Task GetAllStudentsAsync()
{
return await _context.Students.Include(s => s.Group).ToListAsync();
}
public async Task GetStudentByIdAsync(int studentId)
{
return await _context.Students.Include(s => s.Group).FirstOrDefaultAsync(s => s.StudentId == studentId);
}
public async Task AddStudentAsync(Student student)
{
_context.Students.Add(student);
await _context.SaveChangesAsync();
return student;
}
public async Task UpdateStudentAsync(Student student)
{
_context.Entry(student).State = EntityState.Modified;
await _context.SaveChangesAsync();
return student;
}
public async Task DeleteStudentAsync(int studentId)
{
var student = await _context.Students.FindAsync(studentId);
if (student == null)
{
return null;
}
_context.Students.Remove(student);
await _context.SaveChangesAsync();
return student;
}
}
}
Code: Select all
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RESTfull.Domain.Model;
using RESTfull.Infrastructure.Interfaces;
using RESTfull.Infrastructure.Repositories;
namespace RESTfull.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class StudentController : ControllerBase
{
private readonly IStudentRepository _studentRepository;
public StudentController(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
[HttpGet]
public async Task GetStudents()
{
return await _studentRepository.GetAllStudentsAsync();
}
[HttpGet("{id}")]
public async Task GetStudent(int id)
{
var student = await _studentRepository.GetStudentByIdAsync(id);
if (student == null)
{
return NotFound();
}
return student;
}
[HttpPost]
public async Task PostStudent(Student student)
{
await _studentRepository.AddStudentAsync(student);
return CreatedAtAction("GetStudent", new { id = student.StudentId }, student);
}
[HttpPut("{id}")]
public async Task PutStudent(int id, Student student)
{
if (id != student.StudentId)
{
return BadRequest();
}
await _studentRepository.UpdateStudentAsync(student);
return NoContent();
}
[HttpDelete("{id}")]
public async Task DeleteStudent(int id)
{
var student = await _studentRepository.DeleteStudentAsync(id);
if (student == null)
{
return NotFound();
}
return NoContent();
}
}
}
Code: Select all
using System.Collections.Generic;
using System.Threading.Tasks;
using RESTfull.Domain.Model;
namespace RESTfull.Infrastructure.Interfaces
{
public interface IStudentRepository
{
Task GetAllStudentsAsync();
Task GetStudentByIdAsync(int studentId);
Task AddStudentAsync(Student student);
Task UpdateStudentAsync(Student student);
Task DeleteStudentAsync(int studentId);
}
}