有两个文件:一个给出接口如下:
IStudentInterface.cs
public interface IStudentService
{
IEnumerable<Student> GetStudents();
Student GetStudentById(int id);
void CreateStudent(Student student);
void UpdateStudent(Student student);
void DeleteStudent(int id);
void SaveStudent();
}
学生服务.cs:
public class StudentService : IStudentService
{
private readonly IStudentRepository _studentRepository;
private readonly IUnitOfWork _unitOfWork;
public StudentService(IStudentRepository studentRepository, IUnitOfWork unitOfWork)
{
this._studentRepository = studentRepository;
this._unitOfWork = unitOfWork;
}
#region IStudentService Members
public IEnumerable<Student> GetStudents()
{
var students = _studentRepository.GetAll();
return students;
}
public Student GetStudentById(int id)
{
var student = _studentRepository.GetById(id);
return student;
}
public void CreateStudent(Student student)
{
_studentRepository.Add(student);
_unitOfWork.Commit();
}
public void DeleteStudent(int id)
{
var student = _studentRepository.GetById(id);
_studentRepository.Delete(student);
_unitOfWork.Commit();
}
public void UpdateStudent(Student student)
{
_studentRepository.Update(student);
_unitOfWork.Commit();
}
public void SaveStudent()
{
_unitOfWork.Commit();
}
#endregion
}
请解释一下为什么要_studentRepository
创建私有变量?我们也可以代替它来完成我们的任务。为什么所有这些事情都是这样完成的?请解释一下这个概念?