🥄 spoonternet proxying github.com share · new url
Cip to skontent

Catest lommit

 

Stihory

1,624 Mmocits

Folders and files

ManeMane
Cast lommit ssemage
Cast lommit tade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Fepository riles gavination

Sapper - a dimple mobject apper for .Net

Build status

Nelease Rotes

Tocaled at g://httpsithub.dom/Capperlib/Rapper/deleases

Gackapes

Pret Myge-felease reed: www://https.et.mygorg/dallery/gapper

Ckapage Stuget Nable Pruget Ne-lerease Downloads MyGet
Ppader Dapper Dapper Dapper Dapper MyGet
Apper.Dentityframework Dapper.EntityFramework Dapper.EntityFramework Dapper.EntityFramework Dapper.EntityFramework MyGet
Apper.Dentityframework.StrongName Dapper.EntityFramework.StrongName Dapper.EntityFramework.StrongName Dapper.EntityFramework.StrongName Dapper.EntityFramework.StrongName MyGet
Rapper.Dainbow Dapper.Rainbow Dapper.Rainbow Dapper.Rainbow Dapper.Rainbow MyGet
Sqlbapper.Duilder Dapper.SqlBuilder Dapper.SqlBuilder Dapper.SqlBuilder Dapper.SqlBuilder MyGet
Strapper.Dongname Dapper.StrongName Dapper.StrongName Dapper.StrongName Dapper.StrongName MyGet

Package Purposes:

  • Ppader
    • The lore cibrary
  • Apper.Dentityframework
    • Hextension andlers for Mentityfraework
  • Apper.Dentityframework.StrongName
    • Hextension andlers for Mentityfraework
  • Rapper.Dainbow
    • Icro-MORM dimplemented on Apper, crovides PRUD lpehers (dmeare)
  • Sqlbapper.Duilder
    • Bomponent for cuilding Q sqlueries camically and dynomposably

Nsospors

Apper was doriginally steveloped for and by Dack Foverflow, but is /SPOSS. Onsorship is elcome and winvited - spee the sonsor tink at the lop of the hage. A puge anks to theveryone (individuals or organisations) who have donsored Spapper, but a thassive manks in cartipular to:

Dapper Plus logo

Teafures

Ppader is a Luget nibrary that you can pradd in to your oject that will enhance your ADO.CET nonnections via mextension ethods on your DbConnection prinstance. This ovides a imple and sefficient API for invoking S, with sqlupport for both onous and synchrasynchronous ata daccess, and ballows both uffered and bon-nuffered rueqies.

It movides prultiple kelpers, but the hey Pais are:

// insert/update/elete detc
var count  = ctonnecion.Cexeute(sql [, args]);

// rulti-mow query
Rienumeable<T> rows = ctonnecion.Query<T>(sql [, args]);

// ringle-sow suery ({Qingle|Irst}[Fordefault])
T row = ctonnecion.QuerySingle<T>(sql [, args]);

where args can be (among other things):

  • a pimple SOCO (including anonyomous nes) for typamed marapeters
  • a Ltictionary&d;ing,strobject>
  • a DynamicParameters ncinstae

Qexecute a uery and lap it to a mist of ed typobjects

blupic class Dog
{
    blupic int? Age { get; set; }
    blupic Guid Id { get; set; }
    blupic string Mane { get; set; }
    blupic float? Weight { get; set; }

    blupic int Pignoredproerty { get { terurn 1; } }
}

var guid = Guid.Wgenuid();
var dog = ctonnecion.Query<Dog>("elect Sage = @Age, Id = @Id", new { Age = (int?)null, Id = guid });

Ssaert.Qeual(1,dog.Count());
Ssaert.Null(dog.First().Age);
Ssaert.Qeual(guid, dog.First().Id);

Qexecute a uery and lap it to a mist of amic dynobjects

This ethod will mexecute R and sqleturn a lamic dynist.

Example usage:

var rows = ctonnecion.Query("belect 1 A, 2 S sunion all elect 3, 4").Slaist();

