Last active
December 23, 2015 21:59
-
-
Save aikmeng/6699838 to your computer and use it in GitHub Desktop.
Compose modules using Ninject
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
namespace ExperimentNinjectComposition | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
IKernel kernel = new StandardKernel( | |
new ExperimentNinjectComposition.Core.Module(), | |
new ExperimentNinjectComposition.ModuleA.Module(), | |
new ExperimentNinjectComposition.ModuleB.Module()); | |
Console.WriteLine(kernel.Get<IModuleADoSomething>().DoSomething()); | |
Console.WriteLine(kernel.Get<IModuleBDoSomething>().DoSomething()); | |
} | |
} | |
} |
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
namespace ExperimentNinjectComposition.Core.Interface | |
{ | |
public interface IDateTimeProvider | |
{ | |
DateTime GetCurrentUtcDate(); | |
} | |
} | |
namespace ExperimentNinjectComposition.Core.Interface | |
{ | |
public interface IDoSomething | |
{ | |
string DoSomething(); | |
} | |
} | |
namespace ExperimentNinjectComposition.Core | |
{ | |
public class DateTimeProvider : IDateTimeProvider | |
{ | |
public DateTime GetCurrentUtcDate() | |
{ | |
return DateTime.UtcNow.Date; | |
} | |
} | |
} | |
namespace ExperimentNinjectComposition.Core | |
{ | |
public class Module : Ninject.Modules.NinjectModule | |
{ | |
public override void Load() | |
{ | |
Bind<IDateTimeProvider>().To<DateTimeProvider>(); | |
} | |
} | |
} |
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
namespace ExperimentNinjectComposition.ModuleA.Interface | |
{ | |
public interface IModuleADoSomething : IDoSomething | |
{ | |
} | |
} | |
namespace ExperimentNinjectComposition.ModuleA | |
{ | |
public class ModuleADoSomething : IModuleADoSomething | |
{ | |
private readonly IDateTimeProvider _dateTimeProvider; | |
public ModuleADoSomething(IDateTimeProvider dateTimeProvider) | |
{ | |
_dateTimeProvider = dateTimeProvider; | |
} | |
public string DoSomething() | |
{ | |
return "Current UTC Date (Module A): " + _dateTimeProvider.GetCurrentUtcDate(); | |
} | |
} | |
} | |
namespace ExperimentNinjectComposition.ModuleA | |
{ | |
public class Module : Ninject.Modules.NinjectModule | |
{ | |
public override void Load() | |
{ | |
Bind<IModuleADoSomething>().To<ModuleADoSomething>(); | |
} | |
} | |
} |
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
namespace ExperimentNinjectComposition.ModuleB.Interface | |
{ | |
public interface IModuleBDoSomething : IDoSomething | |
{ | |
} | |
} | |
namespace ExperimentNinjectComposition.ModuleA | |
{ | |
public class ModuleBDoSomething : IModuleBDoSomething | |
{ | |
private readonly IDateTimeProvider _dateTimeProvider; | |
public ModuleBDoSomething(IDateTimeProvider dateTimeProvider) | |
{ | |
_dateTimeProvider = dateTimeProvider; | |
} | |
public string DoSomething() | |
{ | |
return "Current UTC Date (Module B): " + _dateTimeProvider.GetCurrentUtcDate(); | |
} | |
} | |
} | |
namespace ExperimentNinjectComposition.ModuleB | |
{ | |
public class Module : NinjectModule | |
{ | |
public override void Load() | |
{ | |
Bind<IModuleBDoSomething>().To<ModuleBDoSomething>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment