C#-通用积垢操作

C#-通用积垢操作,c#,generics,reflection,ado.net,C#,Generics,Reflection,Ado.net,如何在C#中使用泛型和反射实现泛型保存和更新操作 我已经实现了数据检索使用一些参考 目前,我没有太多时间学习NHibernate/LINQ/Entity Framework等技术。出于某种原因,我需要快速解决这个问题。我认为最好使用ORM——LINQ到SQL,LINQ到实体,LINQ到nHibernate——而不是重新发明所有这些。从本质上讲,您所要求的建议已经在这些框架/技术中为您完成了。我的建议是花一些时间学习已经存在的工具,并将您的创造力用于为您的应用程序增加价值,将已经开发的工具用于日常

如何在C#中使用泛型和反射实现泛型保存和更新操作

我已经实现了数据检索使用一些参考


目前,我没有太多时间学习NHibernate/LINQ/Entity Framework等技术。出于某种原因,我需要快速解决这个问题。

我认为最好使用ORM——LINQ到SQL,LINQ到实体,LINQ到nHibernate——而不是重新发明所有这些。从本质上讲,您所要求的建议已经在这些框架/技术中为您完成了。我的建议是花一些时间学习已经存在的工具,并将您的创造力用于为您的应用程序增加价值,将已经开发的工具用于日常工作。当然,除非您希望实现ORM,因为现有的ORM不足以满足您的需求。我怀疑情况并非如此,否则您已经知道如何使用反射来完成您的要求。

我认为您最好使用ORM——LINQ到SQL,LINQ到实体,LINQ到nHibernate——而不是重新发明所有这些。从本质上讲,您所要求的建议已经在这些框架/技术中为您完成了。我的建议是花一些时间学习已经存在的工具,并将您的创造力用于为您的应用程序增加价值,将已经开发的工具用于日常工作。当然,除非您希望实现ORM,因为现有的ORM不足以满足您的需求。我怀疑情况并非如此,否则您已经知道如何使用反射来完成您的请求。

