-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: feature-239-use-proper-di
Are you sure you want to change the base?
Changes from all commits
20c2596
e98f96a
961acfb
8ed8b3d
69973bf
dbe76d4
cebb4eb
9866616
a18a1ed
89bc458
18a9037
e4dbd5d
5d6d471
77158d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
using System; | ||
|
||
using Unity; | ||
|
||
namespace NewPlatform.Flexberry.ORM.ODataService.Tests | ||
{ | ||
using System; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для чего здесь юзинги добавлены? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
using Unity; | ||
|
||
/// <summary> | ||
/// Specifies the Unity configuration for the main container. | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
namespace NewPlatform.Flexberry.ORM.ODataService.Tests | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Configuration; | ||
using System.Globalization; | ||
using System.Linq; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. После System-юзингов желательно пустую строчку. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
||
using Microsoft.Practices.Unity.Configuration; | ||
using Unity; | ||
using Unity.Injection; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// Class for checking that a dependency registered in the code can be overriden from the config. | ||
/// </summary> | ||
public class CheckLoadConfigurationTest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нужны комментарии к классу тестов. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
{ | ||
[Fact] | ||
public void CheckIfLoadConfigurationWorks() | ||
{ | ||
using var container = new UnityContainer(); | ||
|
||
// Default culture | ||
container.RegisterType<IFormatProvider, CultureInfo>( | ||
new InjectionConstructor("en-US")); | ||
|
||
// This type is registered in code only. Just to prove its registration survives. | ||
container.RegisterType<IList, ArrayList>(); | ||
|
||
Assert.Equal("$1,234.56", GetFormattedSum(container.Resolve<IFormatProvider>(), 1234.56M)); | ||
string beforeLoad = GetCodeRegistration(container); | ||
|
||
// Override with the config file | ||
if (ConfigurationManager.GetSection("unity") != null) | ||
{ | ||
container.LoadConfiguration(); | ||
Assert.Equal("1.234,56 kr.", GetFormattedSum(container.Resolve<IFormatProvider>(), 1234.56M)); | ||
|
||
string afterLoad = GetCodeRegistration(container); | ||
|
||
Assert.Equal(beforeLoad, afterLoad); | ||
} | ||
} | ||
|
||
private static string GetFormattedSum(IFormatProvider fp, decimal money) | ||
{ | ||
var fi = fp.GetFormat(typeof(NumberFormatInfo)) as NumberFormatInfo; | ||
return string.Format(fi, "{0:C}", money); | ||
} | ||
|
||
private static string GetCodeRegistration(IUnityContainer container) | ||
{ | ||
var reg = container.Registrations.Single(r => r.RegisteredType == typeof(IList)); | ||
return reg.MappedToType.Name; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
#if NETCOREAPP | ||
namespace NewPlatform.Flexberry.ORM.ODataService.Tests | ||
{ | ||
using System; | ||
using System.IO; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.Practices.Unity.Configuration; | ||
using Unity; | ||
using Unity.Microsoft.DependencyInjection; | ||
|
||
|
@@ -26,14 +26,17 @@ public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStar | |
/// <inheritdoc/> | ||
protected override IWebHostBuilder CreateWebHostBuilder() | ||
{ | ||
if (_unityContainer == null) | ||
{ | ||
throw new Exception("Unity.IUnityContainer is not defined"); | ||
} | ||
// Some tests do not need separate customized containers. | ||
IUnityContainer localContainer = _unityContainer ?? new UnityContainer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Стоит добавить комментарий, в каком случае может потребоваться создавать контейнер прямо здесь. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
UnityContainerRegistrations.BSProviderRegistration(localContainer); | ||
|
||
localContainer.LoadConfiguration(); | ||
// Base dependencies registration (Uncomment if you want to use code instead of config for registrations) | ||
UnityContainerRegistrations.Registration(localContainer); | ||
|
||
string contentRootDirectory = Directory.GetCurrentDirectory(); | ||
var webHostBuilder = new WebHostBuilder() | ||
.UseUnityServiceProvider(_unityContainer) | ||
.UseUnityServiceProvider(localContainer) | ||
.UseContentRoot(contentRootDirectory) | ||
.UseStartup<TestStartup>(); | ||
return webHostBuilder; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мне вот эта регистрация очень интересна, проходит ли корректно. Можешь убрать все регистрации в коде, потом сделать получение dataServiceForAuditAgentManagerAdapter. Мне очень интересно, как оно разрешает конструктор "BusinessServerProvider(IServiceProvider serviceProvider)". Нужно под дебагом посмотреть, инициализируется ли приватное свойство "serviceProvider".
Дело в том, что регистрация "unityContainer.RegisterFactory(new Func<IUnityContainer, object>(o => new BusinessServerProvider(new UnityServiceProvider(o))), FactoryLifetime.Singleton);" была наверчена от того, что конфиг в теории не должен был дать это нормально разрешить. Соответственно, если это работает, то круто, об этом нужно сообщить мне, я включу в свои документы. Если это не работает, а срабатывало только от того, что юнити так сильно не проверяло, то нужно оставить в конфиге только работающее.
Либо возможно работает, потому что сейчас в коде фабрика дорегистрируется IBusinessServerProvider как фабрика. Это тоже хороший вариант, о котором нужно мне сказать.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done