0

Have dsl for executing InsertInto statements and it has this kind of signature:

 ICustomInterface<T1, T2, T3> InsertInto<T1, T2, T3>(string tableName, CustomType type1, 
CustomType type2,CustomType type3);

Example:

 var actualInsert = this._sql
                    .InsertInto<int, string, int>("dbo.Words",
                        type1,//this is some custom sql type
                        type2,
                        type3
                    )
                    .Values(
                        (43, "newVal2", 42), //actual values
                        (44, "newVal3", 42)
                    )

Problem I am having is that it will not work on endless type parameters. One kind of resolution I am thinking is make this kind of method:

 ICustomInterface<TRes> InsertInto<TRes>(string tableName, params CustomType[] types)
                where TRes : ITuple;

But it is not beautiful (You need to type it in brackates - InsertInto<(int,string,int)>) and can not be sure that CustomType[] array length will be equal as TRes tuple items length. Anyone have better idea?

DVL
  • 180
  • 2
  • 11
  • Does this answer your question? [Can I have a variable number of generic parameters?](https://stackoverflow.com/questions/1612887/can-i-have-a-variable-number-of-generic-parameters) – Paul Kertscher Feb 11 '20 at 08:45
  • Thanks for the comment. Question in the link is type of mine, but I need sql type of syntax and Insert(something).Insert(something) will not solve the problem – DVL Feb 11 '20 at 09:13
  • This interface seems bogus -- as far as SQL is concerned the columns in tables have no order. In SQL Server they *happen to have* a physical order based on when columns were created/dropped, but otherwise a table with rows `(ColA, ColB, ColC)` is semantically the same as a table with rows `(ColB, ColC, ColA)`. When you treat them as name/value pairs this problem disappears. Even assuming you've ordered the columns, it's not practical to be required to always specify all of them (defaults/identities). Are column names embedded in your custom types somehow? – Jeroen Mostert Feb 11 '20 at 10:31
  • Incidentally, the "solution" the framework itself employs for cases like this is to simply have overloads for every single arity, up to a number that's "reasonable" and/or an escape mechanism for embedding larger arities -- consider all the `Func` delegate types, which go up to 16 arguments. Elegant? No. Effective? Yes. The boilerplate required for this can be handled with a T4 template. – Jeroen Mostert Feb 11 '20 at 10:35
  • @JeroenMostert yes column names are in custom types and ordering is not problem. Example: this._sql .InsertInto("dbo.Words", IntType("Amount"), NVarCharType("Text", 100)) .Values((43, "newVal2"))' – DVL Feb 11 '20 at 11:45
  • Other than templates to produce combinations of every arity (or even every arity *and* every type), you won't be able to get full compile-time verification on that. Some compromise where a length or type check has to be inserted rather than enforced by the compiler is going to be inevitable. If I wanted compile-time checking for this I'd prefer code generation to spit out the correct methods/types for a specific data model, rather than try to handle this through generic methods. – Jeroen Mostert Feb 11 '20 at 11:56
  • @JeroenMostert I do not know what is reasonable in this situation. Sql table can contain many columns and do not like having interface which contains 16 overloads – DVL Feb 11 '20 at 12:00
  • This is why the vast majority of ORM approaches generate tailored code, and the generic ones that don't (like Dapper) *do* use all those overloads, and/or relax the restriction from having to pass in correctly typed information up front (i.e. they'll allow tuples, anonymous objects, `params object[]` arrays and anything else that's convenient to the user, and check for correctness at runtime). There's no such thing as a free lunch; look at things like F# if you want more advanced approaches that can do slick on-the-fly code generation like this. – Jeroen Mostert Feb 11 '20 at 12:03

0 Answers0