I'm trying to figure out where to put business logic and why and where performance fits in. Im trying to get away from the fat logic-filled repositories that we produce a lot in my company.
So i'm trying to refactor this method DDD style:
var doesCollide = _bookingRepository.HasCollisionWithOtherBookings(bookingEntity, newEnd);
What i think i want is the following:
var doesCollide = booking.CollidesWithOtherBookings(allBookings, newEnd);
The internal logic is just doing some classic date mathematics. I still need to load allBookings from the database.
var allBookings = context.bookings.ToList();
var doesCollide = booking.CollidesWithOtherBookings(allBookings, newEnd);
But now I'm thinking, ALL of the bookings? I can improve performance a lot by just adding some simple conditionals to my query.
var allBookings = context.bookings.Where(x => x.Start < newEnd && booking.Start < x.End && x.Id != booking.Id).ToList();
var doesCollide = booking.CollidesWithOtherBookings(allBookings, newEnd);
Great. But i literally just duplicated the exact logic that i attempted to hide in CollidesWithOtherBookings. What does that even mean? Do i have to test this new query somehow?
Im not sure what im even testing here. The datetime logic is easy to mess up, so i should probably test it. If i test Booking.CollidesWithotherBookings thats great, but in my actual application i could have a bug since i can mess up in the where clause.