简体   繁体   中英

C# - Generic CRUD operation

How to achieve generic Save and Update operation using generics and reflection in C#?

I have achieved data retrieve using some reference from here...

I don't have much time to learn technologies like NHibernate/LINQ/Entity Frameowrk, etc. for this moment. I need a quick solution for this problem for some reason.

I think you'd be better of using an ORM -- LINQ to SQL, LINQ to Entities, LINQ to nHibernate -- rather than reinventing all of this. Essentially, what you are asking advice on doing has already been done for you in these frameworks/technologies. My advice is to spend some time learning about the tools that already exist and apply your creative energy to adding value to your application, using the already developed tools for the mundane work. Unless, of course, you're looking to implement an ORM because the existing ones are inadequate for your needs. I suspect this isn't the case, otherwise you'd already know how to use reflection to do what you're asking.

Use a DBContext helper of GenericType

 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;
            }
        }

And then instantiate and call the class here

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);
    }
}

I've added a DemoProject to gitHub here: https://github.com/andyf1ynn/EntityFramwork-Generic-Type-DAL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM