1

I am using Dapper FastCRUD for insert/update for my project. I have a entity which inherits from another entity. Both of them have separate destination tables, annotated with TableAttribute. Is there a way to save data to both the tables using Fast CRUD, if I have an object of type, which is the inherited class?

tj_abe
  • 25
  • 3

1 Answers1

0

unfortunately I don't think we can expect Dapper FastCRUD or any of the common dapper extensions to handle object inheritance in that way - I've played with a few and no luck.

Best I could come up with was to call Convert.ChangeType before I handover to Dapper to perform the requested operation.

For example, I like to keep my database interactions separate from my business logic. The idea being that I can easily swap out the ORM for another one without going anywhere near my business logic.

public void Create<T>(T bo) where T : BusinessObject
{
    var castedBo = (T)Convert.ChangeType(bo, typeof(T));
    Connection.Insert(castedBo);
}

My Object I'm trying to persist extends BusinessObject

Table("Blog")]
public class Blog : BusinessObject {
 ...
}

and I can then persist my object like so

Blog blog = new Blog();
dataMapper.Create(Blog)

Hope this helps!

Ade
  • 16
  • Thanks @Ade. The best I could come up with, was to override GetEntityProperties on fastcrud OrmConventions class, to filter for properties belonging to a particular Type. Since OrmConventions is an application level setting, I decided not to use that. – tj_abe Mar 29 '17 at 14:37