Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seperate register & execute Bootstrapper tasks
- Loading branch information
Chris van de Steeg
committed
Jun 23, 2017
1 parent
7c3f0f2
commit b186a5c
Showing
5 changed files
with
50 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using BoC.Helpers; | ||
using BoC.InversionOfControl; | ||
|
||
namespace BoC.Tasks | ||
{ | ||
public class Bootstrapper | ||
{ | ||
private readonly IDependencyResolver dependencyResolver; | ||
private readonly IAppDomainHelper[] appDomainHelpers; | ||
private static object bootstrapper_lock = new object(); | ||
public bool Executed { get; set; } | ||
|
||
public Bootstrapper(IDependencyResolver dependencyResolver, IAppDomainHelper[] appDomainHelpers) | ||
public Bootstrapper(IDependencyResolver dependencyResolver) | ||
{ | ||
this.dependencyResolver = dependencyResolver; | ||
this.appDomainHelpers = appDomainHelpers; | ||
} | ||
|
||
ICollection<Func<Type, bool>> taskFilters = new List<Func<Type, bool>>() | ||
public static readonly ICollection<Func<Type, bool>> TaskFilters = new List<Func<Type, bool>>() | ||
{ | ||
type => typeof(IBootstrapperTask).IsAssignableFrom(type), | ||
type => !type.IsAbstract, | ||
type => type.IsClass | ||
}; | ||
|
||
private void RegisterAllTasks() | ||
public virtual void Run() | ||
{ | ||
var tasks = appDomainHelpers.SelectMany(helper => helper.GetTypes(t => taskFilters.All(func => func(t)))); | ||
foreach (var t in tasks) | ||
if (Executed) | ||
return; | ||
lock (bootstrapper_lock) | ||
{ | ||
dependencyResolver.RegisterType(typeof(IBootstrapperTask), t); | ||
} | ||
} | ||
if (Executed) | ||
return; | ||
|
||
private void ExecuteTasks() | ||
{ | ||
var tasks = dependencyResolver.ResolveAll<IBootstrapperTask>(); | ||
foreach (var task in tasks) | ||
{ | ||
task.Execute(); | ||
var tasks = dependencyResolver.ResolveAll<IBootstrapperTask>(); | ||
foreach (var task in tasks) | ||
{ | ||
task.Execute(); | ||
} | ||
Executed = true; | ||
} | ||
} | ||
|
||
public void Run() | ||
{ | ||
RegisterAllTasks(); | ||
ExecuteTasks(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using BoC.Helpers; | ||
using BoC.InversionOfControl; | ||
|
||
namespace BoC.Tasks | ||
{ | ||
public class RegisterBootstrapTasks: IContainerInitializer | ||
{ | ||
private readonly IDependencyResolver dependencyResolver; | ||
private readonly IAppDomainHelper[] appDomainHelpers; | ||
|
||
public RegisterBootstrapTasks(IDependencyResolver dependencyResolver, IAppDomainHelper[] appDomainHelpers) | ||
{ | ||
this.dependencyResolver = dependencyResolver; | ||
this.appDomainHelpers = appDomainHelpers; | ||
} | ||
|
||
public void Execute() | ||
{ | ||
var tasks = appDomainHelpers.SelectMany(helper => helper.GetTypes(t => Bootstrapper.TaskFilters.All(func => func(t)))); | ||
foreach (var t in tasks) | ||
{ | ||
dependencyResolver.RegisterType(typeof(IBootstrapperTask), t); | ||
} | ||
dependencyResolver.RegisterSingleton<Bootstrapper, Bootstrapper>(); | ||
} | ||
} | ||
} |