question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

linq2db as DTO layer

See original GitHub issue

Hello. We’ve been using Linq2db in a project for a while and we got to a stage where we understood that working directly with generated classes is not enough. Is it worth creating a BO and DTO separately, using linq2db?

If i would like to introduce a field UserCreated in my Users table, which would automatically use Insert date, how could i achieve that using Linq2db without an extra layer, where i would manually write my Insert function?

If then i introduce an extra layer, then in order to use linq for my queries (lets say i have

var admins = db.Users.Where(usr => usr.IsAdmin).ToList();

I would have to reproduce the exact same query on my BO class, something like this

var admins = Users.All().Where(usr => usr.IsAdmin);

which makes me load the whole list, thus generate two separated queries, which is not nice.

What is the best approach to this structure?

Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Aug 16, 2018

We need Expression Tree but not just object to object remapping, You have to write mapper only in the following way:

IQueryable<DTOUser> Map(this IQueryable<User> query)
{
   return query.Select(u => new DTOUser{
         UserId = u.USerId,
         ...
         UserCreated = u.InsertDate
      }
}

Or you can return Expresssion Tree for remapping:

Expression<Func<User, DTOUser>> Map()
{
    return u => new DTOUser{
         UserId = u.USerId,
         ...
         UserCreated = u.InsertDate
      }
}

And use in query

db.Users.Select(Map()).ToArray()
0reactions
sdanylivcommented, Aug 16, 2018

Ineresting that i’m thinking about universal implementation for such needs. And i have proposed interfaces for implementing this https://github.com/linq2db/linq2db/issues/230#issuecomment-412290451 Thinking about implementation of some independed DTOEntityServices with registration of IEntityMapping. Almost everything can be done without changing linq2db code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Confusion between DTOs (linq2sql) and Class objects!
Here is my opinion: When dealing with any non-trival application. Using your linq2Sql objects as your domain model is a really bad idea....
Read more >
LINQ to DB | Linq To DB
LINQ to DB is the fastest LINQ database access library offering a simple, light, fast, and type-safe layer between your POCO objects and...
Read more >
DTOGenerator: Easily Generate Data Transfer Objects from ...
First, we get DTO object from another layer in our application (GUI, for example), then, when we add the data (to LINQ to...
Read more >
Discussion on DTO Libraries for PHP
Also there's no DTO to Entity mapping. DTO captures request, another layer applies the request to the domain/model/whatever you want to call it....
Read more >
Efficient querying with LINQ, AutoMapper and Future queries
It's abstracted in the data assembly using DTOs in a Domain assembly being called from a business tier. But the Data layer is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found