C
C#4w ago
Rename

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:
public static Shift None => Database.Find<Shift>(s => s.Department == null && s.IsNone);

public static Shift DayOff => Database.Find<Shift>(s => s.Department == null && s.IsDayOff);
public static Shift None => Database.Find<Shift>(s => s.Department == null && s.IsNone);

public static Shift DayOff => Database.Find<Shift>(s => s.Department == null && s.IsDayOff);
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:
public Shift GetShift(DateTime date)
{

var contract = GetContract(date);
if (contract == null)
return Shift.None;
...
}
public Shift GetShift(DateTime date)
{

var contract = GetContract(date);
if (contract == null)
return Shift.None;
...
}
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
Angius
Angius4w ago
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,
var thing = await context.Things
.Where(...)
.Select(t => new ThingDto {
Name = t.Name,
None = t.Shifts.FirstOrDefault(s => s.Department == null && s.IsNone),
DayOff = t.Shifts.FirstOrDefault(s => s.Department == null && s.IsDayOff),
})
.ToListAsync();
var thing = await context.Things
.Where(...)
.Select(t => new ThingDto {
Name = t.Name,
None = t.Shifts.FirstOrDefault(s => s.Department == null && s.IsNone),
DayOff = t.Shifts.FirstOrDefault(s => s.Department == null && s.IsDayOff),
})
.ToListAsync();
This will fetch the... thing As well as the desired shifts
Rename
Rename4w ago
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?
No description
Rename
Rename4w ago
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
Angius
Angius4w ago
Looking at this code, I think it's too enterprise'y 17 layers of factories code for me to understand lmao
Rename
Rename4w ago
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
Angius
Angius4w ago
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 needed
Rename
Rename4w ago
would it make sense to inject a ShiftService in this Service?
Angius
Angius4w ago
Why?
Rename
Rename4w ago
or are service -to-service dependencies not good since I could add the getShift method in a ShiftService
Angius
Angius4w ago
If so, then sure, do The shift service should be dealing with shifts
Rename
Rename4w ago
exactly
Angius
Angius4w ago
Don't make a ShiftService and a ShiftButOnlyDayOffService
Rename
Rename4w ago
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?
Angius
Angius4w ago
You can't make it a singleton if it uses the dbcontext anyway
Rename
Rename4w ago
makes sense, didn't think of that thanks again
Angius
Angius4w ago
:Ok:
Want results from more Discord servers?
Add your server