PixxelKick
PixxelKick
CC#
Created by Eple on 7/18/2024 in #help
Question regarding multiple calls to AddHttpClient()
Your business layer shouldn't be aware of the service collection at all really. That's an application layer concern. Business layer really should just be defining all your services and not worrying about how the services get their dependencies. Application layer actually handles sewing it all together with DI and running it
11 replies
CC#
Created by Rillian on 7/17/2024 in #help
Help with EF Core polymorphic associations
If that is the case, then how I design the audit table us heavily influenced by the business requirements of said FE page and that's a "there's 100 ways to skin a cat" problem.
7 replies
CC#
Created by Rillian on 7/17/2024 in #help
Help with EF Core polymorphic associations
Do you actually have any need at all to display these audit logs in the application itself? If not, I'd just slap an enum on the column to discriminate but I wouldn't put any traversal properties on it. It'd be a write only table as far as EF core is concerned. Reading audit logs I usually leave to an admin with DB access unless the client explicitly requests the audit logs are exposed to the FE on some kind of admin page.
7 replies
CC#
Created by Gustavo Cruz on 7/16/2024 in #help
Overkill on the use of interfaces for Dependency Injection in Web APIs?
Only way that can happen is if you put a static field or static prop on the class for some reason. If you keep them readonly to just serve as effectively constant values though, it's still stateless really. Only if you did something weird like put a static list on the class that you kept adding stuff to would it be a problem
15 replies
CC#
Created by Gustavo Cruz on 7/16/2024 in #help
Overkill on the use of interfaces for Dependency Injection in Web APIs?
Which means you would want to Integration Test FooRepository, but you can Unit Test FooCalculationService
15 replies
CC#
Created by Gustavo Cruz on 7/16/2024 in #help
Overkill on the use of interfaces for Dependency Injection in Web APIs?
public static class FooRepository
{
public static void RecalculateFoo(AppContext ctx, int fooId)
{
var existingFoo = ctx.Db.Foos.SingleOrDefault(f = f.FooId == fooId);
var recalculation = FooCalculationService.RecalculateFoo(existingFoo);
existingFoo.Value = recalculation;
ctx.Db.SaveChanges();
}
}

public static class FooCalculationService
{
public static decimal RecalculateFoo(Foo existingFoo)
{
...
}
}
public static class FooRepository
{
public static void RecalculateFoo(AppContext ctx, int fooId)
{
var existingFoo = ctx.Db.Foos.SingleOrDefault(f = f.FooId == fooId);
var recalculation = FooCalculationService.RecalculateFoo(existingFoo);
existingFoo.Value = recalculation;
ctx.Db.SaveChanges();
}
}

public static class FooCalculationService
{
public static decimal RecalculateFoo(Foo existingFoo)
{
...
}
}
Note how FooCalculationService has no dependencies and is atomic. It merely takes in a plain ole Foo and does its logic. This is now very easy to unit test. No mocking needed, no dependencies, pure behavior, no side effects. It does it's atomic job and returns a value you can easily assert on. Meanwhile FooRepository depends on your db context and mostly just holds the logic for doing database stuff abd connecting to the Egress (ef core sql stuff)
15 replies
CC#
Created by Gustavo Cruz on 7/16/2024 in #help
Overkill on the use of interfaces for Dependency Injection in Web APIs?
No description
15 replies
CC#
Created by Gustavo Cruz on 7/16/2024 in #help
Overkill on the use of interfaces for Dependency Injection in Web APIs?
Which then brings up the "but then you cant moq how do you unit test?" Question, to which I've made this handy infographic:
15 replies
CC#
Created by Gustavo Cruz on 7/16/2024 in #help
Overkill on the use of interfaces for Dependency Injection in Web APIs?
Some people don't like this but it's served me very well. It reduces clutter, is less crud to maintain, and reduces inversion of control. 1. I don't make a service stateful unless I must, and even then I abstract out the stateful part to its own service and keep as much stateless logic in the static class as possible. 2. I don't add abstraction on top of the stateful service unless I need to mock it because it is "on the edge"
15 replies
CC#
Created by Gustavo Cruz on 7/16/2024 in #help
Overkill on the use of interfaces for Dependency Injection in Web APIs?
My rule of thumb is to only use DI for services that are stateful, or are involved in statefulness. Examples include: - Object pooling - HttpClientFactory stuff - Entity Franework DbContext - Configuration - Logging - Caching Etc, these various services actually are stateful in some way. For the vast majority of my logic I just use static classes and static methods. There's zero reason to use DI on a stateless singleton service, you just re-invented a static class when you do that.
15 replies
CC#
Created by douchebag on 7/12/2024 in #help
✅ Web API Controller Response JObject
80 replies
CC#
Created by douchebag on 7/12/2024 in #help
✅ Web API Controller Response JObject
C# is a strongly typed language
80 replies
CC#
Created by douchebag on 7/12/2024 in #help
✅ Web API Controller Response JObject
Was about to say, haha
80 replies
CC#
Created by douchebag on 7/12/2024 in #help
✅ Web API Controller Response JObject
That's also extremely atypical
80 replies
CC#
Created by douchebag on 7/12/2024 in #help
✅ Web API Controller Response JObject
Why?
80 replies
CC#
Created by douchebag on 7/12/2024 in #help
✅ Web API Controller Response JObject
You can just make your controller method return a Task<YourDTO> and it'll serialize by default using System.Text.Json
80 replies
CC#
Created by douchebag on 7/12/2024 in #help
✅ Web API Controller Response JObject
Asp.net controllers automatically handle json serialization for you
80 replies
CC#
Created by douchebag on 7/12/2024 in #help
✅ Web API Controller Response JObject
Is there a specific reason you are using JObject? Most folks don't use that
80 replies
CC#
Created by IsNotNull on 7/11/2024 in #help
More Elegant Way to Make a Generic Lookup Type
In that case I would just define a lookup interface, and make a few implementation options in sub packages for the consumer to choose how it's backed
15 replies
CC#
Created by IsNotNull on 7/11/2024 in #help
More Elegant Way to Make a Generic Lookup Type
If you are in a DI context, you could just register lookups for each T and use an inner scope service provider Though at that point you pretty much reinvented named services that we got recently, can prolly just use that
15 replies