Skip to content

Instantly share code, notes, and snippets.

@christianarielli
Forked from dkellycollins/BaseForm.cs
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save christianarielli/f6d0fb652eba7ddd1f23 to your computer and use it in GitHub Desktop.

Select an option

Save christianarielli/f6d0fb652eba7ddd1f23 to your computer and use it in GitHub Desktop.

Revisions

  1. @dkellycollins dkellycollins revised this gist Sep 15, 2014. 4 changed files with 103 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion BaseForm.cs
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,8 @@ protected T GetController<T>()
    /// <summary>
    /// Generic BaseForm. Automatically retrieves the correct controller.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <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
    {
    38 changes: 38 additions & 0 deletions BaseUserControl.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    /// <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>();
    }
    }
    57 changes: 57 additions & 0 deletions Example.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    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.
    }
    }
    6 changes: 6 additions & 0 deletions IController.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    /// <summary>
    /// Base interface for all other controllers.
    /// </summary>
    public interface IController
    {
    }
  2. @dkellycollins dkellycollins created this gist Sep 15, 2014.
    37 changes: 37 additions & 0 deletions BaseForm.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    /// <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"></typeparam>
    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>();
    }
    }
    7 changes: 7 additions & 0 deletions NinjectProgram.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    public class NinjectProgram
    {
    /// <summary>
    /// Gets the inject kernal for the program.
    /// </summary>
    public static IKernel Kernel { get; protected set; }
    }