1

我有三个模型,如下面的第一个学生模型

public class Student
{
    public int ID { get; set; }

    [ForeignKey("account")]
    public int AccountID { get; set; }
    public Account account{ get; set; }  
    //some more

}

二等车型

public class ClassMigration
{
    public int ID { get; set; }
    public String Name { get; set; }

    public bool A { get; set; }
    //etc
}

第三个是学生班

public class StudentClass
    {
        public int ID { get; set; }

        [ForeignKey("student")]
        public int StudentID { get; set; }
        public Student student { get; set; }

        [ForeignKey("clas")]
        public int ClassID { get; set; }
        public ClassMigration clas { get; set; }

        public String Section { get; set; }

        public String Session { get; set; }
    }

这是我的学生和班级的抽象课程

    namespace Domain.Abstract
{
    public interface IStudent
    {
        IEnumerable<Student> Students { get; }
        Student SaveStudent(Student student);
        Student DeleteStudent(int ID);
    }
}



namespace Domain.Abstract
{
    public interface IClassMigration
    {
        IEnumerable<ClassMigration> Classes{ get; }
        ClassMigration SaveClass(ClassMigration clas);
        ClassMigration DeleteClass(int ID);
    }
}

这两个类的存储库是:

namespace Domain.Concret
{
    public class EFStudentRepository:IStudent
    {
        public readonly DbAccess context = new DbAccess();

        public IEnumerable<Entities.Student> Students
        {
            get { return context.Students; }
        }

        public Student SaveStudent(Entities.Student student)
        {
            if (student.ID == 0)
            {
                Account account = new Account();
                account.UserName = student.RegistrationNo;
                account.Password = "123456";
                account.Role = UserRole.Student;
                context.Accounts.Add(account);
                context.SaveChanges();
                student.AccountID = context.Accounts.Max(m => m.ID);

                context.Students.Add(student);
            }
            else
            {
                var org = context.Students.Find(student.ID);
                if (org != null)
                {
                    context.Entry(org).CurrentValues.SetValues(student);
                }
            }
            context.SaveChanges();
            if(student.ID==0)
            {
                student.ID = context.Students.Max(m => m.ID);
            }
            return student;
        }

        public Entities.Student DeleteStudent(int ID)
        {
            var std = context.Students.Find(ID);
            if (std != null)
            {
                context.Students.Remove(std);
                context.SaveChanges();
            }
            return std;
        }
    }
}



namespace Domain.Concret
{
    public class EFClassRepository:IClassMigration
    {
        public DbAccess context = new DbAccess();
        public IEnumerable<Entities.ClassMigration> Classes
        {
            get { return context.Classes; }
        }

        public Entities.ClassMigration SaveClass(Entities.ClassMigration clas)
        {
            if (clas.ID == 0)
            {

                context.Classes.Add(clas);
            }
            else
            {
                var org = context.Classes.Find(clas.ID);
                if (org != null)
                {
                    context.Entry(org).CurrentValues.SetValues(clas);
                }
            }
            context.SaveChanges();
            if (clas.ID == 0)
            {
                clas.ID = context.Classes.Max(m => m.ID);
            }
            return clas;
        }

        public Entities.ClassMigration DeleteClass(int ID)
        {
            var clas = context.Classes.Find(ID);
            if (clas != null)
            {
                context.Classes.Remove(clas);
                context.SaveChanges();
            }
            return clas;
        }
    }
}

以及我对班级和部分的看法

<div class="form-group">
                        <label for="Class">Class</label>
                        @Html.DropDownList("Class", @ViewBag.Classes as SelectList, new { @class = "form-control" })
                    </div>
                    <div class="form-group">
                        <label for="Section">Select Section</label>
                        <select id="Section" class="form-control">
                            <option selected="selected" value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                            <option value="D">D</option>
                            <option value="E">E</option>
                            <option value="F">F</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="FatherName">Father Name</label>
                        <input type="text" class="form-control" id="FatherName" placeholder="Father Name" />

现在我想当我们将学生添加到数据库时,我想将其他数据保存到 Student 表,并将班级信息保存到 StudentClass 表。我该怎么做???提前致谢

4

1 回答 1

2

这更多的是设计模式和工作方式的问题。根据您的需要,保存应由服务处理,让我们调用您的 StudentService.cs

这将采用 EFClassRepository 和 EFStudentRepository 的实例

服务接口 IStudentService.cs 应该由您正在保存的页面的控制器使用,这应该在服务上调用 save ,这反过来将获取您从页面传递的任何对象并使用这些详细信息来调用 save on两个存储库。举个简单的例子:

public class StudentService : IStudentService
    {

        private readonly IStudentRepository studentRepository;
        private readonly IClassRepository classRepository;

        public StudentService(IStudentRepository studentRepository, IClassRepository classRepository)
        {
            if (studentRepository == null) throw new ArgumentNullException(nameof(studentRepository));
            if (classRepository == null) throw new ArgumentNullException(nameof(classRepository));

            this.studentRepository = studentRepository;
            this.classRepository = classRepository;
        }

        public void Save(Student student, Class studentsClass)
        {
            if (student == null) throw new ArgumentNullException(nameof(student));
            if (studentsClass == null) throw new ArgumentNullException(nameof(studentsClass));

            studentRepository.Save(student);
            classRepository.Save(studentsClass);
        }
    }

界面:

interface IStudentService
    {
        void Save(Student student, Class studentsClass);
    }

假设您不想只为每个没有信息的学生保存一个空白课程,您的模型将至少包含对学生和班级的要求:

public class StudentPageModel
    {
        public int SchoolYear { get; private set; }
        public Student Student { get; private set; }

        public Class Class { get; private set; }

        public StudentPageModel(int schoolYear, Student student, Class studentClass)
        {
            SchoolYear = schoolYear;
            Student = student;
            Class = studentClass;
        }
    }

您当然可以只引用控制器中的两个存储库并删除服务,但它是关于可重用性的,您应该真正将其推送到服务来处理,控制器需要被视为页面背后的代码并作为尽可能最小化和愚蠢,将想法留给服务、演示者等。

养成如上所述使您的类不可变的习惯也很好,字段和属性应在构造函数和方法中设置,而不是直接访问,有助于构建一些代码完整性并防止意外更改。

于 2016-12-23T08:47:05.393 回答