3
\$\begingroup\$

I've translated this code from and would like it to be more idiomatic :

namespace Foo.App_Start

open System
open System.Web

open Microsoft.Web.Infrastructure.DynamicModuleHelper

open Ninject
open Ninject.Web.Common

type NinjectResolver(kernel:IKernel) =
    let _kernel = kernel
    interface System.Web.Http.Dependencies.IDependencyResolver with 
        member this.BeginScope():Http.Dependencies.IDependencyScope = upcast this
        member this.GetService(t) =
            _kernel.TryGet(t)
        member this.GetServices(t)=
            _kernel.GetAll(t)
        member this.Dispose() = ()

//was static
[<AbstractClass; Sealed>]
type NinjectWebCommon () =
    static let bootstrapper = new Bootstrapper()

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
    //private static void RegisterServices(IKernel kernel)
    static let RegisterServices(kernel:IKernel) =
        System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver <- new NinjectResolver(kernel)
        do()
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        //private static IKernel CreateKernel()
    static let KernelCreation(kernel:IKernel) =
        System.Diagnostics.Debug.WriteLine "kernel Creation!"
        RegisterServices(kernel)
        let bs = fun () -> new Bootstrapper()
        let kernelFun= System.Func<IKernel> (fun () -> bs().Kernel)
        kernel.Bind<System.Func<IKernel>>().ToMethod(fun ac -> kernelFun) |> ignore
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>() |> ignore
        let fetchedKernel:IKernel =System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof<IKernel>) :?> IKernel
        fetchedKernel

    static let CreateKernel():IKernel =
        System.Diagnostics.Debug.WriteLine "standard kernel creating!"
        let kernel= new StandardKernel()  
//        
        try         //RegisterServices(kernel) |> ignore;
            KernelCreation(kernel)
        with
            | ex -> 
                System.Diagnostics.Debug.WriteLine "kernel disposing!"
                kernel.Dispose()
                raise ex


    /// <summary>
    /// Starts the application
    /// </summary>
    static member Start() =
        DynamicModuleUtility.RegisterModule typedefof<OnePerRequestHttpModule> |> ignore
        DynamicModuleUtility.RegisterModule typedefof<NinjectHttpModule> |> ignore
        bootstrapper.Initialize(System.Func<_>(CreateKernel))
        System.Diagnostics.Debug.WriteLine "app Start()"
//        
//        
    /// <summary>
    /// Stops the application.
    /// </summary>
    static member Stop() =
        System.Diagnostics.Debug.WriteLine "app stop"
        bootstrapper.ShutDown()


module DummyModuleToAttachAssemblyAttribute = 
    [<assembly: WebActivatorEx.PreApplicationStartMethod(typeof<NinjectWebCommon>, "Start")>]
    ////[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(FLochart.App_Start.NinjectWebCommon), "Stop")]
    [<assembly: WebActivatorEx.ApplicationShutdownMethod(typeof<NinjectWebCommon>, "Stop")>]
    do()
\$\endgroup\$
2
\$\begingroup\$

Your code seems like it's primarily plumbing. There are no algorithms or types that could be given the functional treatment. I have only two recommendations.

  1. Instead of NinjectWebCommon being abstract/sealed with static members you can make it a module with let-bound functions.

  2. _kernel in NinjectResolver is unnecessary because primary constructor parameters are available throughout the body of the type. So you can just reference kernel throughout instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.