question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET Core transition: Inject service interfaces wherever possible

See original GitHub issue

This task is up for grabs, for contributing to the .NET Core transition project

On the development branch of the .net core transition project netcore/dev, we have used Current.Services.* a lot.

When this is used, we cannot move the class into Umbraco.Abstractions or Umbraco.Infrastructure.

This task is about eliminating the usage of Current.Services.* in general, by replacing it with a constructor injection of the interface of the given service that is in use. E.g. Current.Services.LocalizationService has to be replaced with injection of ILocalizationService.

Expected Procedure:

  • Find an instance class that uses Current.Services.*.
  • Add the interface of the service to the constructor.
  • Save the variable in a private readonly field.
  • Replace the usages of Current.Services.* with the field.
  • Make sure the project builds
    • Often you will need to inject this into classes that uses this specific class
  • Ensure unit tests all pass
  • Repeat the process

If any questions, feel free to ask 😃

Feel free to resolve this task in multiple smaller PRs, Due to the fact long running feature branch most likely have merge conflict with netcore/dev

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:20 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
benjaminccommented, Jan 23, 2020

In then end, I only fixed 6 of the 110 references to Current.Services. The rest break out as follows:

50 - Unit tests (these could likely be done, but not tonight) 40 - Attribute classes (best done with .NET Core and the TypeFilterAttribute) 9 - Default constructors calling DI constructors (see question above) 4 - Extension classes (would need to refactor or add arguments) 1 - PublishedContent with the explicit comment “we don’t want each published content to hold a reference to the service”

1reaction
hishamcocommented, Jan 8, 2020

Let me check …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Do we need interfaces for dependency injection?
No, you don't need interfaces for dependency injection. But dependency injection is much more useful with them!
Read more >
How to resolve dependencies in ASP.NET Core
Explore three different ways to resolve dependencies in ASP.NET Core 6, using constructor injection, action method injection, ...
Read more >
Dependency injection into views in ASP.NET Core
ASP.NET Core supports dependency injection into views. This can be useful for view-specific services, such as localization or data required ...
Read more >
ASP.NET Core Dependency Injection Best Practices, Tips ...
Service Locator · Do not use the service locator pattern wherever possible (if the service type is known in the development time). Because...
Read more >
The correct way to use Dependency Injection and Interfaces in ...
With . NET Core Dependency injection, lots of samples around the web miss the point of using interfaces, in the video you will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found