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;
using ICSSoft.STORMNET.Business;
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.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 Expand Up @@ -42,6 +50,54 @@ public static void RegisterTypes(IUnityContainer container)

// TODO: Register your type's mappings here.
// container.RegisterType<IProductRepository, ProductRepository>();

container.RegisterType<IAuditService, AuditService>();
container.RegisterSingleton<IExportService, ExportExcelODataService>("Export");
container.RegisterSingleton<IODataExportService, NewPlatform.Flexberry.ORM.ODataService.Tests.CRUD.Read.Excel.ExportExcel>();
container.RegisterSingleton<ISpreadsheetCustomizer, NewPlatform.Flexberry.ORM.ODataService.Tests.CRUD.Read.Excel.SpreadsheetCustomizer>();
container.RegisterSingleton<IConfigResolver, ConfigResolver>();

container.RegisterType<ICurrentUser, NewPlatform.Flexberry.ORM.ODataService.Tests.Http.WebHttpUser>();

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

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

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

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

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

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

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

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

container.RegisterSingleton<IPasswordHasher, Sha1PasswordHasher>();
}
}
}
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
58 changes: 56 additions & 2 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 Down Expand Up @@ -66,6 +70,56 @@ public virtual void ConfigureContainer(IUnityContainer unityContainer)
unityContainer.RegisterInstance(dataService);
unityContainer.RegisterInstance<ILockService>(new LockService(dataService));
unityContainer.RegisterInstance<ISecurityManager>(new EmptySecurityManager());

unityContainer.RegisterType<IAuditService, AuditService>();
Copy link
Contributor

@Anisimova2020 Anisimova2020 Jun 8, 2023

Choose a reason for hiding this comment

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

Посмотрела ORM. Там конструктору AuditService требуется ICurrentUser. Давай сразу это понятно пропишем по аналогии с MSSQLDataService.

Думаю, нужно иные классы по аналогии перепроверить.
Вот ветка: https://github.com/Flexberry/NewPlatform.Flexberry.ORM/tree/feature-239-use-proper-di.

unityContainer.RegisterSingleton<IExportService, ExportExcelODataService>("Export");
unityContainer.RegisterSingleton<IODataExportService, NewPlatform.Flexberry.ORM.ODataService.Tests.CRUD.Read.Excel.ExportExcel>();
unityContainer.RegisterSingleton<ISpreadsheetCustomizer, NewPlatform.Flexberry.ORM.ODataService.Tests.CRUD.Read.Excel.SpreadsheetCustomizer>();
unityContainer.RegisterSingleton<IConfigResolver, ConfigResolver>();

// ??? - без этого не давало в регистрацию MSSQLDataService ниже
unityContainer.RegisterType<ICurrentUser, NewPlatform.Flexberry.ORM.ODataService.Tests.Http.WebHttpUser>();

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"));

// Параметры типа bool задаются по умолчанию в конструкторах
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>();
}

// This method gets called by the runtime. Use this method to add services to the container.
Expand Down
Loading