. Extended support for dapper under Netcore SQL Server

Source: Internet
Author: User

Here we customize a iservicecollection extension, such as the following my extension

Services. Adddappercontext (dapperoptions =            {                "Data source=192.168.0.42;initial catalog= NET. Core; User Id=sa; [Email protected]#;integrated security=false";            });

Added to the database connection string settings, of course you can also set more parameters, delegates and so on, here is a simple demonstration under the custom dapper under the database access, the following is the extension settings

1   Public Static classdappermiddlewareextension2     {3 4          Public StaticIservicecollection adddappercontext<tdappercontext> ( ThisIservicecollection servicecollection, action<dapperoptions> optionsaction =NULL, Servicelifetime contextlifetime = servicelifetime.scoped, servicelifetime optionslifetime = ServiceLifetime.Scoped)whereTdappercontext:dappercontext5         {6 servicecollection.configure (optionsaction);7Servicecollection.addtransient<idataprovider, sqlserverdataprovider>();8Servicecollection.addsingleton<tdappercontext>();9             returnservicecollection;Ten         } One         /// <summary> A         ///Add Service -         /// </summary> -         /// <param name= "servicecollection" ></param> the         /// <param name= "optionsaction" ></param> -         /// <param name= "Contextlifetime" ></param> -         /// <param name= "Optionslifetime" ></param> -         /// <returns></returns> +          Public StaticIservicecollection Adddappercontext ( ThisIservicecollection servicecollection, action<dapperoptions> optionsaction =NULL, Servicelifetime contextlifetime = servicelifetime.scoped, servicelifetime optionslifetime =servicelifetime.scoped) -         { + servicecollection.configure (optionsaction); AServicecollection.addtransient<idataprovider, sqlserverdataprovider>(); atServicecollection.addsingleton<dappercontext>(); -             returnservicecollection; -}

Here di related database access class, here is the final point is we set in startup the connection string, in the database Di class how to get access to the database?

Servicecollection.configure (optionsaction); To configure the delegate action in the Ioptions interface
Here's a look at our dappercontext.
  Public classDappercontext {dapperoptions _dapperoptions;        Idataprovider _dataprovider;  PublicDappercontext (ioptions<dapperoptions>options, Idataprovider dataprovider) {_dapperoptions=options.            Value; _dataprovider=Dataprovider; }              #regionCreating Dapper related connectionsPrivateIDbConnection CreateConnection (BOOLEnsureclose =true)        {            varconn =_dataprovider.createconnection (); Conn. ConnectionString=_dapperoptions.connectionstring; Conn.            Open (); returnConn; }        PrivateIDbConnection _connection; Privateidbconnection Connection {Get            {                if(_connection = =NULL|| _connection. State! =connectionstate.open) {_connection=createconnection (); }                return_connection; }        }         Public voidInserttest (stringSQL) {            varconn =Connection; Try{Conn.            Execute (SQL); }            finally            {                if(_connection! =NULL) {_connection.                    Close (); _connection=NULL; }            }        }

In writing the relevant database access connection class processing
Create your own business services, it is relatively simple to write
 Public Interface Icustomdappercontext     {        void Insert (string  sql);           }

 Public class Customdappercontext:icustomdappercontext    {        dappercontext _dappercontext;          Public Customdappercontext (dappercontext dappercontext)        {            = dappercontext;        }          Public void Insert (string  sql)        {            _dappercontext.inserttest (SQL);        }    }

And then under the controller layer di
icustomdappercontext _context;          Public HomeController (icustomdappercontext context)        {            = context;        }          Public iactionresult Index ()        {          _context. Insert ("insert INTO Tb_userlogin (username,userpwd,[order],isdelete) VALUES (' UserName ', ' UserName ', 0,0)");             return View ();        }

Database added successfully after execution

Here are the related classes for my custom middleware

. Extended support for dapper under Netcore SQL Server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.