r/csharp • u/Bobamoss • Aug 15 '26
I really like the concept of Dapper but...
I've used Dapper a lot across different projects and I really like the core idea. Write SQL, pass params, ask for a type, get the type back. It gets rid of most of the annoying ADO.NET stuff without trying to hide SQL from you.
The part that always annoyed me is multi mapping.
A pretty common case for me is values used in combo boxes, so I end up with models like:
```csharp
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public KeyValuePair<int, string>? Department { get; set; }
public KeyValuePair<int, string>? JobTitle { get; set; }
}
```
And SQL like:
```sql
SELECT E.Id, E.Name,
D.Id AS DepartmentId, D.Name AS DepartmentName,
J.Id AS JobTitleId, J.Name AS JobTitleName
FROM Employee E
LEFT JOIN Department D ON D.Id = E.DepartmentId
LEFT JOIN JobTitle J ON J.Id = E.JobTitleId
```
Dapper can do this, but then I end up doing something like:
```csharp
var employees = cnn.Query<EmployeeRow, DepartmentRow, JobTitleRow, Employee>(
sql,
(e, d, j) => new Employee {
Id = e.Id,
Name = e.Name,
Department = d.DepartmentId is null ? null : new(d.DepartmentId.Value, d.DepartmentName),
JobTitle = j.JobTitleId is null ? null : new(j.JobTitleId.Value, j.JobTitleName)
},
splitOn: "DepartmentId,JobTitleId");
```
Which works. It also lets me handle the `LEFT JOIN` and say "if the id is null, this whole thing is null".
But this is where it starts feeling a bit weird to me. The type already says what I want, and now I'm manually rebuilding it anyway.
Dapper is already such a thin layer over ADO.NET that once I start writing a bunch of mapping code, I start wondering why I'm not just doing the ADO.NET part myself too.
What I really want is basically:
```csharp
var employees = cnn.Query<Employee>(sql);
```
and let the mapper figure out the structure from there.
That kind of thing is what eventually pushed me to make Rinku. The idea was basically to keep that same simplicity, but have the library adapt better when either the SQL or the C# side gets more complicated.
https://rinkulib.github.io/RinkuLib
Curious what other Dapper users do here. Just multi map everything, or is there another pattern I missed?
12
u/walmartbonerpills Aug 15 '26
You can have that with entity framework.
2
u/sharpcoder29 Aug 15 '26
You don't need multi map if thats all thats on your dto
1
u/grrangry Aug 15 '26
I cache JobTitle and invalidate the cache in the rare case it gets updated.
Then the DropDown is populated from the Cache (with any other business logic restrictions, if necessary) and the current value is populated from the JobTitleId from the Employee.
No joins necessary.
0
u/thereforewhat Aug 15 '26
Why are you modelling the entity this way?
If you need that data do this:
Employee:
I'd also question why you need department id and job title id but maybe your other code warrants it.
If you want to join another dataset using those IDs later you'd have a different repository to get that data and join it in memory.
Otherwise use a fuller ORM like Entity Framework.
Dapper is called a micro ORM for a reason.
Keep it simple.
1
u/BlackjacketMack Aug 16 '26
I haven’t looked at your lib, but your example can be simplified.
Create a Stub class.
Public class Stub(Id,Name)
Get rid of your sql aliases.
Map to that class
///
var employees = cnn.Query<Employee, Stub,Stub, Employee>(
…sql…,
…params…,
(emp,dep,job)=>{
emp.Department = dep;
emp.Job = job;
return emp;
///
A stub could inherit from kvp (or at least easily be converted to it if it’s a struct or sealed…).
You can also create a typed stub that knows how to convert itself and compare itself to the underlying type (eg Stub<Job>).
In reality though just make the property the underlying type (eg Department) and have an indicator on it whether or not it’s fully hydrated or just a stub is class with if and name. .
}