HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

NinjectWebCommon implementation/translation - C# to F#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementationtranslationninjectwebcommon

Problem

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

```
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
[]
type NinjectWebCommon () =
static let bootstrapper = new Bootstrapper()

///
/// Load your modules or register your services here!
///
/// The kernel.
//private static void RegisterServices(IKernel kernel)
static let RegisterServices(kernel:IKernel) =
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver
/// Creates the kernel that will manage your application.
///
/// The created kernel.
//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 (fun () -> bs().Kernel)
kernel.Bind>().ToMethod(fun ac -> kernelFun) |> ignore
kernel.Bind().To() |> ignore
let fetchedKernel:IKernel =System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof) :?> 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.Debu

Solution

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.

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

-
_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.

Context

StackExchange Code Review Q#47828, answer score: 2

Revisions (0)

No revisions yet.