有兩個文件:一個給人界面如下:任何人都可以解釋什麼是私人只讀IStudentRepository _studentRepository?爲什麼要採取?
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();
}
StudentService.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
?我們也可以完成我們的任務而不是它。爲什麼所有這些都是這樣完成的?請給我解釋一下這個概念?
正是我想要的......謝謝 –