Skip to content

Commit 94ab027

Browse files
committed
resolved #97
1 parent 405a4cc commit 94ab027

File tree

2 files changed

+131
-7
lines changed

2 files changed

+131
-7
lines changed

src/CatLib.Core.Tests/CatLib/ApplicationTests.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,111 @@ public void TestRegisterProcessMake()
481481
app.Register(new TestRegisterProcessMakeServiceProvider());
482482
}
483483

484+
public class TestExistedBoostrap : IBootstrap
485+
{
486+
public void Bootstrap()
487+
{
488+
489+
}
490+
}
491+
492+
[TestMethod]
493+
[ExpectedException(typeof(LogicException))]
494+
public void TestExistBoostrap()
495+
{
496+
var app = new Application();
497+
var boostrap = new TestExistedBoostrap();
498+
app.Bootstrap(boostrap, boostrap);
499+
}
500+
501+
private static int assertValue = 0;
502+
503+
public class OrderAssertClass : IBootstrap, IServiceProvider
504+
{
505+
private readonly int assert;
506+
public OrderAssertClass(int assert)
507+
{
508+
this.assert = assert;
509+
}
510+
511+
public void Bootstrap()
512+
{
513+
Assert.AreEqual(assert, assertValue++);
514+
}
515+
516+
/// <summary>
517+
/// 服务提供者初始化
518+
/// </summary>
519+
public void Init()
520+
{
521+
Bootstrap();
522+
}
523+
524+
/// <summary>
525+
/// 当注册服务提供者
526+
/// </summary>
527+
public void Register()
528+
{
529+
530+
}
531+
}
532+
533+
public class OrderAssertClassSub : OrderAssertClass
534+
{
535+
public OrderAssertClassSub(int assert)
536+
:base(assert)
537+
{
538+
539+
}
540+
}
541+
542+
[Priority(0)]
543+
public class OrderFirstClass : IBootstrap, IServiceProvider
544+
{
545+
public void Bootstrap()
546+
{
547+
Assert.AreEqual(0, assertValue);
548+
}
549+
550+
/// <summary>
551+
/// 服务提供者初始化
552+
/// </summary>
553+
public void Init()
554+
{
555+
Bootstrap();
556+
}
557+
558+
/// <summary>
559+
/// 当注册服务提供者
560+
/// </summary>
561+
public void Register()
562+
{
563+
564+
}
565+
}
566+
567+
[TestMethod]
568+
public void TestBoostrapOrder()
569+
{
570+
assertValue = 0;
571+
var app = new Application();
572+
app.Bootstrap(new OrderAssertClass(0), new OrderFirstClass(), new OrderAssertClass(1));
573+
Assert.AreEqual(2, assertValue);
574+
}
575+
576+
[TestMethod]
577+
public void TestProviderOrder()
578+
{
579+
assertValue = 0;
580+
var app = new Application();
581+
app.Bootstrap();
582+
app.Register(new OrderAssertClass(0));
583+
app.Register(new OrderFirstClass());
584+
app.Register(new OrderAssertClassSub(1));
585+
app.Init();
586+
Assert.AreEqual(2, assertValue);
587+
}
588+
484589
private Application MakeApplication()
485590
{
486591
var app = new Application();

src/CatLib.Core/CatLib/Application.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public enum StartProcess
9090
/// <summary>
9191
/// 服务提供者
9292
/// </summary>
93-
private readonly SortSet<IServiceProvider, int> serviceProviders = new SortSet<IServiceProvider, int>();
93+
private readonly List<KeyValuePair<IServiceProvider, int>> serviceProviders =
94+
new List<KeyValuePair<IServiceProvider, int>>();
9495

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

201-
var sorting = new SortSet<IBootstrap, int>();
202+
var sorting = new List<KeyValuePair<IBootstrap, int>>();
203+
var existed = new HashSet<IBootstrap>();
202204

203205
foreach (var bootstrap in bootstraps)
204206
{
205-
if (bootstrap != null)
207+
if (bootstrap == null)
206208
{
207-
sorting.Add(bootstrap, GetPriority(bootstrap.GetType(), nameof(IBootstrap.Bootstrap)));
209+
continue;
208210
}
211+
212+
if (existed.Contains(bootstrap))
213+
{
214+
throw new LogicException($"The bootstrap already exists : {bootstrap}");
215+
}
216+
217+
existed.Add(bootstrap);
218+
sorting.Add(new KeyValuePair<IBootstrap, int>(bootstrap,
219+
GetPriority(bootstrap.GetType(), nameof(IBootstrap.Bootstrap))));
209220
}
210221

211-
foreach (var bootstrap in sorting)
222+
sorting.Sort((left, right) => left.Value.CompareTo(right.Value));
223+
224+
foreach (var kv in sorting)
212225
{
226+
var bootstrap = kv.Key;
213227
var allow = TriggerHalt(ApplicationEvents.Bootstrapping, bootstrap) == null;
214228
if (bootstrap != null && allow)
215229
{
@@ -250,9 +264,11 @@ protected IEnumerator CoroutineInit()
250264
Trigger(ApplicationEvents.OnInit, this);
251265
Process = StartProcess.Initing;
252266

267+
serviceProviders.Sort((left, right) => left.Value.CompareTo(right.Value));
268+
253269
foreach (var provider in serviceProviders)
254270
{
255-
yield return InitProvider(provider);
271+
yield return InitProvider(provider.Key);
256272
}
257273

258274
inited = true;
@@ -312,7 +328,10 @@ protected IEnumerator CoroutineRegister(IServiceProvider provider)
312328
{
313329
registering = false;
314330
}
315-
serviceProviders.Add(provider, GetPriority(provider.GetType(), nameof(IServiceProvider.Init)));
331+
332+
serviceProviders.Add(
333+
new KeyValuePair<IServiceProvider, int>(provider,
334+
GetPriority(provider.GetType(), nameof(IServiceProvider.Init))));
316335
serviceProviderTypes.Add(GetProviderBaseType(provider));
317336

318337
if (inited)

0 commit comments

Comments
 (0)