When to use services [Answered]
This might sounds like a weird question, is it worth using services in a small-ish non-server console app? I haven't really ever used services (nor DI) to any extent since I've never done anything with ASP.NET or server-side, but I'm just wondering since a lot of people seem to talk about services quite often. How do you know when you should use services as an integral part of your architecture?
39 Replies
ive only used services (with a DI container) with MVVM
never used asp.net
so let's say you have entity called Order, you might create a class called an OrderService, that service class provides for services for the Order. IMHO services are a fast way to create god classes.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
like background services?
Honestly I don't really know, but I think the kind used with ASP.NET and whatnot
Where you have an
ISomethingService
you inject into stuff using DIUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
there's not a thing called a service container
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
show an example?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
soooo
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
services.AddSingleton<ITokenProvider, TokenProvider>();
yes that
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
calling it a service is a disservice in my opinion
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
There is a thing called a service in MS di
Whatever it's called, is it worth using
isnt that called a DI container?
DI can be useful, but it tends to get overblown
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
depends how complex your app is imho
A service is just a name for a class with a focus on functionality
and I think abstracting parts of your app's functionality into smaller classes is almost always a good idea
there is the concept of a hosted service
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
you need some form of dependency inversion
or at least it helps a lot
you don't expressly need DI to unit test
is there any form of dependency inversion outside of DI that is actually used/not bad?
the only one i can think of off the top of my head is service location, which is... not great
in my opinion
like mediatr?
mediatr is common
and has some di aspects
but far less
that counts? 🤔
hm, i guess
but yeah, DI is pretty simple, and works well for asp.net
it's a fast road to god classes too
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
unity is a di framework
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
tinyioc, ninject, etc
all the same
autofac
MEF is an alternative to DI
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
but MEF is pretty outdated and shouldn't be used
some of this really comes down to opinion
anyone who really used MEF eventually wrote a bunch of extensions to turn it into a modern DI framework
✅ This post has been marked as answered!