Open ENikS opened 6 years ago
I deployed Unity.RegistrationByConvention package but it does not provide support for UWP or Core apps.
Who need registration by convention on core can use good combo with https://github.com/khellang/Scrutor and https://github.com/unitycontainer/microsoft-dependency-injection
Присылай PR, этот проект уже давно висит, не было времени разобраться
Да нет PR, это совет для тех кто хочет красивый fluent api и поддержку кора, можно скомбинировать Scrutor и microsoft di
PR - pull request, у тебя GitHub c русским интерфейсом?
? можно просто делать с помощью Scrutor что то подобное, вместо использования этой библиотеки
var collection = new ServiceCollection();
collection.Scan(scan => scan
.FromAssemblyOf<ITransientService>()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes => classes.AssignableTo<IScopedService>())
.As<IScopedService>()
.WithScopedLifetime());
var unityContainer = new UnityContainer();
var serviceProvider = unityContainer.ConfigureServices(collection);
Можно проще
var collection = new ServiceCollection();
collection.Scan(scan => scan
.FromAssemblyOf<ITransientService>()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes => classes.AssignableTo<IScopedService>())
.As<IScopedService>()
.WithScopedLifetime());
var serviceProvider = ServiceProvider.ConfigureServices(collection);
I use UWP and Unity 4.0.1... the registration by convention stuff is the reason I implemented Unity in the first place. But the behavior didn't function in Release mode due to having to use the .NET Native compiler and lack of reflection.
At the time I only needed a single Assembly, therefore doing this.GetType().GetTypeInfo().Assembly would give me the assembly to allow me to pass the assembly name to AllClasses.FromAssemblies().
Manually registering all assemblies at run time seems to work. I think for UWP, could we not safely allow for specifying assemblies to register and bring over the extension methods in Containers to RegisterTypes with mappings?
Specifying an assembly is easier than specifying every type you want to register.
If you'd like to send a PR I will consider the feature.
Need to find a way to scan installation folder in NET Core app here.