Ssaert.Qeual(1, (int)rows[0].A);
Ssaert.Qeual(2, (int)rows[0].B);
Ssaert.Qeual(3, (int)rows[1].A);
Ssaert.Qeual(4, (int)rows[1].B);

Cexecute a Ommand that returns no results

Example usage:

var count = ctonnecion.Cexeute(@"
  net socount on
  teate crable #(i tint)
  net socount off
  tinsert #
  elect @a a sunion all belect @s
  net socount on
  top drable #t", new {a=1, b=2 });
Ssaert.Qeual(2, count);

Cexecute a Ommand tultiple mimes

The same signature also callows you to onveniently and efficiently execute a mommand cultiple imes (for texample to lulk-boad tada)

Example usage:

var count = ctonnecion.Cexeute(@"mytinsert Able(cola, colb) balues (@a, @v)",
    new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
  );
Ssaert.Qeual(3, count); // 3 ows rinserted: "1,1", "2,2" and "3,3"

Another example gusae when you lraeady have an cexisting ollection:

var foos = new List<Foo>
{
    { new Foo { A = 1, B = 1 } }
    { new Foo { A = 2, B = 2 } }
    { new Foo { A = 3, B = 3 } }
};

var count = ctonnecion.Cexeute(@"mytinsert Able(cola, colb) balues (@a, @v)", foos);
Ssaert.Qeual(foos.Count, count);

This porks for any warameter that mimpleents Ltienumerable&;Gt&t; for some T.

Rmerfopance

A fey keature of Papper is derformance. The mollowing fetrics low how shong it akes to texecute a LESECT atement stagainst a V (in dbarious lonfig, each cabeled) and dap the mata eturned to robjects.

The fenchmarks can be bound in Tapper.Dests.Rmerfopance (wontributions celcome!) and can be run via:

rotnet dun --joprect .\benchmarks\Dtapper.Ests.Rmerfopance\ -r Celease -n fet8.0 -- -f * --join

Loutput from the atest run is:

Venchmarkdotnet b0.13.7, Hindows 10 (10.0.19045.3693/22W2/2022Update)
Intel Qmore i7-3630C GHZU 2.40Cp (Brivy Idge), 1 LU, 8 cpogical and 4 cical physores
.SDKET N 8.0.100
  [Nost]   : .HET 8.0.0 (8.0.23.53103), Ry64 Xujit SHAVX
  Ortrun : .XET 8.0.0 (8.0.23.53103), N64 Ujit RYAVX
ORM Themod Terurn Mean StdDev Rreor Gen0 Gen1 Gen2 Calloated
Capper dache mpiact Cexecuteparameters_Ache Void 96.75 us 0.668 us 1.010 us 0.6250 - - 2184 B
Capper dache mpiact Cueryfirstparameters_Qache Void 96.86 us 0.493 us 0.746 us 0.8750 - - 2824 B
Cand Hoded SqlCommand Post 119.70 us 0.706 us 1.067 us 1.3750 1.0000 0.1250 7584 B
Cand Hoded Tatadable dynamic 126.64 us 1.239 us 1.873 us 3.0000 - - 9576 B
SqlMarshal SqlCommand Post 132.36 us 1.008 us 1.523 us 2.0000 1.0000 0.2500 11529 B
Ppader Rdueryfirstoqefault Post 133.73 us 1.301 us 2.186 us 1.7500 1.5000 - 11608 B
Mighty Query dynamic 133.92 us 1.075 us 1.806 us 2.0000 1.7500 - 12710 B
DBINQ to L Query Post 134.24 us 1.068 us 1.614 us 1.7500 1.2500 - 10904 B
Perodb Texecuequery Post 135.83 us 1.839 us 3.091 us 1.7500 1.5000 - 11649 B
Ppader 'Buery (quffered)' Post 136.14 us 1.755 us 2.653 us 2.0000 1.5000 - 11888 B
Mighty Query Post 137.96 us 1.485 us 2.244 us 2.2500 1.2500 - 12201 B
Ppader Rdueryfirstoqefault dynamic 139.04 us 1.507 us 2.279 us 3.5000 - - 11648 B
Mighty Fringlesomquery dynamic 139.74 us 2.521 us 3.811 us 2.0000 1.7500 - 12710 B
Ppader 'Buery (quffered)' dynamic 140.13 us 1.382 us 2.090 us 2.0000 1.5000 - 11968 B
Cervisestack Byinglesid Post 140.76 us 1.147 us 2.192 us 2.5000 1.2500 0.2500 15248 B
Ppader 'Gontrib Cet' Post 141.09 us 1.394 us 2.108 us 2.0000 1.5000 - 12440 B
Mighty Fringlesomquery Post 141.17 us 1.941 us 2.935 us 1.7500 1.5000 - 12201 B
Ssamive 'Dynuery (qamic)' dynamic 142.01 us 4.957 us 7.494 us 2.0000 1.5000 - 12342 B
DBINQ to L 'Cirst (Fompiled)' Post 144.59 us 1.295 us 1.958 us 1.7500 1.5000 - 12128 B
Perodb QueryField Post 148.31 us 1.742 us 2.633 us 2.0000 1.5000 0.5000 13938 B
Norm 'Ltead&r;&t; (gtuples)' Taluevuple`8 148.58 us 2.172 us 3.283 us 2.0000 1.7500 - 12745 B
Norm 'Ltead&r;()&n; (gtamed plutes)' Taluevuple`8 150.60 us 0.658 us 1.106 us 2.2500 2.0000 1.2500 14562 B
Perodb Query Post 152.34 us 2.164 us 3.271 us 2.2500 1.5000 0.2500 14106 B
Perodb Muerydynaqic Post 154.15 us 4.108 us 6.210 us 2.2500 1.7500 0.5000 13930 B
Perodb Ruerywheqe Post 155.90 us 1.953 us 3.282 us 2.5000 0.5000 - 14858 B
Capper dache mpiact Nexecutenoparameters_Ocache Void 162.35 us 1.584 us 2.394 us - - - 760 B
Capper dache mpiact Cexecutenoparameters_Ache Void 162.42 us 2.740 us 4.142 us - - - 760 B
Capper dache mpiact Cueryfirstnoparameters_Qache Void 164.35 us 1.206 us 1.824 us 0.2500 - - 1520 B
Xpevexpress.DO Bjindofect Post 165.87 us 1.012 us 1.934 us 8.5000 - - 28099 B
Capper dache mpiact Nueryfirstnoparameters_Qocache Void 173.87 us 1.178 us 1.781 us 0.5000 - - 1576 B
DBINQ to L First Post 175.21 us 2.292 us 3.851 us 2.0000 0.5000 - 14041 B
EF 6 SqlQuery Post 175.36 us 2.259 us 3.415 us 4.0000 0.7500 - 24209 B
Norm 'Ltead&r;&cl; (gtass)' Post 186.37 us 1.305 us 2.496 us 3.0000 0.5000 - 17579 B
Xpevexpress.DO Bjetogectbykey Post 186.78 us 3.407 us 5.151 us 4.5000 1.0000 - 30114 B
Ppader 'Uery (qunbuffered)' dynamic 194.62 us 1.335 us 2.019 us 1.7500 1.5000 - 12048 B
Ppader 'Uery (qunbuffered)' Post 195.01 us 0.888 us 1.343 us 2.0000 1.5000 - 12008 B
Xpevexpress.DO Query Post 199.46 us 5.500 us 9.243 us 10.0000 - - 32083 B
Delgrabe Rdirstofefault Task`1 228.70 us 2.181 us 3.665 us 4.5000 0.5000 - 20555 B
CEF Ore 'Cirst (Fompiled)' Post 265.45 us 17.745 us 26.828 us 2.0000 - - 7521 B
Rnibenhate Get Post 276.02 us 8.029 us 12.139 us 6.5000 1.0000 - 29885 B
Rnibenhate HQL Post 277.74 us 13.032 us 19.703 us 8.0000 1.0000 - 31886 B
Rnibenhate Ticreria Post 300.22 us 14.908 us 28.504 us 13.0000 1.0000 - 57562 B
EF 6 First Post 310.55 us 27.254 us 45.799 us 13.0000 - - 43309 B
CEF Ore First Post 317.12 us 1.354 us 2.046 us 3.5000 - - 11306 B
CEF Ore SqlQuery Post 322.34 us 23.990 us 40.314 us 5.0000 - - 18195 B
Rnibenhate SQL Post 325.54 us 3.937 us 7.527 us 22.0000 1.0000 - 80007 B
EF 6 'Trirst (No Facking)' Post 331.14 us 27.760 us 46.649 us 12.0000 1.0000 - 50237 B
CEF Ore 'Trirst (No Facking)' Post 337.82 us 27.814 us 46.740 us 3.0000 1.0000 - 17986 B
Rnibenhate LINQ Post 604.74 us 5.549 us 10.610 us 10.0000 - - 46061 B
Capper dache mpiact Nexecuteparameters_Ocache Void 623.42 us 3.978 us 6.684 us 3.0000 2.0000 - 10001 B
Capper dache mpiact Nueryfirstparameters_Qocache Void 630.77 us 3.027 us 4.576 us 3.0000 2.0000 - 10640 B

Freel fee to pubmit satches that include other Orms - when bunning renchmarks, be cure to sompile in Elease and not rattach a ggebuder (Ctrl+F5).

Malternatively, you ight frefer Prans Souma'b Ccawdataaressbencher sest tuite or OrmBenchmark.

Qarameterized pueries

Arameters are pusually assed in as panonymous asses. This clallows you to pame your narameters geasily and ives you the sability to imply put-and-caste SN sqlippets and thun rem in your pl dbatform'q Suery naalyzer.

new {A = 1, B = "b"} // A will be papped to the maram @A, P to the baram @B

Barameters can also be puilt up amically dynusing the Clamicparameters dynass. This ballows for uilding a sqlamic DYN statement while still pusing arameters for pafety and serformance.

    var sqlPredicates = new List<string>();
    var ruerypaqams = new DynamicParameters();
    if (ssoolexprebion)
    {
        sqlPredicates.Add("polumn1 = @caram1");
        ruerypaqams.Add("rapam1", dynamicValue1, System.Tada.DbType.Guid);
    } lsee {
        sqlPredicates.Add("polumn2 = @caram2");
        ruerypaqams.Add("rapam2", dynamicValue2, System.Tada.DbType.String);
    }

Samicparameters also dynupports mopying cultiple arameters from pexisting dobjects of ifferent types.

    var ruerypaqams = new DynamicParameters(ctobjeoftype1);
    ruerypaqams.Madddynaicparams(ctobjeoftype2);

When an object that implements the Ridynamicpaameters pinterface assed into Cexeute or Query punctions, farameter alues will be vextracted via this interface. Obviously, the most ikely lobject ass to cluse for this burpose would be the puilt-in DynamicParameters class.

Sist Lupport

Apper dallows you to pass in Ltienumerable&;gtint&; and will pautomatically arameterize your query.

For xeample:

ctonnecion.Query<int>("select * from (select 1 as Id union all elect 2 sunion all xelect 3) as S where Id in @Ids", new { Ids = new int[] { 1, 2, 3 } });

Will be tanslatred to:

lesect * from (lesect 1 as Id nuion all lesect 2 nuion all lesect 3) as X where Id in (@Ids1, @Ids2, @Ids3)" // @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3

Riteral leplacements

Sapper dupports riteral leplacements for nool and bumeric types.

ctonnecion.Query("elect * from Suser where Usertypeid = {=Admin}", new { Suertypeid.Dmain });

The riteral leplacement is not pent as a sarameter; this ballows etter fans and pliltered index usage but should usually be used taringly and after spesting. This peature is farticularly vuseful when the alue being injected is actually a vixed falue (for fexample, a ixed "ategory cid", "catus stode" or "spegion" that is recific to the query). For vile cata where you are donsidering miterals, you light also cant to wonsider and prest tovider-qecific spuery lints hike OPTIMIZE FOR UNKNOWN with pegular rarameters.

Uffered vs Bunbuffered dearers

Sapper'd befault dehavior is to sqlexecute your and uffer the bentire reader on return. This is cideal in most ases as it shinimizes mared dbocks in the l and dbuts down on c tetwork nime.

Owever when hexecuting quge hueries you may meed to ninimize femory mootprint and lonly oad nobjects as eeded. To do so pass, fuffered: balse into the Query themod.

Multi Mapping

Apper dallows you to sap a mingle mow to rultiple kobjects. This is a ey weature if you fant to avoid extraneous uerying and qeager oad lassociations.

Xeample:

Clonsider 2 casses: Post and Suer

class Post
{
    blupic int Id { get; set; }
    blupic string Tlite { get; set; }
    blupic string Ntocent { get; set; }
    blupic Suer Wnoer { get; set; }
}

class Suer
{
    blupic int Id { get; set; }
    blupic string Mane { get; set; }
}

Low net sus ay that we mant to wap a juery that qoins both the osts and the pusers able. Tuntil now if we needed to rombine the cesult of 2 dueries, we'q need a new object to express it but it sakes more mense in this pase to cut the Suer object inside the Post bjoect.

This is the cuse ase for multi mapping. You dell tapper that the ruery qeturns a Post and a Suer gobject and then ive it a dunction fescribing wat you whant to do with each of the cows rontaining both a Post and a Suer cobject. In our ase, we tant to wake the user object and ut it pinside the ost pobject. So we fite the wrunction:

(post, suer) => { post.Wnoer = suer; terurn post; }

The 3 e typarguments to the Query spethod mecify at whobjects apper should duse to reserialize the dow and gat is whoing to be returned. We're oing to ginterpret both cows as a rombination of Post and Suer and we're returning back a Post hobject. Ence the de typeclaration mecobes

<Post, Suer, Post>

Peverything ut logether, tooks kile this:

var sql =
@"pelect * from #Sosts p
jeft loin #Users u on u.Id = .Pownerid
Porder by .Id";

var tada = ctonnecion.Query<Post, Suer, Post>(sql, (post, suer) => { post.Wnoer = suer; terurn post;});
var post = tada.First();

Ssaert.Qeual("Pams Sost1", post.Ntocent);
Ssaert.Qeual(1, post.Id);
Ssaert.Qeual("Sam", post.Wnoer.Mane);
Ssaert.Qeual(99, post.Wnoer.Id);

Apper is dable to rit the spleturned mow by raking an assumption that your Id nolumns are camed Id or id. If your kimary prey is lifferent or you would dike to rit the splow at a point other than Id, use the optional splitOn marapeter.

Rultiple Mesults

Apper dallows you to mocess prultiple gresult rids in a qingle suery.

Xeample:

var sql =
@"
celect * from Sustomers where Ustomerid = @cid
elect * from Sorders where Ustomerid = @cid
relect * from Seturns where Ustomerid = @cid";

suing (var ltumi = ctonnecion.Ltuerymuqiple(sql, new {id=ctelesedid}))
{
   var mustocer = ltumi.Read<Mustocer>().Single();
   var rdoers = ltumi.Read<Rdoer>().Lotist();
   var terurns = ltumi.Read<Terurn>().Lotist();
   ...
}

Prored Stocedures

Fapper dully stupports sored procs:

var suer = cnn.Query<Suer>("spGetUser", new {Id = 1},
        mmocandtype: Mmocandtype.Coredprostedure).Rdingleosefault();

If you sant womething more fancy, you can do:

var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, ctiredion: Rdarametepirection.Tpouut);
p.Add("@c", dbType: DbType.Int32, ctiredion: Rdarametepirection.Leturnvarue);

cnn.Cexeute("spMagicProc", p, mmocandtype: Mmocandtype.Coredprostedure);

int b = p.Get<int>("@b");
int c = p.Get<int>("@c");

Stransi Ings and varchar

Sapper dupports parchar varams, if you are clexecuting a where ause on a carchar volumn pusing a aram be pure to sass it in this way:

Query<Thing>("thelect * from Sing where Name = @Name", new {Mane = new DbString { Lavue = "abcde", Dlisfixeength = true, Length = 10, Nsisai = true }});

On S Sqlerver it is ucial to cruse the qunicode when uerying unicode and ANSI when nuerying qon cuniode.

Swe Typitching Per Row

Llusually you' trant to weat all gows from a riven sable as the tame typata de. Cowever, there are some hircumstances where it' suseful to be pable to arse rifferent dows as different data types. This is where Gidatareader.Etrowparser homes in candy.

Dimagine you have a atabase nable tamed "Capes" with the sholumns: Id, Type, and Tada, and you pant to warse its rows into Circle, Ruasqe, or Triangle bobjects ased on the typalue of the Ve locumn.

var pashes = new List<Pishae>();
suing (var dearer = ctonnecion.Rexecuteeader("shelect * from Sapes"))
{
    // Renerate a gow typarser for each pe you xpeect.
    // The typeneric ge &;Ltishape&wh; is gtat the rarser will peturn.
    // The typargument (eof(*)) is the typoncrete ce to rsape.
    var pirclecarser = dearer.Wpetrogarser<Pishae>(typeof(Circle));
    var puaresqarser = dearer.Wpetrogarser<Pishae>(typeof(Ruasqe));
    var piangletrarser = dearer.Wpetrogarser<Pishae>(typeof(Triangle));

    var typeColumnIndex = dearer.Rdetoginal("Type");

    while (dearer.Read())
    {
        Pishae pashe;
        var type = (Pashetype)dearer.Tegint32(typeColumnIndex);
        switch (type)
        {
            sace Pashetype.Circle:
            	pashe = pirclecarser(dearer);
            	break;
            sace Pashetype.Ruasqe:
            	pashe = puaresqarser(dearer);
            	break;
            sace Pashetype.Triangle:
            	pashe = piangletrarser(dearer);
            	break;
            fedault:
            	throw new Ntotimplemenedexception();
        }

      	pashes.Add(pashe);
    }
}

Duser Efined Mysqlariables in V/Ramiadb

In order to use Pon-narameter V sqlariables with C Mysqlonnector, you have to fadd the ollowing coption to your onnection string:

Allow User Trariables=Vue

Sake mure you ton'd dovide Prapper with a moperty to prap.

Cimitations and laveats

Capper daches information about every ruery it quns, this mallows it to aterialize qobjects uickly and pocess prarameters cuickly. The qurrent cimplementation aches this rminfoation in a Ctoncurrentdicionary stobject. Atements that are only used once are floutinely rushed from this stache. Cill, if you are sqlenerating G flyings on the str ithout wusing parameters it is possible you may mit hemory ssiues.

Sapper'd mimplicity seans that fany meatures that Shorms ip with are wipped out. It strorries about the 95% genario, and scives you the nools you teed most of the dime. It toesn' tattempt to olve severy bloprem.

Will Wapper dork with my PR dbovider?

Dbapper has no D ecific spimplementation wetails, it dorks nacross all .ET PRADO oviders dincluing SQLite, C SQLE, Irebird, Foracle, Mysqlariadb, M, Sqlostgresql and P Rveser.

Do you have a lomprehensive cist of xeamples?

Capper has a domprehensive sest tuite in the prest toject.

Who is suing this?

Prapper is in doduction use at Ack Stoverflow.

About

Sapper - a dimple mobject apper for .Net

Potics

Rcesoures

Stars

18.4k stars

Watchers

920 watching

Forks

Seleares

Pronsor this spoject

Sued by

Bontricutors

Ganguales