如何为两个或更多实体实现存储库模式

时间:2018-05-16 08:23:25

标签: .net repository repository-pattern repository-design

我正在尝试为Web Api项目实现存储库模式,并且在实现涉及两个或更多实体的存储库时感到困惑。处理此案件的最佳方法是什么?

通用存储库:

public interface IRepository<T>
{
    IEnumerable<T> Get();
    T GetById(int Id);
    int Save(T Object);
    bool Update(T Object);
    bool Delete(int Id);
}

学生资料库:

public class StudentRepository : IRepository<Student>
{
    private IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    public bool Delete(int Id)
    {
        int affectedRows;
        using (connection)
        {
            connection.Open();
            using (var trans = connection.BeginTransaction())
            {
                affectedRows = connection.Execute(StudentSql.Default.DELETE, new { Id = Id }, transaction: trans);
                trans.Commit();
            }
            return affectedRows > 0;
        }
    }

    public IEnumerable<Student> Get()
    {
        using (connection)
        {
            connection.Open();
            IEnumerable<Student> students = connection.Query<Student>(StudentSql.Default.SELECT);
            return students;
        }
    }

    public Student GetById(int Id)
    {
        using (connection)
        {
            connection.Open();
            Student student = connection.QueryFirstOrDefault<Student>(StudentSql.Default.SELECT_BY_ID, new { Id = Id });
            return student;
        }
    }

    public int Save(Student StudentModel)
    {
        int newRecordId;
        using (connection)
        {
            connection.Open();
            using (var trans = connection.BeginTransaction())
            {
                newRecordId = connection.QuerySingle<int>(StudentSql.Default.INSERT, new
                {
                    StudentModel.First_Name,
                    StudentModel.Last_Name,
                    StudentModel.Course_Id
                }, transaction : trans);

                trans.Commit();
            }
            return newRecordId;
        }
    }

    public bool Update(Student StudentModel)
    {
        int affectedRows;
        using (connection)
        {
            connection.Open();
            using (var trans = connection.BeginTransaction())
            {
                affectedRows = connection.Execute(StudentSql.Default.UPDATE, new
                {
                    StudentModel.Id,
                    StudentModel.First_Name,
                    StudentModel.Last_Name,
                    StudentModel.Course_Id
                }, transaction: trans);

                trans.Commit();
            }
            return affectedRows > 0;
        }
    }
}

服务:

IRepository<Student> repository;
    public StudentController(IRepository<Student> studentRepository)
    {
        repository = studentRepository;
        transformer = studentTransformer;
    }

    [HttpGet]
    [Route("api/v1/student")]
    public IHttpActionResult Get()
    {
        IEnumerable<Student> students = null;
        try
        {
            students = repository.Get();

            if (students.Count<Student>() > 0)
            {
                return Ok(students);
            }
            else
            {
                return StatusCode(HttpStatusCode.NoContent);
                //return Content(HttpStatusCode.NoContent, students);
            }
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }

如果我想要学生,那么我必须编写一个自定义存储库,它必须在通用存储库接口中实现所有5种方法。

如何实施这些方法?我可以在自定义存储库中编写插入课程和学生的查询,或者我必须将其委托给各个存储库以完成插入吗?

哪种方法最好?

0 个答案:

没有答案