修改了Ioc相同类型实例化了多个对象的问题

This commit is contained in:
fengjiayi
2024-09-15 19:48:27 +08:00
parent 19247b5afe
commit fe2ccaf74c
8 changed files with 199 additions and 112 deletions

View File

@@ -70,6 +70,21 @@ namespace Serein.Library.Utils
return this;
}
public ISereinIoc RegisterInstantiate(object instantiate)
{
//var type = instantiate.GetType();
if (!_typeMappings.TryGetValue(instantiate.GetType().FullName,out var type))
{
_typeMappings[type.FullName] = type;
}
if(!_dependencies.TryGetValue(type.FullName, out var instancei))
{
_dependencies[type.FullName] = Activator.CreateInstance(type);
}
// _dependencies.AddOrUpdate(type.FullName,s => instantiate, (s,o) => instantiate);
return this;
}
public ISereinIoc Register(Type type, params object[] parameters)
{
@@ -127,21 +142,20 @@ namespace Serein.Library.Utils
}
public ISereinIoc Build()
{
// 遍历已注册类型
foreach (var type in _typeMappings.Values)
{
// 如果没有创建实例,则创建对应的实例
if(!_dependencies.ContainsKey(type.FullName))
{
_dependencies[type.FullName] = Activator.CreateInstance(type);
}
}
// 注入实例的依赖项
foreach (var instance in _dependencies.Values)
{
InjectDependencies(instance); // 替换占位符
}
@@ -155,7 +169,7 @@ namespace Serein.Library.Utils
{
var instance = Activator.CreateInstance(controllerType, parameters);
if(instance != null)
{
{
InjectDependencies(instance);
}
return instance;
@@ -285,6 +299,7 @@ namespace Serein.Library.Utils
return this;
}
#endregion
}