Last active
August 6, 2024 06:54
-
-
Save dkellycollins/9c3fecaedd830094d7f2 to your computer and use it in GitHub Desktop.
Example of how you might use Ninject within a WinForms application.
This file contains hidden or 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
/// <summary> | |
/// Non-Generic BaseForm. Provides functionality for retrieving the controller. | |
/// </summary> | |
public class BaseForm : Form | |
{ | |
/// <summary> | |
/// Gets the controller for the given type. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <returns></returns> | |
protected T GetController<T>() | |
where T : class, IController | |
{ | |
return NinjectProgram.Kernel.Get<T>(); | |
} | |
} | |
/// <summary> | |
/// Generic BaseForm. Automatically retrieves the correct controller. | |
/// </summary> | |
/// <typeparam name="T">The type of the controller for forms.</typeparam> | |
/// <remarks>This class cannot be inherited if the sub class must appear in the designer as the designer cannot instanciate generic classes.</remarks> | |
public class BaseForm<T> : Form | |
where T : IController | |
{ | |
/// <summary> | |
/// Gets the controller for the form. | |
/// </summary> | |
protected T Controller { get; private set; } | |
/// <summary> | |
/// Default constructor. | |
/// </summary> | |
public BaseForm() | |
{ | |
Controller = NinjectProgram.Kernel.Get<T>(); | |
} | |
} |
This file contains hidden or 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
/// <summary> | |
/// Base class for UserControls. Provides functionality for retriveing controls. | |
/// </summary> | |
public class BaseUserControl : UserControl | |
{ | |
/// <summary> | |
/// Gets a controller of the given type. | |
/// </summary> | |
/// <typeparam name="T">The type of the controller to get.</typeparam> | |
/// <returns>An instance of a controller of the given type.</returns> | |
protected T GetController<T>() | |
where T : class, IController | |
{ | |
return NinjectProgram.Kernel.Get<T>(); | |
} | |
} | |
/// <summary> | |
/// Generic base class for UserControls. Automatically retrives the controller for the control. | |
/// </summary> | |
/// <typeparam name="T">The type of the controller.</typeparam> | |
/// <remarks>This class cannot be inherited if the sub class must appear in the designer as the designer cannot instanciate generic classes.</remarks> | |
public class BaseUserControl<T> : UserControl | |
where T : IController | |
{ | |
/// <summary> | |
/// Gets the controller for the control. | |
/// </summary> | |
protected T Controller { get; private set; } | |
/// <summary> | |
/// Default constructor. | |
/// </summary> | |
public BaseUserControl() | |
{ | |
Controller = NinjectProgram.Kernel.Get<T>(); | |
} | |
} |
This file contains hidden or 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
public interface IMainController : IController | |
{ | |
//Defines functionality for the MainForm. | |
} | |
public class MainController : IMainController | |
{ | |
//Implementes functionality for the MainForm. | |
public MainController(IOtherController otherController) | |
{ | |
//Other controllers can be loaded into the form controller by just defining them in the constructor. | |
} | |
} | |
public class MainForm : BaseForm | |
{ | |
private IMainController _controller; | |
public MainForm() | |
{ | |
InitializeComponent(); | |
if(LicenseManager.UsageMode == LicenseUsageMode.Designtime) | |
return; //The controller cannot be fetched at design time because the program has not loaded the kernel. | |
init(); | |
} | |
private void init() | |
{ | |
_controller = GetController<IMainController>(); | |
} | |
} | |
public class ExampleModule : NinjectModule | |
{ | |
public override void Load() | |
{ | |
//Here is where we define what implementations map to what interfaces. | |
Bind<IMainController>().To<MainController>(); | |
Bind<IOtherController>().To<OtherController>().InSingletonScope(); //We can mark an implementation as singleton here. | |
//We can also load other modules this project depends on. | |
Kernel.Load(new NinjectModule()); | |
} | |
} | |
public class Program : NinjectProgram | |
{ | |
private static void Main() | |
{ | |
Kernel = new StandardKernel(); | |
Kernel.Load(new ExampleModule()); //We load the module when the program starts. | |
Application.Run(new MainForm()); //Now the mainform will fetch the controller it depends on, which will auto fetch controllers it depends on and so on. | |
} | |
} |
This file contains hidden or 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
/// <summary> | |
/// Base interface for all other controllers. | |
/// </summary> | |
public interface IController | |
{ | |
} |
This file contains hidden or 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
public class NinjectProgram | |
{ | |
/// <summary> | |
/// Gets the inject kernal for the program. | |
/// </summary> | |
public static IKernel Kernel { get; protected set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this an example of WinForms adopting MVC?