Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed IServiceProviderType feature. #246

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 7 additions & 34 deletions src/CatLib.Core.Tests/CatLib/ApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,8 @@ public void Register()
}
}

public class TestServiceProvider : IServiceProvider, IServiceProviderType
public class TestServiceProvider : IServiceProvider
{
/// <summary>
/// 提供者基础类型.
/// </summary>
public Type BaseType
{
get { return typeof(TestBaseServiceProvider); }
}

/// <summary>
/// 服务提供者初始化.
/// </summary>
Expand Down Expand Up @@ -86,27 +78,6 @@ public void RepeatInitTest()
app.Init();
}

[TestMethod]
public void TestBaseTypeProvider()
{
var app = new Application();
app.Bootstrap();
app.Register(new TestServiceProvider());

RuntimeException ex = null;
try
{
app.Init();
}
catch (RuntimeException e)
{
ex = e;
}

Assert.AreNotEqual(null, ex);
Assert.AreEqual("TestServiceProvider", ex.Message);
}

/// <summary>
/// 未经引导的初始化.
/// </summary>
Expand Down Expand Up @@ -311,11 +282,12 @@ public void RegisteredIllegalType()
public void RepeatRegister()
{
var app = MakeApplication();
app.Register(new ProviderTest1());
var provider = new ProviderTest1();
app.Register(provider);

ExceptionAssert.Throws<RuntimeException>(() =>
{
app.Register(new ProviderTest1());
app.Register(provider);
});
}

Expand Down Expand Up @@ -399,11 +371,12 @@ public void TestRepeatRegister()
});

app.Bootstrap();
app.Register(new ProviderTest1());
var provider = new ProviderTest1();
app.Register(provider);

ExceptionAssert.Throws<RuntimeException>(() =>
{
app.Register(new ProviderTest1());
app.Register(provider);
});
}

Expand Down
29 changes: 6 additions & 23 deletions src/CatLib.Core/CatLib/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ public class Application : CatLibContainer, IApplication
/// </summary>
private static string version;

/// <summary>
/// All of the registered service providers.
/// </summary>
private readonly List<IServiceProvider> serviceProviders;

/// <summary>
/// The types of the loaded service providers.
/// </summary>
private readonly HashSet<Type> loadedProviders;
private readonly List<IServiceProvider> loadedProviders;

/// <summary>
/// The main thread id.
Expand Down Expand Up @@ -79,8 +74,7 @@ public class Application : CatLibContainer, IApplication
/// <param name="global">True if sets the instance to <see cref="App"/> facade.</param>
public Application(bool global = true)
{
serviceProviders = new List<IServiceProvider>();
loadedProviders = new HashSet<Type>();
loadedProviders = new List<IServiceProvider>();

mainThreadId = Thread.CurrentThread.ManagedThreadId;
RegisterBaseBindings();
Expand Down Expand Up @@ -289,7 +283,7 @@ public virtual void Init()
Dispatch(new BeforeInitEventArgs(this));
Process = StartProcess.Initing;

foreach (var provider in serviceProviders)
foreach (var provider in loadedProviders)
{
InitProvider(provider);
}
Expand All @@ -314,7 +308,7 @@ public virtual void Register(IServiceProvider provider, bool force = false)
throw new LogicException($"Provider [{provider.GetType()}] is already register.");
}

loadedProviders.Remove(GetProviderBaseType(provider));
loadedProviders.Remove(provider);
}

if (Process == StartProcess.Initing)
Expand Down Expand Up @@ -344,8 +338,7 @@ public virtual void Register(IServiceProvider provider, bool force = false)
registering = false;
}

serviceProviders.Add(provider);
loadedProviders.Add(GetProviderBaseType(provider));
loadedProviders.Add(provider);

if (inited)
{
Expand All @@ -357,7 +350,7 @@ public virtual void Register(IServiceProvider provider, bool force = false)
public bool IsRegistered(IServiceProvider provider)
{
Guard.Requires<ArgumentNullException>(provider != null);
return loadedProviders.Contains(GetProviderBaseType(provider));
return loadedProviders.Contains(provider);
}

/// <inheritdoc />
Expand Down Expand Up @@ -413,16 +406,6 @@ protected override void GuardConstruct(string method)
base.GuardConstruct(method);
}

/// <summary>
/// Get the base type from service provider.
/// </summary>
/// <param name="provider">The service provider.</param>
/// <returns>Base type for service provider.</returns>
private static Type GetProviderBaseType(IServiceProvider provider)
{
return !(provider is IServiceProviderType providerType) ? provider.GetType() : providerType.BaseType;
}

/// <summary>
/// Register the core service aliases.
/// </summary>
Expand Down
26 changes: 0 additions & 26 deletions src/CatLib.Core/CatLib/IServiceProviderType.cs

This file was deleted.