-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to inject into module initializer (#165)
* Add option to inject into module initializer * Change AutoInit into InitMode enum * Add unit tests for InitMode.ModuleLoad
- Loading branch information
Showing
10 changed files
with
178 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using AutoDI.AssemblyGenerator; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using ModuleLoadInjectionNamespace; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace AutoDI.Build.Tests | ||
{ | ||
[TestClass] | ||
public class ModuleLoadInjectionTests | ||
{ | ||
private static Assembly _testAssembly; | ||
|
||
[ClassInitialize] | ||
public static async Task Initialize(TestContext context) | ||
{ | ||
var gen = new Generator(); | ||
|
||
_testAssembly = (await gen.Execute()).SingleAssembly(); | ||
} | ||
|
||
[ClassCleanup] | ||
public static void Cleanup() | ||
{ | ||
DI.Dispose(_testAssembly); | ||
} | ||
|
||
[TestMethod] | ||
public void InitThrowsIfModuleLoadAssembly() | ||
{ | ||
try | ||
{ | ||
DI.Init(_testAssembly); | ||
} | ||
catch (TargetInvocationException e) | ||
when (e.InnerException is AlreadyInitializedException) | ||
{ | ||
return; | ||
} | ||
Assert.Fail($"Excepted {nameof(AlreadyInitializedException)}"); | ||
} | ||
|
||
[TestMethod] | ||
public void TryInitReturnsFalseOnFirstInvocation() | ||
{ | ||
Assert.IsFalse(DI.TryInit(_testAssembly)); | ||
} | ||
|
||
[TestMethod] | ||
public void LibraryDependenciesAreInjected() | ||
{ | ||
dynamic sut = _testAssembly.CreateInstance<ModuleLoadingLibrary>(); | ||
Assert.IsTrue(((object)sut.Service).Is<Service>()); | ||
} | ||
} | ||
} | ||
|
||
//<assembly /> | ||
//<ref: AutoDI /> | ||
//<weaver: AutoDI.Build.ProcessAssemblyTask /> | ||
//<raw:[assembly:AutoDI.Settings(InitMode = AutoDI.InitMode.ModuleLoad)] /> | ||
namespace ModuleLoadInjectionNamespace | ||
{ | ||
using AutoDI; | ||
|
||
public class ModuleLoadingLibrary | ||
{ | ||
public IService Service { get; } | ||
|
||
public ModuleLoadingLibrary([Dependency] IService service = null) | ||
{ | ||
Service = service; | ||
} | ||
} | ||
|
||
public interface IService { } | ||
|
||
public class Service : IService { } | ||
} | ||
//</assembly> |
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace AutoDI | ||
{ | ||
/// <summary> | ||
/// Controls when AutoDI is initialized | ||
/// </summary> | ||
public enum InitMode | ||
{ | ||
/// <summary> | ||
/// The container must be initialized manually. | ||
/// </summary> | ||
None, | ||
/// <summary> | ||
/// The container will be initialized on your program's entry point. | ||
/// Recommended for executable programs. | ||
/// </summary> | ||
EntryPoint, | ||
/// <summary> | ||
/// The container will be initialized on assembly module load. | ||
/// Recommended for libraries. | ||
/// </summary> | ||
ModuleLoad | ||
} | ||
} |
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