How to setup shared/common data in a web app
I've been working on a legacy project with .NET Framework 4.6.
there is a Shift class and initially it had this code in its Shift entity class:
now I'm updating everything to .NET 8 and trying to use clean architecture, I'm using Entity Framewokr 8.
It does not make sense to use direct DB calls in the entity class, these Shifts are all saved in the DB and could be edited anytime
but the None and DayOff shifts are used very often.
an example is GetShift() method for a staff which has something like this:
I'm a bit lost on what would be the best way to set this up.
I could pass those shifts to the GetShift method from a service but I'm wondering if there's a better solution,
this GetShift method is used in a lot of .Where clauses also.
16 Replies
You're correct in that entities should not contain code that connects to the database
You should eagerly fetch what you need, in one go
For example,
This will fetch the... thing
As well as the desired shifts
so far I've got this setup, the occurrenceItem service which gets occurrence-items based on a filter.
now I need to run the GetShift method in order to see if staff has a day off or not on that date.
so maybe I would move the GetShift method here and use/inject all needed repositories in the OccurrenceItemService?
my issue with that is I'll have to use GetShift() method in many other places so I would have to duplicate that code.
I have not used clean architecture before so I can't quite grasp what the setup should be
Looking at this code, I think it's too enterprise'y 17 layers of factories code for me to understand lmao
I added that myself actually, since I'm moving it to .net 8
my issue is I need to use those Shifts a lot and not sure the service is the best way to do it, I don't know if what I'm saying makes sense to you lol
A move to a non-obsolete version of the framework usually does not mean making the code more complex than it needs to be
Well then, make that service, and give it whatever methods it needs
GetNoneShifts()
GetDaysOff()
GetAllShifts()
etc
Inject DbContext
into the service
Inject the service wherever neededwould it make sense to inject a ShiftService in this Service?
Why?
or are service -to-service dependencies not good
since I could add the getShift method in a ShiftService
If so, then sure, do
The shift service should be dealing with shifts
exactly
Don't make a
ShiftService
and a ShiftButOnlyDayOffService
thanks, I'll go ahead with that ShiftService then
so you'd say there's no need with making that ShiftService a Singleton or something like that?
You can't make it a singleton if it uses the dbcontext anyway
makes sense, didn't think of that
thanks again
:Ok: