Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a CRUD using an Interface

What is the best approach to implement a CRUD on the BL using interface that will be used to abstract DAL operations? I need your opinion guys..

Here's my draft..

Data Entities that are mapped in the database table

 public class Student
 {
    public string StudentId { get; set; }
    public string StudentName { get; set; }
    public Course StudentCourse { get; set; }
 }

 public class Course
 {
    public string CourseCode { get; set; }
    public string CourseDesc { get; set; }
 }

I created an CRUD Interface to abstract the object's operations

public interface IMaintanable<T>
{
   void Create(T obj);
   T Retrieve(string key);
   void Update(string key);
   void Delete(string key);
 }

And then a component that manages the Entity and its operations by implementing the interface

public class StudentManager : IMaintainable<Student>
{
    public void Create(Student obj)
    {
        // inserts record in the DB via DAL
    }

    public Student Retrieve(string userId)
    {
        // retrieveds record from the DB via DAL
    }

    public void Update()
    {
        // should update the record in the DB
    }

    public void Delete(string userId)
    {
        // deletes record from the DB
    }
}

sample usage

    public void Button_SaveStudent(Event args, object sender)
    {
        Student student = new Student()
        {
           StudentId = "1", StudentName = "Cnillincy"
        }

        new StudentManager().Create(student);   
     }

as you can see, there is quite an abnormalities on the update method

    public void Update()
    {
        // should update the record in the DB
    }

what should this method have to update the objects property? should I inherit the Student?

    public class StudentManager : Student , IMaintainable<Student>
    {
        public void Update()
        {
            //update record via DAL
         }
    }


    public void Button_SaveStudent(Event args, object sender)
    {
        Student student = new StudentManager();
        student.StudentId = "1";
        student.StudentName = "Cnillincy"
        student.Update()
    }

Or should I just contain the Student class as an attribute of the Student manager?

     public class StudentManager : IMaintainable<Student>
    {
        public Student student { get; private set };

        public void Create() {}
        public void Update() {}
        public void Retrieve() {}
        public void Delete() {}
    }

Which more appropriate? What about the interface? Any other suggestions guys? thanks..C

like image 847
CSharpNoob Avatar asked Sep 21 '10 15:09

CSharpNoob


People also ask

What is a CRUD interface?

In computer programming, create, read, update, and delete (CRUD) are the four basic operations of persistent storage. CRUD is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information using computer-based forms and reports.

How are CRUD operations implemented?

CRUD comes in since Application roles are added to the database using a stored procedure. It is also implemented by granting permission to execute the CRUD stored procedures and revoking direct access to the tables. Once an Application Role is added, permissions are assigned, and a password is given.

Which interface provides CRUD operations for the managed entity?

Spring Boot provides an interface called CrudRepository that contains methods for CRUD operations. It is defined in the package org.


1 Answers

Your CRUD interface should probably look like

public interface IMaintanable<T>
{
    string Create(T obj);
    T Retrieve(string key);
    void Update(T obj);
    void Delete(string key);
}

that is, both Create and Update take a copy of the object you're updating. The difference is that the Update can get the key from the obj, so it knows which object it's changing. Create would normally cause the key to be created so you pass it back as a return value. Hope that helps.

(The Update might also pass back the key too.)

like image 145
Lunivore Avatar answered Sep 20 '22 18:09

Lunivore