Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Расширения для внедрения зависимостей #256

Open
wants to merge 2 commits into
base: feature-239-use-proper-di
Choose a base branch
from

Conversation

turbcool
Copy link
Contributor

@turbcool turbcool commented Jun 14, 2023

Добавлена возможность резолвить именованные зависимости через IServiceProvider, используя внутри зарегистрированный IUnityContainer.

@sonarcloud
Copy link

sonarcloud bot commented Jun 14, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

/// Получить именованную зависимость с помощью IUnityContainer (IUnityContainer должен быть зарегистрирован в IServiceProvider).
/// </summary>
/// <typeparam name="T">Тип.</typeparam>
/// <param name="serviceProvider">IServiceProvider.</param>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Тип.
/// IServiceProvider."

Непонятные комментарии.

using Unity;

/// <summary>
/// Расширения для внедрения зависимостей.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Формально в новых классах нужно стараться делать комментарии на английском.


if (unityContainer == null)
{
throw new InvalidOperationException($"Невозможно получить именнованную зависимость - IUnityContainer не зарегистрирован в этом ServiceProvider.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше на английском.

}
else
{
return (unityContainer as IUnityContainer).Resolve<T>(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А вот тут некоторая особенность. Дело в том, что насколько я поняла, в описании IServiceProvider в методе GetService если зависимость не зарегана, то должно вернуться null. UnityFactory в таком раскладе вернёт exception.
Но опять же, метод isregistered не совсем удачен для проверки, потому что может быть что-то не зарегано, но имеющихся сведений может хватить, чтобы зарезолвить.

/// <param name="name">Имя зависимости.</param>
/// <returns>Разрешенная зависимость.</returns>
/// <exception cref="InvalidOperationException">Ошибка получения именованной зависимости.</exception>
public static object GetService<T>(this IServiceProvider serviceProvider, string name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обязательно нужны тесты:

  1. Ничего не зарегано.
  2. Зарегано всё.
  3. Зареганы только параметры конструктора, а сам тип нет.
  4. Именованные\неименованные резолвы.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тесты реализованы здесь

@sonarcloud
Copy link

sonarcloud bot commented Jul 21, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants