schotime / NPoco

Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco
Apache License 2.0
848 stars 302 forks source link

get rid of using statements , automatically open close #679

Closed blackholeearth closed 1 year ago

blackholeearth commented 1 year ago

question: is there a way to get rid of using statements , make npoco automatically open close. ?

user story: library is great, but for simple crud operations, this is just too much boiler plate. i have to write crud class just to hide this. (example class is at bottom/at 2nd comment)

current:

 using (IDatabase db = new Database("constr") )
 {
       var li = db.Fetch<ARACHAR>();  
 }

expected either this:

   var li = db.Fetch<ARACHAR>();   //automate "using  open close"

expected or this:

   var li = db.AutoClose().Fetch<ARACHAR>();   //automate "using  open close"

blackholeearth commented 1 year ago

example crud class: too see unnecessary boilerplate

   public class ARACHAR_crud 
    {

        public List<ARACHAR> get()
        {
            using (IDatabase db = new Database("constr") )
            {
               var li = db.Fetch<ARACHAR>();  

                return li;
            }

        }

        public void delete(int pkey)
        {
            using (IDatabase db = plakadb.new_db_con()) 
            {
                db.Delete<ARACHAR>(pkey);
            }
        }

        public void insert(ARACHAR poco)
        {
            using (IDatabase db = plakadb.new_db_con()) 
            {
                db.Insert<ARACHAR>(poco);
            }
        }

        public void update(ARACHAR poco , int pkey)
        {
            using (IDatabase db = plakadb.new_db_con())
            {
                db.Update(poco, pkey);
            }
        }

    }
schotime commented 1 year ago

Database does auto close if you don't explicitly call OpenSharedConnection()

blackholeearth commented 1 year ago

edit: yes. i don't explicitly call OpenSharedConnection() .

so the expected thing is already working as i expected ??

this is already automated , i dont have to use using statement, ??

var li = db.Fetch<MyPoco>();

schotime commented 1 year ago

Well, you never call OpenSharedConnection() so I guess so.

blackholeearth commented 1 year ago

thats good . thank you. im relived to hear that.

but this is. FAQ - able. lots of newbies to npoco are probably wondering about this.

thank you. for this good library.