Iam planning to use Contrib with dapper to make my classes in desktop applications look like this
public abstract class DB : IDisposable
{
public virtual long Insert()
{
using (var db = ConFactory.GetConnection())
{
db.Open();
return db.Insert(this);
}
}
// ... and other CRUD operations
}
then any Concept class will inherits from DB class; like this
[Table("test")]
public class Test : DB
{
[Key]
public int TestId { get; set; }
public string Name { get; set; }
}
using Test class
using (Test t = new Test { Name = textBox2.Text })
{
textBox1.Text = t.Insert().ToString();
}
this sample always fails and gives me
SQL logic error near ")": syntax error
BUT when I implement Insert()
method inside child class it works well !!!
the problem is: my code contains lot of classes, rewrite All CUD operations is so disturbing,
any idea to solve this with less code?
I figured out a better way to solve this... which is more flexible and more pro i think. I'll put the solution here ... soon on github
the solution based on Extension methods, to inject CUD operation with only concept classes I proposed IDbTable
base class
public abstract class IDbTable : IDisposable
{
public void Dispose()
{
// do disposing if you need
}
}
public static class DB
{
public static long Insert<T>(this T entity) where T : IDbTable
{
using (var db = ConFactory.GetConnection())
{
db.Open();
return db.Insert(entity);
}
}
public static void Delete<T>(this T entity) where T : IDbTable
{
using (var db = ConFactory.GetConnection())
{
db.Open();
db.Delete(entity);
}
}
public static void Update<T>(this T entity) where T : IDbTable
{
using (var db = ConFactory.GetConnection())
{
db.Open();
SqlMapperExtensions.Update(db, entity);
}
}
public static T Get<T>(int id)
{
using (var db = ConFactory.GetConnection())
{
db.Open();
return db.Get<T>(id);
}
}
}
ok this is all
when any class inherits from IDbTable
base class, it have insert, update and delete method as extenssion
[Table("test")]
public class Test : IDbTable
{
[Key]
public int TestId { get; set; }
public string Name { get; set; }
}
.......
Test t = new Test { Name = textBox2.Text };
textBox1.Text = t.Insert().ToString();
AND it WORKS fine !!!
any suggestion to improve will be appreciated.