در این آموزش به سرویسهای حرفهای برای روابط چند به چند در EF Core میپردازیم. نحوه پیادهسازی لایه سرویس در EF Core برای مدیریت روابط دانشجو-درس در Asp.Net Core را یاد میگیرید. این راهنما شامل عملیات CRUD، روابط چند به چند است.
در این آموزش، به بررسی سرویس StudentCourseService میپردازیم. این سرویس با استفاده از ابزارهایی مانند AutoMapper، فرآیندهای CRUD را ساده سازی میکند. سرویس StudentCourseService با ارائه متدهای استاندارد برای مدیریت روابط دانشجو-درس، توسعه را تسهیل میکند. با به کارگیری EF Core و AutoMapper، این سرویس هم عملکرد بهینه و هم خوانایی کد را تضمین میکند.
public interface IStudentCourseService
{
Task AddStudentToCourseAsync(int studentId, int courseId);
Task RemoveStudentFromCourseAsync(int studentId, int courseId);
Task<bool> IsStudentEnrolledAsync(int studentId, int courseId);
Task<List<CourseViewModel>> GetCoursesByStudentAsync(int studentId);
Task<List<StudentViewModel>> GetStudentsByCourseAsync(int courseId);
Task<List<StudentViewModel>> GetStudentsNotInCourseAsync(int courseId);
Task<List<CourseViewModel>> GetCoursesNotInStudentsAsync(int studentId);
}
public async Task AddStudentToCourseAsync(int studentId, int courseId)
{
bool exists = await _context.StudentCourses
.AnyAsync(sc => sc.StudentId == studentId && sc.CourseId == courseId);
if (!exists)
{
var student = await _context.students.FirstOrDefaultAsync(s => s.Id == studentId);
var course = await _context.courses.FirstOrDefaultAsync(c => c.Id == courseId);
if (student == null || course == null)
throw new InvalidOperationException("Student or Course not found.");
var studentCourse = new StudentCourse
{
StudentId = studentId,
CourseId = courseId,
Student = student,
Course = course
};
_context.StudentCourses.Add(studentCourse);
await _context.SaveChangesAsync();
}
}
public async Task RemoveStudentFromCourseAsync(int studentId, int courseId)
{
var relation = await _context.StudentCourses
.FirstOrDefaultAsync(sc => sc.StudentId == studentId && sc.CourseId == courseId);
if (relation != null)
{
_context.StudentCourses.Remove(relation);
await _context.SaveChangesAsync();
}
}
public async Task<bool> IsStudentEnrolledAsync(int studentId, int courseId)
{
return await _context.StudentCourses
.AnyAsync(sc => sc.StudentId == studentId && sc.CourseId == courseId);
}
public async Task<List<CourseViewModel>> GetCoursesByStudentAsync(int studentId)
{
return await _context.StudentCourses
.Where(sc => sc.StudentId == studentId)
.Select(sc => sc.Course)
.AsNoTracking()
.ProjectTo<CourseViewModel>(_mapper.ConfigurationProvider)
.ToListAsync();
}
public async Task<List<StudentViewModel>> GetStudentsByCourseAsync(int courseId)
{
return await _context.StudentCourses
.Where(sc => sc.CourseId == courseId)
.Select(sc => sc.Student)
.AsNoTracking()
.ProjectTo<StudentViewModel>(_mapper.ConfigurationProvider)
.ToListAsync();
}
public async Task<List<StudentViewModel>> GetStudentsNotInCourseAsync(int courseId)
{
return await _context.students
.Where(s => !(s.StudentCourses != null && s.StudentCourses.Any(sc => sc.CourseId == courseId)))
.AsNoTracking()
.ProjectTo<StudentViewModel>(_mapper.ConfigurationProvider)
.ToListAsync();
}
public async Task<List<CourseViewModel>> GetCoursesNotInStudentsAsync(int studentId)
{
return await _context.courses
.Where(s => !(s.StudentCourses != null && s.StudentCourses.Any(sc => sc.StudentId == studentId)))
.AsNoTracking()
.ProjectTo<CourseViewModel>(_mapper.ConfigurationProvider)
.ToListAsync();
}