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

Register unity without config file #286

Open
wants to merge 14 commits into
base: feature-239-use-proper-di
Choose a base branch
from
40 changes: 0 additions & 40 deletions Tests/NewPlatform.Flexberry.ORM.ODataService.Tests/App.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<connectionStrings>
Expand All @@ -15,45 +14,6 @@
<add key="DataServiceType" value="ICSSoft.STORMNET.Business.MSSQLDataService, ICSSoft.STORMNET.Business.MSSQLDataService" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="singleton" type="Unity.Lifetime.ContainerControlledLifetimeManager, Unity.Abstractions" />
<container>
<register type="ICSSoft.STORMNET.Business.Audit.IAuditService, ICSSoft.STORMNET.Business" mapTo="ICSSoft.STORMNET.Business.Audit.AuditService, ICSSoft.STORMNET.Business">
<lifetime type="singleton" />
</register>
<!-- Для использования экспорта нужно установить пакет NewPlatform.Flexberry.Reports.ExportToExcel -->
<!--
<register name="Export" type="NewPlatform.Flexberry.IExportService, ICSSoft.STORMNET.Business" mapTo="NewPlatform.Flexberry.Reports.ExportToExcel.ExportExcelODataService, NewPlatform.Flexberry.Reports.ExportToExcel">
<lifetime type="singleton" />
</register>
-->
<register type="NewPlatform.Flexberry.IODataExportService, ICSSoft.STORMNET.Business" mapTo="NewPlatform.Flexberry.ORM.ODataService.Tests.CRUD.Read.Excel.ExportExcel,NewPlatform.Flexberry.ORM.ODataService.Tests">
<lifetime type="singleton" />
</register>
<!--
<register type="NewPlatform.Flexberry.Reports.ExportToExcel.ISpreadsheetCustomizer, NewPlatform.Flexberry.Reports.ExportToExcel" mapTo="NewPlatform.Flexberry.ORM.ODataService.Tests.CRUD.Read.SpreadsheetCustomizer,NewPlatform.Flexberry.ORM.ODataService.Tests">
<lifetime type="singleton" />
</register>
-->
<register type="ICSSoft.STORMNET.Business.IConfigResolver, ICSSoft.STORMNET.Business" mapTo="ICSSoft.STORMNET.Business.ConfigResolver, ICSSoft.STORMNET.Business">
<lifetime type="singleton" />
<constructor />
</register>

<register type="NewPlatform.Flexberry.Caching.ICacheService, NewPlatform.Flexberry.Caching" mapTo="NewPlatform.Flexberry.Caching.MemoryCacheService, NewPlatform.Flexberry.Caching">
<lifetime type="singleton" />
<constructor>
<param name="cacheName" type="System.String" value="defaultCacheForApplication" />
<param name="defaultExpirationTime" type="System.Int32" value="3600" />
</constructor>
</register>

<register type="NewPlatform.Flexberry.Security.IPasswordHasher, NewPlatform.Flexberry.Security" mapTo="NewPlatform.Flexberry.Security.Sha1PasswordHasher, NewPlatform.Flexberry.Security">
<lifetime type="singleton" />
<constructor />
</register>
</container>
</unity>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using System;

using Unity;

