Skip to content

Commit

Permalink
从对象容器创建服务提供者,支持传入另一个服务提供者作为内部提供者
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 29, 2024
1 parent 685f9c4 commit 2c3f096
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
4 changes: 0 additions & 4 deletions NewLife.Core/Model/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public class Host : DisposeBase, IHost
/// <summary>服务提供者</summary>
public IServiceProvider ServiceProvider { get; set; }

//private readonly IList<Type> _serviceTypes = new List<Type>();
/// <summary>服务集合</summary>
public IList<IHostedService> Services { get; } = [];
#endregion
Expand Down Expand Up @@ -125,9 +124,6 @@ protected override void Dispose(Boolean disposing)
/// <typeparam name="TService"></typeparam>
public void Add<TService>() where TService : class, IHostedService
{
//var type = typeof(TService);
//_serviceTypes.Add(type);

// 把服务类型注册到容器中,以便后续获取
var ioc = (ServiceProvider as ServiceProvider)?.Container ?? ObjectContainer.Current;
//ioc.TryAddTransient(type, type);
Expand Down
17 changes: 10 additions & 7 deletions NewLife.Core/Model/ObjectContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ static ObjectContainer()
{
var ioc = new ObjectContainer();
Current = ioc;
Provider = ioc.BuildServiceProvider() ?? new ServiceProvider(ioc);
Provider = ioc.BuildServiceProvider();
}
#endregion

#region 属性
private readonly IList<IObject> _list = new List<IObject>();
private readonly IList<IObject> _list = [];
/// <summary>服务集合</summary>
public IList<IObject> Services => _list;

Expand Down Expand Up @@ -126,7 +126,7 @@ public virtual Object Resolve(IObject item, IServiceProvider? serviceProvider)
if (item.Lifetime == ObjectLifetime.Singleton && map?.Instance != null) return map.Instance;

var type = item.ImplementationType ?? item.ServiceType;
serviceProvider ??= new ServiceProvider(this);
serviceProvider ??= new ServiceProvider(this, null);
switch (item.Lifetime)
{
case ObjectLifetime.Singleton:
Expand Down Expand Up @@ -287,13 +287,13 @@ public ServiceDescriptor(Type serviceType, Type? implementationType, Object? ins
#endregion
}

internal class ServiceProvider : IServiceProvider
internal class ServiceProvider(IObjectContainer container, IServiceProvider? innerServiceProvider) : IServiceProvider
{
private readonly IObjectContainer _container;
private readonly IObjectContainer _container = container;
/// <summary>容器</summary>
public IObjectContainer Container => _container;

public ServiceProvider(IObjectContainer container) => _container = container;
public IServiceProvider? InnerServiceProvider { get; set; } = innerServiceProvider;

public Object? GetService(Type serviceType)
{
Expand All @@ -311,6 +311,9 @@ internal class ServiceProvider : IServiceProvider
});
}

return _container.Resolve(serviceType, this);
var service = _container.Resolve(serviceType, this);
if (service != null) return service;

return InnerServiceProvider?.GetService(serviceType);
}
}
12 changes: 9 additions & 3 deletions NewLife.Core/Model/ObjectContainerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,22 @@ public static IObjectContainer TryAddTransient<TService>(this IObjectContainer c

#region 构建
/// <summary>从对象容器创建服务提供者</summary>
/// <param name="container"></param>
/// <param name="container">对象容器</param>
/// <returns></returns>
public static IServiceProvider BuildServiceProvider(this IObjectContainer container) => BuildServiceProvider(container, null);

/// <summary>从对象容器创建服务提供者,支持传入另一个服务提供者作为内部提供者</summary>
/// <param name="container">对象容器</param>
/// <param name="innerServiceProvider">内部服务提供者,常用于NETCore的IoC</param>
/// <returns></returns>
public static IServiceProvider BuildServiceProvider(this IObjectContainer container)
public static IServiceProvider BuildServiceProvider(this IObjectContainer container, IServiceProvider? innerServiceProvider)
{
if (container == null) throw new ArgumentNullException(nameof(container));

var provider = container.Resolve(typeof(IServiceProvider)) as IServiceProvider;
if (provider != null) return provider;

var provider2 = new ServiceProvider(container);
var provider2 = new ServiceProvider(container, innerServiceProvider);

container.TryAddSingleton(provider2);

Expand Down

0 comments on commit 2c3f096

Please sign in to comment.