News for hackers. Stuff that matters.
Re:Astonishing Performance of .NET 5 by didibus on Thursday November 12, 2020 @02:18PM (#25073704)
Nah, ServiceLocator is another overly abstruse pattern.

What you do instead is just:

public static IMyService myService;

public static IMyService getMyService() { if myService return myService; else if(isInTest()) return mockedMyService; else return new MyProdService(wtv); }

And call that in your controller that needs to use MyService.

Re:Astonishing Performance of .NET 5 by sharpercoder on Thursday November 12, 2020 @02:39PM (#25073952)
Above code is the servicelocator pattern. It's just for one service.
Re:Astonishing Performance of .NET 5 by didibus on Friday November 13, 2020 @01:05AM (#25079412)
ServiceLocator has you create a runtime service registry where you can dynamically register and fetch instances too and from. And often that means it bypasses static type checking, as you might be adding and fetching services by name using a string.

This is different in that it's static. You don't add a service to it, it creates it the first time you ask for it (if singleton), or everytime you ask for it (otherwise), or pools it, etc.

I recon some similarities, but most enterprise "service locator" involve a lot more than this, and often are meant to give you this runtime dynamism where you can load and unload services as the app is running, etc. making them way more complex for apps that don't care about this.

Re:Astonishing Performance of .NET 5 by shireboy on Thursday November 12, 2020 @03:38PM (#25074639)
Actually, as written, myService never gets set, but I think I know what you mean. This is not thread-safe and assumes you want singleton. To use it you would in each controller do:

var service = Startup.getMyService();

And potentially know if the controller should dispose service.

This doesn't seem much less "in the way" or "massive" than:

services.AddSingleton<IMyService,MyService>();