C# 有人能解释什么是私人只读的学生档案吗?为什么被拿走了?
有两个文件:一个提供如下接口: IStudentInterface.csC# 有人能解释什么是私人只读的学生档案吗?为什么被拿走了?,c#,dependency-injection,asp.net-mvc-4,inversion-of-control,C#,Dependency Injection,Asp.net Mvc 4,Inversion Of Control,有两个文件:一个提供如下接口: IStudentInterface.cs public interface IStudentService { IEnumerable<Student> GetStudents(); Student GetStudentById(int id); void CreateStudent(Student student); void UpdateStudent(Student student); void Dele
public interface IStudentService
{
IEnumerable<Student> GetStudents();
Student GetStudentById(int id);
void CreateStudent(Student student);
void UpdateStudent(Student student);
void DeleteStudent(int id);
void SaveStudent();
}
公共接口IStudentService
{
IEnumerable GetStudents();
学生GetStudentById(int id);
无效学生(学生);
无效更新学生(学生);
无效删除学生(内部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
}
公共班级学生服务:IStudentService
{
私人只读是学生档案库(studentRepository);
私人只读i工作单元(unitof工作单元);;
公共学生服务(IStudentRepository studentRepository,IUnitOfWork unitOfWork)
{
这个._studentRepository=studentRepository;
这个。_unitOfWork=unitOfWork;
}
#地区学生服务会员
公共IEnumerable GetStudents()
{
var studentRepository.GetAll();
留学生;
}
公立学生GetStudentById(int id)
{
var student=\u studentRepository.GetById(id);
留学生;
}
公立学校学生(学生)
{
_studentRepository.Add(学生);
_unitOfWork.Commit();
}
公共学生(内部id)
{
var student=\u studentRepository.GetById(id);
_studentRepository.Delete(学生);
_unitOfWork.Commit();
}
公共无效更新学生(学生)
{
_studentRepository.Update(学生);
_unitOfWork.Commit();
}
公共图书馆(学生)
{
_unitOfWork.Commit();
}
#端区
}
请解释为什么创建private
\u studentRepository
变量?我们也可以做我们的任务,而不是它。为什么所有这些事情都是这样做的?请给我解释一下这个概念?是一组功能(契约、抽象、服务,可以用不同的术语),StudentService
对象依赖于这些功能来执行其工作。StudentService
也依赖于IUnitOfWork
来执行其工作
private readonly IStudentRepository\u studentRepository
是一个变量声明,用于存储实现约定的对象的实例。它是私有的,因为它只需要在StudentService
类中访问;它是只读的,因为它是在类的构造函数中设置的,不需要在任何“StudentService”实例的生命周期中进行修改
控制反转的概念是,StudentService
不负责定位其依赖项的实现并实例化它们并管理它们的生命周期,而由StudentService
之外的其他一些代码承担这些责任。这是可取的,因为它减少了StudentService
类的顾虑
为了使应用控制反转更容易,通常使用容器。容器提供了一种方式来说明接口或契约应该使用哪些实现,还提供了一种机制来自动提供这些实现。最常见的方法是通过构造函数注入。IoC容器将创建
StudentService
类,还将创建构建该类所需的依赖项。在构造函数中,您可以将依赖项对象存储在类级变量中,以便在调用方法时可以使用它们执行工作。IstudeEntrepository
是一组功能(契约、抽象、服务-可以使用各种术语)StudentService
对象执行其工作所依赖的。StudentService
也依赖于IUnitOfWork
来执行其工作
private readonly IStudentRepository\u studentRepository
是一个变量声明,用于存储实现约定的对象的实例。它是私有的,因为它只需要在StudentService
类中访问;它是只读的,因为它是在类的构造函数中设置的,不需要在任何“StudentService”实例的生命周期中进行修改
控制反转的概念是,StudentService
不负责定位其依赖项的实现并实例化它们并管理它们的生命周期,而由StudentService
之外的其他一些代码承担这些责任。这是可取的,因为它减少了StudentService
类的顾虑
为了使应用控制反转更容易,通常使用容器。容器提供了一种方式来说明接口或契约应该使用哪些实现,还提供了一种机制来自动提供这些实现。最常见的方法是通过构造函数注入。IoC容器将创建
StudentService
类,还将创建构建该类所需的依赖项。在构造函数中,您可以将依赖项对象存储在类级变量中,以便在调用方法时使用它们执行工作。我认为qes的答案非常清楚
IStudentService是一个接口,它不包含具体的方法,只定义实现必须具备和必须具备的功能
StudentService类就是实现,您已经知道需要创建、更新、删除和保存这些方法,因为它们在接口中。所以你会在这个类中找到这个具体的方法
_studentRepository是一个字段,而不是一个属性,所以你不想让任何人接触它,这就是为什么它是私有的。但是也是只读的,因为在初始化StudentServices类时需要定义它,并且它需要保持与