Home > front end >  Is it okay to convert a result dataset in database layer
Is it okay to convert a result dataset in database layer

Time:10-07

I'm working on an ASP.NET MVC project in C# with an N-tier architecture.

A class (EmployeeRepo) in the database project has many functions.
Each function's natural result from the transaction is a dataset, but they are converted as per the need.

The return types are:

  • Dataset (with one or more tables)
  • DataTable
  • int
  • bool
  • Object (single employee)
  • List<Object> (list of employees)

Which is better among the two or is there a standard to follow:

  1. Return the result as is without any conversion. The conversion should only happen at other layer and not in the database layer.
  2. It is okay to convert the result in this layer before returning.

CodePudding user response:

Return the result as is without any conversion. The conversion should only happen at other layer and not in > the database layer.

In my view, repository layer should only do one thing. And this thing is to query database without applying any business logic and return result. Why? There are some reasones such as:

So all conversions are made in service layer.

In addition, it is not good idea to return Dataset (with one or more tables) or DataTable from repository. It is better to return IEnumerable<T>. I mean you should avoid to return IQueryable or DataTable from repository to avoid querying at service layer. There are many opinions about this. So you can choose what is better for you. Should Repositories return IQueryable? imho, repository should not return IQueryable as all query logic should be placed in one place and not scattered across services.

More over, I hihly recommend you to read this post about is it better to return the most specific or most general type from an action method?

CodePudding user response:

Generally speaking, your repository layer will return entities and it is your service layer that will repackage those into a form appropriate for your application to consume.

Note that what constitutes an entity may differ from project to project. For instance, if you use Entity Framework then it uses ADO.NET under the hood but it packages the data into entity classes and returns those objects. You could, in theory, do the same but, if you do, I'd question why you're not already using EF or some other ORM. If you're going to use ADO.NET directly then you probably ought to be returning DataTables and/or DataSets to your service layer. You should certainly not do some of one and some of the other. Go all in with one or the other.

Part of the idea behind a separate repository layer is that multiple different service layers should be able to be laid on top and work seamlessly, so the repository layer should be pretty general, based on the data rather than the application. Conversions are going to require business logic and that belongs in the service layer.

I was debating whether to post this as an answer or vote the question as too opinion-based but I think that the roles of the various layers are well-enough accepted that it's not really opinion any more.

CodePudding user response:

Ideally your repository layer returns data mapped to Domain entitites/Domain types. So the repository implementation would convert from low level db/ado specific types to your DTOs / Domain Types.

This way, you keep your domain clean and don't let those db specific types trickle into your business domain.

  • Related