1

This is my third day doing experiments with ServiceStack ORMLite. My sample project which used to be working only with Dapper, now it is a mix of both Dapper and ServiceStack ORMLite, taking the best of both worlds.

Last night I was struggling with a Dapper query. It is basically a select joining 4 tables which was runing ok before ORMLite's code came in. Now it is failing over and over for the table mapping against objects. It doesn't break it just assigns wrong values to the objects.

I had to move on so as a temporary workaround what I did was executing each of the 4 selects without the SQL join using a Dapper's QueryMultiple and later, on C# joining the results one by one.

Please don't start laughing. I know this is of course, not the best solution at all. Yet it still makes one trip to DB but it doesn't feel natural. When the number of rows increases most likely this will be a performance issue.

This morning I think I found the source of my issue: I am droping and creating the tables using ORMLite and noticed that the field order isn't quite what I expected it to be. Before ORMLite code I just ran SQL scripts manually so I decided which order the fields had. Since my project needs to support MySql, Postgresql and SQL Server as the final user desires, it would be a pain to keep in sync three versions of such scripts. I would like to automate this process with ORMLite and take advantage out of it of course.

Now let me explain my theory and you expert guys may decide whether I am right or wrong. Dapper's query using joins splits the results of each table to map to each class, based on naming conventions. It expects the "Id" or "id" field to be the first field on a table.

I know Dapper has a "SplitOn" option to handle tables not following its naming convention ("IdProduct" as PK instead of "Id") but in my case it cannot be used like that, since I really don't have any control on how fields are ordered on ORMLite's CreateTable function. It could end up with "id" being on the middle and thus, causing the same issue I have now.

Maybe looking at my code can give you more clues. This is the code which was working fine before ORMLite:

        using (var conn = this.ConnectionString.OpenDbConnection())
        {
            string sql = @"
                select  *
                from    insumo i
                join    unidadmedida um on um.id = i.idum
                join    lineainsumo l on l.id = i.idlinea
                left outer join archivo a on a.id = i.idimagen;";

            var insumos = conn.Query<Entities.Insumo, Entities.UnidadMedida,
                Entities.LineaInsumo, Entities.Archivo, Entities.Insumo>(sql, (i, um, l, a)
                    =>
                    {
                        i.UnidadMedida = um; i.Linea = l; i.Fotografia = a ?? new Entities.Archivo();
                        return i;
                    }).ToList(); //Dapper

            return insumos;
        }

And this is the temporary workaround:

        using (var conn = this.ConnectionString.OpenDbConnection())
        {
            string sql = @"
                select * from insumo;
                select * from unidadmedida;
                select * from lineainsumo;
                select  a.*
                from    archivo a
                join    insumo i on i.idimagen = a.id;";


            var q = conn.QueryMultiple(sql); //Dapper
            var insumos = q.Read<Entities.Insumo>().ToList();
            var ums = q.Read<Entities.UnidadMedida>().ToList();
            var lineas = q.Read<Entities.LineaInsumo>().ToList();
            var archivos = q.Read<Entities.Archivo>().ToList();

            foreach (var i in insumos)
            {
                i.UnidadMedida = ums.FirstOrDefault(c => c.Id == i.IdUm);
                i.Linea = lineas.FirstOrDefault(c => c.Id == i.IdLinea);
                i.Fotografia = archivos.FirstOrDefault(c => c.Id == i.IdImagen) ?? new Entities.Archivo();
            }

            return insumos;
        }

I was looking into ORMLite's code and it seems the function ToCreateTableStatement may be the place to modify in order to solve my problem. Perhaps adding an attribute to each field on my class definition to state the desired order of field creation could work?

I was thinking on adding this to FieldDefinition code:

public bool FirstField { get; set; }

And adding an attribute to decorate C# fields like this:

[Alias("name")]
public string Name { get; set; }

[Alias("id")]
[PrimaryKey]
[FirstField(true)]
public int Id { get; set; }

Then when creating the field's the code could follow the values on such property and allow to control the final field's position on the table.

Please let me know if I am into the right track or, maybe there is another alternative more elegant that this one.

Thanks in advance.

4
  • Have you tried making Id the first property in the class definition? If it's still an issue can you show a class definition where OrmLite doesn't generate Id as the first column in the table?
    – mythz
    Jan 31, 2013 at 16:04
  • That's another workaround, if I could write Id as the first class property but, Id is a shared property among many classes (they derive from and abstract class.
    – coffekid
    Jan 31, 2013 at 18:30
  • So it seems I have two choices here: either forget about the abstract class and repeat all Id properties on the other classes or modify ORMLite's code. Thanks for the help, this is a small problem when compared with all the bells and whistles this ORM has, so at the end it pays off very well.
    – coffekid
    Jan 31, 2013 at 18:33
  • I'd forget about using abstract classes. You can use the T.GetId() extension method for a generic way to get the Id value of any type.
    – mythz
    Jan 31, 2013 at 19:12

1 Answer 1

1

It seems to be easier to just override the abstract Id on every class instead of modifying ORMLite's source code.

The project's schedule is narrow to try another approach. Perhaps for the next one I can find the time to try what @mithz kindly suggests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.