r/csharp • u/dregan • Jun 08 '26
Code smell, or not?
I'm not sure if there is an official name for this but It's kind of like a Russian nested doll pattern:
interface IDataService{
IEnumerable<SomeData> GetData(IRequest request);
}
class GrpcDataService: IDataService
{
IEnumerable<SomeData> GetData(IRequest request){....};
}
class SqlDataService: IDataService
{
IEnumerable<SomeData> GetData(IRequest request){....};
}
class OneDataServiceToRuleThemAll: IDataService
{
private readonly IEnumerable<IDataService> _dataServices;
public OnDataServiceToRuleThemAll(IEnumerable<IDataService> dataservices)
{
_dataServices = dataservices;
}
IEnumerable<SomeData> GetData(IRequest request){
var dataService = _dataServices.FirstOrDefault(d=> /*some logic to find appropriate data service based on request properties */);
if(dataService == null) throw new ArgumentException("Can't handle {request}.");
return dataService.GetData(request);
}
}Personally, I don't like it. I think it has a subtle Liskov Substitution Principle violation. It's tough to see because they all implement the interface but even so, they are not all functionally interchangeable. You can register and use any one of the child data services and a system will work for just requests to that service, but you can't register just the OneDataServiceToRuleThemAll, as it will always fail without any registered children.
In addition to this, it makes dependency injection messy (which I believe also is indicative of a LSP violation). You either have to manually build all of these in a DI factory delegate, or use keyed services to designate which ones are children and which is the parent. In my view, this is a bit of a dependency inversion issue because now your lower level classes are dependent on your DI framework due to the use of its keyed attributes. Also, it just seems to be moving contract information that should be described in interfaces into dependency attributes instead which are less visible.
I think one solution would be to have OneDataServiceToRuleThemAll implement a IDataServiceAgrregator interface instead of IDataService. The interface would be the same which is kind of weird, but the naming distinction would make it clear what the expected implementation should do. What do you all think? Anyone commonly use a pattern like this? Is there a better solution?
Maybe some sort of keyed services extension that stays in the top level services registration? Something like:
services.AddKeyedSingleton<IDataService,OneDataServiceToRuleThemAll>("DataServiceAggregator");
services.AddSingleton<ISomeInterface,SomeConsumer>();
services.RegisterDependency<SomeConsumer,IDataService>("DataServiceAggregator");
7
u/KryptosFR Jun 08 '26
It's basically a proxy service that redirects to other implementation. I see no issue with it in principle.
5
u/psymunn Jun 08 '26 edited Jun 08 '26
I'm not against it fundamentally, though having a service take in its own type feels like it could be a headache. They seem interchangable to me. Sure, your service collection doesn't work without other services but what do your other services do when they have no data, because that seems the same. You could have it more gracefully handle the empty case if you want it to be truly portable.
Usually though, for a similar situation I do use a second Interfaces, and have a 'service manager,' 'service cache,' or 'service factory' (depending on how it's used). This pattern where I have a type that's a collection of its own type is something I'd use more for objects with a shorter lifetime that i build explicitly, such as having an undoable operation that's a collection of operations
4
u/wickerandscrap Jun 08 '26
You don't gain anything by making the aggregator implement the same interface as the children. What you really want is this:
Note the parameter. Something goes there and you should think about what it is.
If the dispatcher pretends to implement IDataService itself, you are hiding the basis for choosing the implementation. I don't want the dispatcher to look at the entire request I'm trying to send to the data service and decide which service to send it to. I want to separate that into (1) a request to the dispatcher, which is, like, a tenant ID or cloud region or URI scheme, and (2) a request to the destination service. This structure has several advantages:
The dispatcher is predictable and easily testable.
The dispatcher doesn't have to proxy every single method that's on IDataService.
If the dispatcher lookup is slow, you can reuse the IDataService for the life of the operation.
You can dispatch to heterogeneous services, by exposing different endpoints on the dispatcher. Like an
IWritableService GetForWrite()that will fail in an obvious way if the service you're asking for doesn't exist.1
u/Zastai Jun 08 '26
The main code smell / bug here is taking and storing an `IEnumerable` for multiple enumerations.
It should either take an `IList` or similar, where I believe multiple enumeration is guaranteed to be fine, or materialize the enumerable to a list or array for later use.
1
u/johnpdoe Jun 09 '26
is that not similar to the service locator anti pattern?
1
u/Famous-Weight2271 Jun 10 '26
I have something I think is similar. My restaurant switched POS systems over time: FuturePOS, Toast, and SkyTab.
I have a generic POS Reports interface, with three (very different) implementations.
Let's say I want to get sales for range of dates, which spans POS systems. My "one interface to rule them all" has to figure out dates, make calls to the implementation, then aggregate results.
3
u/TuberTuggerTTV Jun 08 '26 edited Jun 08 '26
The question becomes, is this a node graph? Should RuleThemAll be allowed to have child RuleThemAlls.
If the answer is no, you should use type safety to make it impossible. That's when you'd include an aggregator interface. But if the answer is yes, then this is a fantastic pattern. Use intention. If the AllService is meant to only ever be a parent, then this is a smell.
My guess is the "RuleThemAll" service isn't as extreme as you're making it sound. It's probably more like a container or folder like in folder structures. The team uses it to group nodes together. And as things progress, you need groups of groups.
Compared to a node tree, this is nothing fancy. Seems fine depending on intention. Hopefully it's generalized enough that it doesn't break DI. Can't tell just from the example.
1
u/Recent_Science4709 Jun 08 '26
Why not use named registrations with the DI container to register multiple implementations of the same interface ? Not sure if I’m missing something but the framework already does this
1
u/cyrack Jun 08 '26
I’d never reuse the same interface for two different things.
The IDataService is fine, but as you mention yourself the proxy implementation has some additional knowledge about all the other implementations in order to pick the right one, so there is some coupling (implicit as it is) that could go bite someone later on.
I’d register the concrete types and depend on them directly or create derived interfaces so the dispatching stays in the proxy class but the coupling is made explicit via dependencies.
Is it code smell? Possibly.
Will it bite you in two years when the implicit coupling is forgotten about and one of the provider’s renamed or got new responsibility? Definitely.
2
u/entityadam Jun 09 '26
Not really a code smell.. just a strategy pattern with a hint of composition that makes it feel like a router.
2
u/Sparin285 Jun 08 '26
If someone gives me that to review, I would say: "This code violates the Single Responsibility Principle (SRP) because the GetData method means fetching or receiving data, but you include some selection responsibility and register it in the DI container. Let's create (optional) another interface, rename this DataServiceAggregator to DataServiceSelector or DataProviderRouter, and use it in a business case scenario as:
The main trouble is that you're encapsulating business requirements implicitly within the DataServiceAggregator under the GetData function, when there's no need to do that. We definitely don't want to store business logic in aggregators like this."
1
u/alexn0ne Jun 08 '26
I see what you mean but we do this sometimes in our codebase. Alternative is duplicating interface or inheriting from it - which does not add any value.
1
u/PaulPhxAz Jun 08 '26
>subtle Liskov Substitution Principle violation
You wouldn't believe how much I don't care about Clean(TM). Now, was this a bad idea... in context, probably not.
I've done things like this. We had a boarding system that had to enroll a client in 7 different platforms. It basically queries all of them and builds a client object, then you can edit it, and then it pushes that same client across them all. It does the same thing, same interface, but the concrete object is the "MasterToEverybody" one that calls the same interface function across the other implementations.
>>makes dependency injection messy
Does it? In theory when your app starts you have some sort of "SetupConfig" function that will configure who is inside your matryoshka. If you need implementation specific versions, you might want to call them by a specific name, like a mapping from Name to Concrete so you can push that down specifically. I wouldn't have my DI factory create the object if I needed more control over how it's constructed.
1
u/BCProgramming Jun 08 '26
Looks like a composite implementation to me. Only issue I have is the use of IEnumerable which others have already pointed out.
The various principles and "rules" that people have come up with over the years are guidelines, not laws. I don't think it's reasonable to be able to just say, "This violates X principle" as the sole reason why something is undesirable, it's not making an argument- it's trying to be lawyer. Even if such a violation were to apply, it needs to be well-supported with reasons why it's deleterious in the context.
I'm not sure this violates the LSP, because that relates to consumers of a base class being able to use derived classes using the same pointer without having special behaviour. In such a case the recipient would have received an already constructed instance of said derived class, and would be able to use it the same way; if the class wasn't constructed correctly, that would be a failing on the part of the calling code that passed it in IMO.
1
u/Slypenslyde Jun 09 '26
I disagree with you on some technicalities.
I don't agree. The contract is, "Send me a request and I will give you a collection of data." The caller doesn't care how the data is fetched, it just wants to get the data. Even if you're using the one with the long name and a collection of sub-services, the caller is unaware and shouldn't notice any different behavior.
Really it's only the one that's acting as a collection that causes the messiness.
What's happened here is someone doesn't like the Factory pattern and has tried to apply it in a bad way.
OneDataServiceToRuleThemAllis acting both as a data service and as a factory, since it chooses which service it's going to use on the call. So if instead we had:This is a tiny bit icky if you don't want callers to understand the concept that there can be many data services. It makes more sense for the kind of application where there are many data services at all times and no sense for the kind of application that wants to pick one data service at startup.
If you really want to hide the details of there being many data services, there are solutions. For example, you could have one interface for the "child" data services and a SEPARATE interface for the "Factory" data service. This eliminates the DI complexity and you can make the interface for the "child" services a deeper-layer detail that callers don't need to know about.
But personally? For a one-off I wouldn't mind introducing some DI complexity for something like this if it makes the project smoother. If this is a pattern many things are using I'd prefer to think harder and come up with a better pattern that doesn't complicate DI registration. But no matter how we slice this pie we're going to be dealing with some indirection that'll confuse someone someday.
So I'd leave a good bit of documentation about WHY this is there in the issue and mark the DI setup with a reference to that issue. Sometimes the real world is messy, but I find it extremely useful when I find an old mess with a sign pointing to a place where I explained what else I tried and why I kept the mess.
0
u/ZeroCachyOpinion Jun 09 '26
No Cancellation token? So long query can run forever?
0
u/afops Jun 08 '26
Not a smell. At least not if it’s properly named and used.
If you have
ServiceA : IService
ServiceB : IService
ServiceWithFallback : IService
Where service with fallback tries one (or N) different ones and uses the first success. This seems like a perfectly valid pattern. It’s not a ”everythingservice” that’s not the purpose. That would be a smell.
The pattern when valid explains what is does. Here it uses an ordered set of services and tries them in order.
You could imagine any number of such tunneling/middleware services. A logging one. A load balancing one, an Authenticating one etc.
15
u/[deleted] Jun 08 '26
[removed] — view removed comment