Skip to content

Commit

Permalink
resolved #97
Browse files Browse the repository at this point in the history
  • Loading branch information
ymh199478 committed Dec 4, 2018
1 parent 405a4cc commit 94ab027
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 7 deletions.
105 changes: 105 additions & 0 deletions src/CatLib.Core.Tests/CatLib/ApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,111 @@ public void TestRegisterProcessMake()
app.Register(new TestRegisterProcessMakeServiceProvider());
}

public class TestExistedBoostrap : IBootstrap
{
public void Bootstrap()
{

}
}

[TestMethod]
[ExpectedException(typeof(LogicException))]
public void TestExistBoostrap()
{
var app = new Application();
var boostrap = new TestExistedBoostrap();
app.Bootstrap(boostrap, boostrap);
}

private static int assertValue = 0;

public class OrderAssertClass : IBootstrap, IServiceProvider
{
private readonly int assert;
public OrderAssertClass(int assert)
{
this.assert = assert;
}

public void Bootstrap()
{
Assert.AreEqual(assert, assertValue++);
}

/// <summary>
/// 服务提供者初始化
/// </summary>
public void Init()
{
Bootstrap();
}

/// <summary>
/// 当注册服务提供者
/// </summary>
public void Register()
{

}
}

public class OrderAssertClassSub : OrderAssertClass
{
public OrderAssertClassSub(int assert)
:base(assert)
{

}
}

[Priority(0)]
public class OrderFirstClass : IBootstrap, IServiceProvider
{
public void Bootstrap()
{
Assert.AreEqual(0, assertValue);
}

/// <summary>
/// 服务提供者初始化
/// </summary>
public void Init()
{
Bootstrap();
}

/// <summary>
/// 当注册服务提供者
/// </summary>
public void Register()
{

}
}

[TestMethod]
public void TestBoostrapOrder()
{
assertValue = 0;
var app = new Application();
app.Bootstrap(new OrderAssertClass(0), new OrderFirstClass(), new OrderAssertClass(1));
Assert.AreEqual(2, assertValue);
}

[TestMethod]
public void TestProviderOrder()
{
assertValue = 0;
var app = new Application();
app.Bootstrap();
app.Register(new OrderAssertClass(0));
app.Register(new OrderFirstClass());
app.Register(new OrderAssertClassSub(1));
app.Init();
Assert.AreEqual(2, assertValue);
}

private Application MakeApplication()
{
var app = new Application();
Expand Down
33 changes: 26 additions & 7 deletions src/CatLib.Core/CatLib/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public enum StartProcess
/// <summary>
/// 服务提供者
/// </summary>
private readonly SortSet<IServiceProvider, int> serviceProviders = new SortSet<IServiceProvider, int>();
private readonly List<KeyValuePair<IServiceProvider, int>> serviceProviders =
new List<KeyValuePair<IServiceProvider, int>>();

/// <summary>
/// 注册服务提供者
Expand Down Expand Up @@ -198,18 +199,31 @@ public virtual void Bootstrap(params IBootstrap[] bootstraps)
Trigger(ApplicationEvents.OnBootstrap, this);
Process = StartProcess.Bootstrapping;

var sorting = new SortSet<IBootstrap, int>();
var sorting = new List<KeyValuePair<IBootstrap, int>>();
var existed = new HashSet<IBootstrap>();

foreach (var bootstrap in bootstraps)
{
if (bootstrap != null)
if (bootstrap == null)
{
sorting.Add(bootstrap, GetPriority(bootstrap.GetType(), nameof(IBootstrap.Bootstrap)));
continue;
}

if (existed.Contains(bootstrap))
{
throw new LogicException($"The bootstrap already exists : {bootstrap}");
}

existed.Add(bootstrap);
sorting.Add(new KeyValuePair<IBootstrap, int>(bootstrap,
GetPriority(bootstrap.GetType(), nameof(IBootstrap.Bootstrap))));
}

foreach (var bootstrap in sorting)
sorting.Sort((left, right) => left.Value.CompareTo(right.Value));

foreach (var kv in sorting)
{
var bootstrap = kv.Key;
var allow = TriggerHalt(ApplicationEvents.Bootstrapping, bootstrap) == null;
if (bootstrap != null && allow)
{
Expand Down Expand Up @@ -250,9 +264,11 @@ protected IEnumerator CoroutineInit()
Trigger(ApplicationEvents.OnInit, this);
Process = StartProcess.Initing;

serviceProviders.Sort((left, right) => left.Value.CompareTo(right.Value));

foreach (var provider in serviceProviders)
{
yield return InitProvider(provider);
yield return InitProvider(provider.Key);
}

inited = true;
Expand Down Expand Up @@ -312,7 +328,10 @@ protected IEnumerator CoroutineRegister(IServiceProvider provider)
{
registering = false;
}
serviceProviders.Add(provider, GetPriority(provider.GetType(), nameof(IServiceProvider.Init)));

serviceProviders.Add(
new KeyValuePair<IServiceProvider, int>(provider,
GetPriority(provider.GetType(), nameof(IServiceProvider.Init))));
serviceProviderTypes.Add(GetProviderBaseType(provider));

if (inited)
Expand Down

0 comments on commit 94ab027

Please sign in to comment.