namespace NewPlatform.Flexberry.ORM.ODataService.Tests
{
using System;
Copy link
Contributor

Choose a reason for hiding this comment

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

Для чего здесь юзинги добавлены?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

using ICSSoft.STORMNET.Business;
using ICSSoft.STORMNET.Business.Audit;
using ICSSoft.STORMNET.Business.Interfaces;
using ICSSoft.STORMNET.Security;
using NewPlatform.Flexberry.Caching;
using NewPlatform.Flexberry.ORM.CurrentUserService;
using NewPlatform.Flexberry.Reports.ExportToExcel;
using NewPlatform.Flexberry.Security;
using Unity;
using Unity.Injection;

/// <summary>
/// Specifies the Unity configuration for the main container.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class UnityWebApiActivator
/// <summary>
/// Integrates Unity when the application starts.
/// </summary>
public static void Start()
public static void Start()
torres6093 marked this conversation as resolved.
Show resolved Hide resolved
{
// Use UnityHierarchicalDependencyResolver if you want to use
// a new child container for each IHttpController resolution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ protected BaseIntegratedTest(CustomWebApplicationFactory<Startup> factory, ITest
_factory = factory;
#endif

_container = new UnityContainer();
#if NETCOREAPP
_container.LoadConfiguration();
#endif
_container = new UnityContainer();
_serviceProvider = new UnityServiceProvider(_container);
_container.RegisterFactory<IBusinessServerProvider>(new Func<IUnityContainer, object>(o => new BusinessServerProvider(new UnityServiceProvider(o))), FactoryLifetime.Singleton);
businessServerProvider = _container.Resolve<IBusinessServerProvider>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using NewPlatform.Flexberry.ORM.ODataService.Extensions;
using NewPlatform.Flexberry.ORM.ODataService.WebApi.Extensions;
using Unity.AspNet.WebApi;
using ICSSoft.STORMNET.Business.Interfaces;
torres6093 marked this conversation as resolved.
Show resolved Hide resolved
using ICSSoft.Services;
#endif
#if NETCOREAPP
using NewPlatform.Flexberry.ORM.ODataService.Routing;
Expand Down Expand Up @@ -131,6 +133,11 @@ public virtual void ActODataService(Action<TestArgs> action)
using (HttpServer server = new HttpServer(config))
using (HttpClient client = new HttpClient(server, false) { BaseAddress = new Uri("http://localhost/odata/") })
{
container.RegisterFactory<IBusinessServerProvider>(new Func<IUnityContainer, object>(o => new BusinessServerProvider(new UnityServiceProvider(o))), FactoryLifetime.Singleton);
torres6093 marked this conversation as resolved.
Show resolved Hide resolved

// Base dependencies registration (dependencies from configuration)
UnityContainerRegistrations.Registration(container);

container.RegisterType<DataObjectEdmModelDependencies>(
new InjectionConstructor(
container.IsRegistered<IExportService>() ? container.Resolve<IExportService>() : null,
Expand Down
13 changes: 10 additions & 3 deletions Tests/NewPlatform.Flexberry.ORM.ODataService.Tests/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ namespace ODataServiceSample.AspNetCore
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NewPlatform.Flexberry;
using NewPlatform.Flexberry.Caching;
using NewPlatform.Flexberry.ORM.CurrentUserService;
using NewPlatform.Flexberry.ORM.ODataService.Extensions;
using NewPlatform.Flexberry.ORM.ODataService.Files;
using NewPlatform.Flexberry.ORM.ODataService.Model;
using NewPlatform.Flexberry.ORM.ODataService.Tests;
using NewPlatform.Flexberry.ORM.ODataService.WebApi.Extensions;
using NewPlatform.Flexberry.ORM.ODataServiceCore.Common.Exceptions;
using NewPlatform.Flexberry.ORM.ODataServiceCore.Extensions;
using NewPlatform.Flexberry.ORM.ODataServiceCore.Extensions;
Copy link
Contributor

Choose a reason for hiding this comment

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

В этом файле точно все добавленные юзинги нужны?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

using NewPlatform.Flexberry.Reports.ExportToExcel;
using NewPlatform.Flexberry.Security;
using NewPlatform.Flexberry.Services;
using Unity;
using Unity.Injection;
using LockService = NewPlatform.Flexberry.Services.LockService;
using LockService = NewPlatform.Flexberry.Services.LockService;

public class Startup
{
Expand All @@ -49,7 +53,10 @@ public Startup(IConfiguration configuration)
/// <param name="unityContainer">Unity container.</param>
public virtual void ConfigureContainer(IUnityContainer unityContainer)
{
// Configure Flexberry services via Unity.
// Base dependencies registration.
UnityContainerRegistrations.Registration(unityContainer);

// Configure Flexberry services via Unity.
var securityManager = new EmptySecurityManager();
Mock<IAuditService> mockAuditService = new Mock<IAuditService>();
IBusinessServerProvider businessServerProvider = unityContainer.Resolve<IBusinessServerProvider>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
namespace NewPlatform.Flexberry.ORM.ODataService.Tests
{
using ICSSoft.STORMNET.Business;
using ICSSoft.STORMNET.Business.Audit;
using ICSSoft.STORMNET.Business.Interfaces;
using ICSSoft.STORMNET.Security;
using Microsoft.AspNetCore.Http;
using NewPlatform.Flexberry.Caching;
using NewPlatform.Flexberry.ORM.CurrentUserService;
using NewPlatform.Flexberry.ORM.ODataService.Tests.CRUD.Read.Excel;
using NewPlatform.Flexberry.ORM.ODataService.Tests.Http;
using NewPlatform.Flexberry.Reports.ExportToExcel;
using NewPlatform.Flexberry.Security;
using System.Web;
using Unity;
using Unity.Injection;

/// <summary>
/// This static class is designed to register dependencies in the Unity container.
Copy link
Contributor

@Anisimova2020 Anisimova2020 Jun 26, 2023

Choose a reason for hiding this comment

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

Мне кажется, что в комментариях нужно сделать акцент, что это просто вспомогательный класс для тестов.

Также добавь в комментарии, что при удалении UnityConfigResolver для версии под дотнет 4.5 нужно определить нужность данного класса (пока что перерегистрировать нужно именно из-за него). Также упоминание "UnityConfigResolver" требуется, чтобы при удалении этого класса поиском наткнулись на это примечание.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

/// Created to eliminate code duplication for different dotnet versions.
/// </summary>
public static class UnityContainerRegistrations
{
/// <summary>
/// Method for base dependencies registration into Unity container.
/// </summary>
/// <param name="unityContainer">Unity container.</param>
public static void Registration(IUnityContainer unityContainer)
Copy link
Contributor

Choose a reason for hiding this comment

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

Пожалуй, стоит ещё отметить, какой код можно сделать только кодом, а какой и кодом и конфигом. Например, сначала тот, что и там и там, отметив комментарием. А потом тот, что только кодом теперь.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

{
unityContainer.RegisterType<IHttpContextAccessor, HttpContextAccessor>();
#if NETFRAMEWORK
unityContainer.RegisterType<ICurrentUser, WebHttpUser>(
new InjectionConstructor(unityContainer.Resolve<IHttpContextAccessor>()));
#endif
#if NETCOREAPP
unityContainer.RegisterType<ICurrentUser, WebHttpUser>();
#endif
unityContainer.RegisterType<IAuditService, AuditService>(
new InjectionConstructor(unityContainer.Resolve<ICurrentUser>()));

unityContainer.RegisterSingleton<IExportService, ExportExcelODataService>("Export");
Copy link
Contributor

Choose a reason for hiding this comment

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

Разве это не регистрация изначально закомментированного кода?

"
"

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

unityContainer.RegisterSingleton<IODataExportService, ExportExcel>();
unityContainer.RegisterSingleton<ISpreadsheetCustomizer, SpreadsheetCustomizer>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Аналогично.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

unityContainer.RegisterSingleton<IConfigResolver, ConfigResolver>();

unityContainer.RegisterSingleton<ICacheService, MemoryCacheService>(
new InjectionConstructor("defaultCacheForApplication", 3600));

unityContainer.RegisterSingleton<ISecurityManager, EmptySecurityManager>("securityManagerWithoutRightsCheck");

unityContainer.RegisterSingleton<IDataService, MSSQLDataService>(
"dataServiceForAuditAgentManagerAdapter",
new InjectionConstructor(
unityContainer.Resolve<ISecurityManager>("securityManagerWithoutRightsCheck"),
unityContainer.Resolve<IAuditService>(),
unityContainer.Resolve<IBusinessServerProvider>()),
new InjectionProperty(nameof(MSSQLDataService.CustomizationStringName), "DefConnStr"));

unityContainer.RegisterType<IDataService, MSSQLDataService>(
"dataServiceForSecurityManager",
new InjectionConstructor(
unityContainer.Resolve<ISecurityManager>("securityManagerWithoutRightsCheck"),
unityContainer.Resolve<IAuditService>(),
unityContainer.Resolve<IBusinessServerProvider>()),
Inject.Property(nameof(MSSQLDataService.CustomizationStringName), "DefConnStr"));

unityContainer.RegisterSingleton<ICacheService, MemoryCacheService>(
"cacheServiceForSecurityManager",
new InjectionConstructor("cacheForSecurityManager"));

unityContainer.RegisterSingleton<ICacheService, MemoryCacheService>(
"cacheServiceForAgentManager", new InjectionConstructor("cacheForAgentManager"));

unityContainer.RegisterSingleton<ISecurityManager, SecurityManager>(
new InjectionConstructor(
unityContainer.Resolve<IDataService>("dataServiceForSecurityManager"),
unityContainer.Resolve<ICacheService>("cacheServiceForSecurityManager")));

unityContainer.RegisterSingleton<IAgentManager, AgentManager>(
new InjectionConstructor(
unityContainer.Resolve<IDataService>("dataServiceForSecurityManager"),
unityContainer.Resolve<ICacheService>("cacheServiceForSecurityManager")));

unityContainer.RegisterSingleton<IPasswordHasher, Sha1PasswordHasher>();
}
}
}