Merge pull request #706 from thogil/dependency_modules

Added IDependencyModule to allow plugins to define IoC bindings
This commit is contained in:
Luke 2014-02-11 23:29:14 -05:00
commit c5f29c67ea
4 changed files with 56 additions and 1 deletions

View file

@ -35,7 +35,7 @@ namespace MediaBrowser.Common.Implementations
/// Class BaseApplicationHost /// Class BaseApplicationHost
/// </summary> /// </summary>
/// <typeparam name="TApplicationPathsType">The type of the T application paths type.</typeparam> /// <typeparam name="TApplicationPathsType">The type of the T application paths type.</typeparam>
public abstract class BaseApplicationHost<TApplicationPathsType> : IApplicationHost public abstract class BaseApplicationHost<TApplicationPathsType> : IApplicationHost, IDependencyContainer
where TApplicationPathsType : class, IApplicationPaths where TApplicationPathsType : class, IApplicationPaths
{ {
/// <summary> /// <summary>
@ -406,9 +406,30 @@ namespace MediaBrowser.Common.Implementations
IsoManager = new IsoManager(); IsoManager = new IsoManager();
RegisterSingleInstance(IsoManager); RegisterSingleInstance(IsoManager);
RegisterModules();
}); });
} }
private void RegisterModules()
{
var moduleTypes = GetExportTypes<IDependencyModule>();
foreach (var type in moduleTypes)
{
try
{
var instance = Activator.CreateInstance(type) as IDependencyModule;
if (instance != null)
instance.BindDependencies(this);
}
catch (Exception ex)
{
Logger.ErrorException("Error setting up dependency bindings for " + type.Name, ex);
}
}
}
protected virtual IFileSystem CreateFileSystemManager() protected virtual IFileSystem CreateFileSystemManager()
{ {
return new CommonFileSystem(Logger, true); return new CommonFileSystem(Logger, true);
@ -479,6 +500,11 @@ namespace MediaBrowser.Common.Implementations
} }
} }
void IDependencyContainer.RegisterSingleInstance<T>(T obj, bool manageLifetime)
{
RegisterSingleInstance(obj, manageLifetime);
}
/// <summary> /// <summary>
/// Registers the specified obj. /// Registers the specified obj.
/// </summary> /// </summary>
@ -501,6 +527,11 @@ namespace MediaBrowser.Common.Implementations
} }
} }
void IDependencyContainer.RegisterSingleInstance<T>(Func<T> func)
{
RegisterSingleInstance(func);
}
/// <summary> /// <summary>
/// Registers the single instance. /// Registers the single instance.
/// </summary> /// </summary>
@ -512,6 +543,11 @@ namespace MediaBrowser.Common.Implementations
Container.RegisterSingle(func); Container.RegisterSingle(func);
} }
void IDependencyContainer.Register(Type typeInterface, Type typeImplementation)
{
Container.Register(typeInterface, typeImplementation);
}
/// <summary> /// <summary>
/// Resolves this instance. /// Resolves this instance.
/// </summary> /// </summary>

View file

@ -152,4 +152,15 @@ namespace MediaBrowser.Common
/// <returns>System.Object.</returns> /// <returns>System.Object.</returns>
object CreateInstance(Type type); object CreateInstance(Type type);
} }
public interface IDependencyContainer
{
void RegisterSingleInstance<T>(T obj, bool manageLifetime = true)
where T : class;
void RegisterSingleInstance<T>(Func<T> func)
where T : class;
void Register(Type typeInterface, Type typeImplementation);
}
} }

View file

@ -79,6 +79,7 @@
<Compile Include="Net\IWebSocketServer.cs" /> <Compile Include="Net\IWebSocketServer.cs" />
<Compile Include="Net\MimeTypes.cs" /> <Compile Include="Net\MimeTypes.cs" />
<Compile Include="Net\WebSocketConnectEventArgs.cs" /> <Compile Include="Net\WebSocketConnectEventArgs.cs" />
<Compile Include="Plugins\IDependencyModule.cs" />
<Compile Include="Plugins\IPlugin.cs" /> <Compile Include="Plugins\IPlugin.cs" />
<Compile Include="Progress\ActionableProgress.cs" /> <Compile Include="Progress\ActionableProgress.cs" />
<Compile Include="ScheduledTasks\IScheduledTask.cs" /> <Compile Include="ScheduledTasks\IScheduledTask.cs" />

View file

@ -0,0 +1,7 @@
namespace MediaBrowser.Common.Plugins
{
public interface IDependencyModule
{
void BindDependencies(IDependencyContainer container);
}
}