使用GenericType的DBContext助手

 public class ContextHelper<T>  : IContextHelper<T>
    {
        //Instantiate your own EntityFrameWork DB context here,
        //Ive called the my EntityFramework Namespace 'EF' and the context is named 'Reporting'
        private EF.DataContext DbContext = new EF.DataContext(); 
        public bool Insert<T>(T row) where T : class
        {
            try
            {
                DbContext.Set<T>().Add(row);
                DbContext.SaveChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
        public bool Update<T>(T row) where T : class
        {
            try
            {
                DbContext.Set<T>().AddOrUpdate(row);
                DbContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool Delete<T>(T row) where T : class
        {
           return Update(row); //Pass an entity with IsActive = false and call update method
        }
        public bool AddRows<T>(T[] rows) where T : class
        {
            try
            {
                DbContext.Set<T>().AddOrUpdate(rows);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
公共类ContextHelper:IContextHelper
{
//在此处实例化您自己的EntityFrameWork数据库上下文,
//我将my EntityFramework命名空间称为“EF”,上下文名为“Reporting”
private EF.DataContext DbContext=new EF.DataContext();
公共布尔插入(T行),其中T:class
{
尝试
{
DbContext.Set().Add(行);
DbContext.SaveChanges();
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
公共布尔更新(T行),其中T:class
{
尝试
{
DbContext.Set().AddOrUpdate(行);
DbContext.SaveChanges();
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
公共布尔删除(T行),其中T:class
{
return Update(row);//传递IsActive=false的实体并调用Update方法
}
公共bool AddRows(T[]行),其中T:class
{
尝试
{
DbContext.Set().AddOrUpdate(行);
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
然后在这里实例化并调用该类

public class MainLogicClassExample //Catty out logi operations on objects and update DataBase
{
    public void NewEmailRecipient(EF.EmailRecipient recipient)
    {
        // logic operation here

        EntityToDB(recipient);
    }
    public void NewReportRecipient(EF.ReportRecipient recipient)
    {
        // logic operation here

        EntityToDB(recipient);
    }
    public void UpdateEmailRecipient(EF.EmailRecipient recipient)
    {
        // logic operation here

        UpdateEntity(recipient);
    }

    public void UpdateReportRecipient(EF.ReportRecipient recipient)
    {
        // logic operation here

        UpdateEntity(recipient);
    }
    // call generic methods to update DB
    private void EntityToDB<T>(T entity) where T : class
    {
        var context = new ContextHelper<T>();
        context.Insert(entity);
    }

    private void UpdateEntity<T>(T entity) where T : class
    {
        var context = new ContextHelper<T>();
        context.Update(entity);
    }
}
public类MainLogicClassExample//Catty out对象上的logi操作并更新数据库
{
public void NewEmailRecipient(EF.EmailRecipient收件人)
{
//这里的逻辑运算
实体TODB(接收方);
}
public void NewReportRecipient(EF.ReportRecipient)
{
//这里的逻辑运算
实体TODB(接收方);
}
public void UpdateEmailRecipient(EF.EmailRecipient)
{
//这里的逻辑运算
更新(收件人);
}
public void UpdateReportRecipient(EF.ReportRecipient)
{
//这里的逻辑运算
更新(收件人);
}
//调用泛型方法来更新数据库
私有void EntityToDB(T实体),其中T:class
{
var context=new ContextHelper();
上下文。插入(实体);
}
私有void UpdateEntity(T实体),其中T:class
{
var context=new ContextHelper();
更新(实体);
}
}
我在这里向gitHub添加了一个DemoProject:

使用GenericType的DBContext帮助程序

 public class ContextHelper<T>  : IContextHelper<T>
    {
        //Instantiate your own EntityFrameWork DB context here,
        //Ive called the my EntityFramework Namespace 'EF' and the context is named 'Reporting'
        private EF.DataContext DbContext = new EF.DataContext(); 
        public bool Insert<T>(T row) where T : class
        {
            try
            {
                DbContext.Set<T>().Add(row);
                DbContext.SaveChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
        public bool Update<T>(T row) where T : class
        {
            try
            {
                DbContext.Set<T>().AddOrUpdate(row);
                DbContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool Delete<T>(T row) where T : class
        {
           return Update(row); //Pass an entity with IsActive = false and call update method
        }
        public bool AddRows<T>(T[] rows) where T : class
        {
            try
            {
                DbContext.Set<T>().AddOrUpdate(rows);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
公共类ContextHelper:IContextHelper
{
//在此处实例化您自己的EntityFrameWork数据库上下文,
//我将my EntityFramework命名空间称为“EF”,上下文名为“Reporting”
private EF.DataContext DbContext=new EF.DataContext();
公共布尔插入(T行),其中T:class
{
尝试
{
DbContext.Set().Add(行);
DbContext.SaveChanges();
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
公共布尔更新(T行),其中T:class
{
尝试
{
DbContext.Set().AddOrUpdate(行);
DbContext.SaveChanges();
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
公共布尔删除(T行),其中T:class
{
return Update(row);//传递IsActive=false的实体并调用Update方法
}
公共bool AddRows(T[]行),其中T:class
{
尝试
{
DbContext.Set().AddOrUpdate(行);
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
然后在这里实例化并调用该类

public class MainLogicClassExample //Catty out logi operations on objects and update DataBase
{
    public void NewEmailRecipient(EF.EmailRecipient recipient)
    {
        // logic operation here

        EntityToDB(recipient);
    }
    public void NewReportRecipient(EF.ReportRecipient recipient)
    {
        // logic operation here

        EntityToDB(recipient);
    }
    public void UpdateEmailRecipient(EF.EmailRecipient recipient)
    {
        // logic operation here

        UpdateEntity(recipient);
    }

    public void UpdateReportRecipient(EF.ReportRecipient recipient)
    {
        // logic operation here

        UpdateEntity(recipient);
    }
    // call generic methods to update DB
    private void EntityToDB<T>(T entity) where T : class
    {
        var context = new ContextHelper<T>();
        context.Insert(entity);
    }

    private void UpdateEntity<T>(T entity) where T : class
    {
        var context = new ContextHelper<T>();
        context.Update(entity);
    }
}
public类MainLogicClassExample//Catty out对象上的logi操作并更新数据库
{
public void NewEmailRecipient(EF.EmailRecipient收件人)
{
//这里的逻辑运算
实体TODB(接收方);
}
公共无效新报告接收人(EF.Repo