Architecture of .Net Core services, entity framework and responsibilities

asp.net c# entity-framework-core

Question

I'm working on a project with angular + net core 2.1 and entity framework core.

In my back end I have this structure:

FooController -> FooService -> FooRepository -> FooEFContext

​

Controllers only route requests to the right service(s).

Services know the business logic of how things work.

Repositories abstract EF.

EFContexts access data.

​

Now, imagine I have a two entities of this kind:

Employee n...1 Company (where a company may have n employees and an emply only 1 company)

​

And now imagine I'm at the point when I want to update the company data of a specific employee:

public void Post([FromBody]Company newData) {
  var user = _userService.GetUser( // id of current logged user );
  ...
  // Get the company associated with the user
  ...
  _companyService.Update(company)
}

How should I get the company? I have two alternatives in my mind:

_userService.GetUser(..) returns the user with all the details of the associated entities (company, location, .. others)

_userService.GetUser(..) returns just the user info, without including all the details, then I call:

_companyService.GetCompany( user.companyId)

In the first case I may have to have 2 different methods in my service: one that gets only the user data and another that gets the user data plus all the details of all the associated entities.

In the second case I do two roundtrips to the database, but I keep a more strict "single responsibility" concept for each service.

​

Which do you think is the better approach? Is there something I'm missing?

​

Thank you so much :)

1
2
8/28/2018 1:33:06 PM

Popular Answer

I have questioned myself on this scenario a bunch of times as well, but I really think that keeping things small with single responsibility is the way to go (so your second option). You state that the downside is more DB calls (which is good to be aware of), but in this instance I think that additional call doesn't warrant returning all associated entities together.

0
8/28/2018 2:01:07 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow