Sql – LINQ to SQL Generic Class for Insert and Delete operation

linq-to-sqlvb.net

I have been writing same code for insert, update, delete with LINQ over and over again. I want to have some sort of generic function for Insert, Update, Delete operation. I read a post here like the following :

    public static void Insert<T>(T entity) where T : class
{
   using (OrcasDB database = new OrcasDB())
   {
      database.GetTable<T>().Add(entity);
      database.SubmitChanges();
   }
}

public static void Delete<T>(Expression<Func<T, bool>> predicate)
where T : class
{
   using (OrcasDB database = new OrcasDB())
   {
      T instance = (T) database.GetTable<T>().Where<T>(predicate).Single();
      database.GetTable<T>().Remove(instance);
      database.SubmitChanges();
   }
}

How to Use
// insert
Employee will = new Employee
{
   Username = "will.asrari",
   EmailAddress = "me@willasrari.com",
   CanCode = true
};

LinqHelper.Insert<Employee>(will); 

// delete
LinqHelper.Delete(emp => emp.EmployeeId.Equals(3));

Yes, I would like to write something like in VB.NET. Is the code above good to follow? Can anyone show me any LINQ to SQL generic class for Insert, Delete, Update written in VB.NET?

Thank you.

Best Answer

FYI, I managed to write a simple class to do the generic CUD operantion for LINQ to SQL.

'Class GenericCUD.vb

Imports System.Linq.Expressions Imports System.Data.Linq

Public Class GenericCUD

Public Shared Sub Insert(Of T As Class)(ByVal theEntity As T)
    Using db As New DemoDataContext()
        db.GetTable(Of T)().InsertOnSubmit(theEntity)
        db.SubmitChanges()
    End Using
End Sub


Public Shared Sub Update(Of T As Class)(ByVal originalEntity As T, ByVal newEntity As T)
    Using db As New DemoDataContext()
        db.GetTable(Of T)().Attach(newEntity, originalEntity)
        db.Refresh(RefreshMode.KeepCurrentValues, newEntity)
        db.SubmitChanges()
    End Using
End Sub

Public Shared Sub Delete(Of T As Class)(ByVal theEntity As T)
    Using db As New DemoDataContext()
        db.GetTable(Of T)().Attach(theEntity)
        db.GetTable(Of T).DeleteOnSubmit(theEntity)
        db.Refresh(RefreshMode.KeepCurrentValues, theEntity)
        db.SubmitChanges()
    End Using
End Sub

End Class

How to use the class :

   'Using Insert
    Dim ta As New TestAuthor
    ta.FirstName = TextBox1.Text
    ta.LastName = TextBox2.Text
    GenericCUD.Insert(ta)


   'Using Update
    Dim original As New TestAuthor
    original.Id = 3


    Dim newEntity As New TestAuthor
    newEntity.Id = original.Id
    newEntity.FirstName = TextBox1.Text
    newEntity.LastName = TextBox2.Text

    GenericCUD.Update(original, newEntity)


   'Using Delete
    Dim ta As New TestAuthor
    ta.Id = 7
    GenericCUD.Delete(ta)

I read a lot of post on many blogs. Here are a few that really helped me to make the GenericCUD work:

  1. LINQ, Lambda, and Generics: Insert and Delete
  2. LINQ to SQL CRUD
  3. How to Make LINQ to SQL Check for Changes After Attach

So, What do you think about the GernericCUD class above? Please give me some comment because I want to improve it. Thank you.