LoadEntity event or callback

Jun 15, 2011 at 10:14 AM
Edited Jun 15, 2011 at 10:15 AM

Mar DataMapper uses LoadEntity event for custom loading entities. I propose to use callback Action<DbDataReader>

 

using (var db = CreateDb())
{
      var result = db.Query<Entity>(@"SELECT *  FROM table", (dbReader)=> {...});
}

Event usually used as a notification, but not modification.

Coordinator
Jun 15, 2011 at 2:36 PM

Yes, I think this approach definitely looks cleaner than the current event based approach, and I think it would also be more intuitive.

Since the Query() method is fluent, maybe the best approach would be to add a fluent method there to specify a LoadEntity action:

db.Query<Person>()
        .Where(p => p.ID == 1)
        .LoadEntity(reader => { ... });

As far as the existing Event, as much as I would like to get rid of it, that could be too much of a breaking change for anyone that has used that pattern already.  Perhaps that event could just be flagged as Deprecated.

Jordan

 

Jun 15, 2011 at 5:49 PM
Edited Jun 15, 2011 at 5:49 PM

Not all queries can be made using Linq. Marr DataMapper is first mapper, not orm. I prefer plain text sql and proposed just to add ability using the callback.  Is not required to delete "event way".

Coordinator
Jun 15, 2011 at 6:28 PM
vitidev wrote:

Not all queries can be made using Linq. Marr DataMapper is first mapper, not orm. I prefer plain text sql and proposed just to add ability using the callback.  Is not required to delete "event way".


You can still pass a query using the QueryText fluent method:

db.Query<Person>()
     .QueryText("SELECT * FROM table")
     .LoadEntity(reader => { ... });     

One benefit that I see to using fluent methods is that it reduces the number overloaded methods.

 

Jun 15, 2011 at 7:49 PM

  When using a fluent-interface, we are losing the power of Intellisence and need to remember more information, I think. But, I still may to add the method overload through extension.

 

db.Query<Person>()
     .QueryText("SELECT * FROM table")
     .LoadEntity(reader => { ... }); 

Method name "LoadEntity" is not intuitive. I think whould be better:

db.Query<Person>()
     .QueryText("SELECT * FROM table")
     .CustomEntityBuilder(reader => { ... });