❔ Can someone help me understand this statement around dependency inversion with an example?

I was refreshing my memory on SOLID based on conversations in #chat today, and a particular statement is hurting my head this evening:
High-level modules should not depend on low-level modules. Both should depend on abstractions.
I'm wondering, can someone share a simple example that demonstrates how both can depend on abstractions? I think I'm just overthinking it at the moment.
4 Replies
Hazel 🌊💃
Hazel 🌊💃10mo ago
For clarity, the both part throws me off. I found another article that says what I currently do is fine So something like:
public interface IUserService {
Task<User> Get(Guid id, CancellationToken cancellationToken);
}
public class CachedUserService : IUserService {
...
public async Task<User> Get(Guid id, CancellationToken cancellationToken) {
if (_cache.TryGetValue(id, out User cachedUser))
return cachedUser;

var request = new UserByIdRequest(id);
var user = await _mediator.Send(request);
return user;
}
}
public class Sample {
private readonly IUserService _userService;
public Sample(IUserService userService) =>
_userService = userService;

// terrible example
public async Task PrintUsername(Guid userId) {
User user = await _userService.Get(userId);
if (user is null) {
Console.WriteLine($"User `{userId}` not found.");
return;
}

Console.WriteLine($"User `{userId}` has a username of `{user.Username ?? "null"}`.");
}
}
public interface IUserService {
Task<User> Get(Guid id, CancellationToken cancellationToken);
}
public class CachedUserService : IUserService {
...
public async Task<User> Get(Guid id, CancellationToken cancellationToken) {
if (_cache.TryGetValue(id, out User cachedUser))
return cachedUser;

var request = new UserByIdRequest(id);
var user = await _mediator.Send(request);
return user;
}
}
public class Sample {
private readonly IUserService _userService;
public Sample(IUserService userService) =>
_userService = userService;

// terrible example
public async Task PrintUsername(Guid userId) {
User user = await _userService.Get(userId);
if (user is null) {
Console.WriteLine($"User `{userId}` not found.");
return;
}

Console.WriteLine($"User `{userId}` has a username of `{user.Username ?? "null"}`.");
}
}
My understanding is that this is fine since I'm consuming the user service through an interface instead of interacting with CachedUserService directly.
x0rld
x0rld10mo ago
yeap your Sample doesn't need to know if there is any cache system
life grinder
life grinder10mo ago
yeah, essentially you have a layer of implementations which inherits from an accompanying layer of abstractions when you have to use something from that layer you register dependencies in a module and you're done ymmv depending on where those abstractions are, sometimes architecture could be more sophisticated than that
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ How to integrate stripe payment gateway into C# application?I have a small work that needs to be done soon. Only stipe payment integration into a C# applicationTypeInitializationException thrown by runtime when attempting to use JsonSerializer (.NET 8.0).Hi. I am running dotnet version 8.0.100-preview.4.23260.5, using ASP.NET version 8.0.0-preview.7.233❔ Assignment: TCPListener that recieves and returns to tcpClientHey. I do know how the tcpListener and tcpClient class works in c#, however im stuck on this assign❔ Sign up /Log in formI need help creating a sign up form for my app. I understand the components, but still find it quite❔ Trying to shorten this code.The code works well, but it's a bit dual coded so I've been trying to shorten it. I can't find a goo❔ CEFSharp is not working in WPF Application with DEVExpress.I am working on a mid-sized application that embed the website application in windows and shell view❔ OSS/Examples for JSON Deserialization?I'm working on a mid-sized application that does a lot of json deserialization from third party API ❔ My discord bot Does work in DMs but serversToday i wrote a simple C# bot in .NET framework. It Works well in dms but it does not answer my comm❔ Repository patternCreating a tutorial App and I have a EF Core DbContext injected in a a generic Repostitory<T> class.✅ Using services.PostConfigure<ApiBehaviorOptions> to log Model Validation errors in dotnet6?Is this a safe implementation to log Model Validation errors in dotnet6? ``` public static IServic