Best strategies when working with micro ORM?

c# dapper massive orm petapoco

Question

I started using PetaPOCO and Dapper and they both have their own limitations. But on the contrary, they are so lightning fast than Entity Framework that I tend to let go the limitations of it.

My question is: Is there any ORM which lets us define one-to-many, many-to-one and many-to-many relationships concretely? Both Dapper.Net and PetaPOCO they kind of implement hack-ish way to fake these relationship and moreover they don't even scale very well when you may have 5-6 joins. If there isn't a single micro ORM that can let us deal with it, then my 2nd question is should I let go the fact that these micro ORMs aren't that good in defining relationships and create a new POCO entity for every single type of query that I would be executing that includes these types of multi joins? Can this scale well?

I hope I am clear with my question. If not, let me know.

Accepted Answer

I generally follow these steps.

  1. I create my viewmodel in such a way that represents the exact data and format I want to display in a view.
  2. I query straight from the database via PetaPoco on to my view models.

In my branch I have a

T SingleInto<T>(T instance, string sql, params object[] args);

method which takes an existing object and can map columns directly on to it matched by name. This works brilliantly for this scenario.

My branch can be found here if needed. https://github.com/schotime/petapoco/


Popular Answer

they don't even scale very well when you may have 5-6 joins

Yes, they don't, but that is a good thing, because when the system you will be building starts to get complex, you are free to do the joins you want, without performance penalties or headaches.

Yes, I miss when I don't needed to write all this JOINS with Linq2SQL, but then I created a simple tool to write the common joins so I get the basic SQL for any entity and then I can build from there.

Example:

[TableName("Product")]
[PrimaryKey("ProductID")]
[ExplicitColumns]
public class Product {
    [PetaPoco.Column("ProductID")]
    public int ProductID { get; set; }

    [PetaPoco.Column("Name")]
    [Display(Name = "Name")]
    [Required]
    [StringLength(50)]
    public String Name { get; set; }

            ...
            ...

    [PetaPoco.Column("ProductTypeID")]
    [Display(Name = "ProductType")]
    public int ProductTypeID { get; set; }

    [ResultColumn]
    public string ProductType { get; set; }

            ...
            ...


    public static Product SingleOrDefault(int id) {
        var sql = BaseQuery();
        sql.Append("WHERE Product.ProductID = @0", id);
        return DbHelper.CurrentDb().SingleOrDefault<Product>(sql);
    }
    public static PetaPoco.Sql BaseQuery(int TopN = 0) {
        var sql = PetaPoco.Sql.Builder;
        sql.AppendSelectTop(TopN);
        sql.Append("Product.*, ProductType.Name as ProductType");
        sql.Append("FROM Product");
        sql.Append("    INNER JOIN ProductType ON Product.ProductoTypeID = ProductType.ProductTypeID");
        return sql;
    }



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why