13 people like it.
Like the snippet!
Mini IoC Container
Minimal Inversion of Control (IoC) Container for Dependency Injection (DI) in under 100 lines of code. Implements the 3 Rs of DI: Register, Resolve, Release.
Note: missing thread safety and fluent interface.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
|
open System
open System.Collections.Generic
open Microsoft.FSharp.Reflection
type Lifetime = Singleton | Transient
type private Constructor =
| Reflected of Type
| Factory of (unit -> obj)
let (|FunType|_|) t =
if FSharpType.IsFunction t
then FSharpType.GetFunctionElements t |> Some
else None
type Container () =
let catalog = Dictionary<Type, Constructor * Lifetime>()
let singletons = Dictionary<Type,obj>()
let rec resolve t =
match catalog.TryGetValue(t) with
| true, (Factory f, lifetime)->
obtain t (fun () -> f() |> Some) lifetime
| true, (Reflected u, lifetime) ->
obtain t (fun () -> construct u) lifetime
| false, _ ->
obtain t (fun () -> construct t) Singleton
and obtain t f lifetime =
match singletons.TryGetValue t with
| true, value -> Some value
| false, _ ->
let result = f()
result |> Option.iter (fun value -> store lifetime t value)
result
and store lifetime t value =
match lifetime with
| Singleton -> singletons.Add(t,value)
| Transient -> ()
and construct t =
t.GetConstructors() |> Array.tryPick (fun ctr ->
let ps = ctr.GetParameters()
let args = ps |> Array.map (fun p ->
let t =p.ParameterType
match t with
| FunType(arg,result) when arg = typeof<unit> ->
match resolve result with
| Some x -> FSharpValue.MakeFunction(t,fun args -> x) |> Some
| None -> None
| t -> resolve t
)
match args |> Array.forall Option.isSome with
| true -> args |> (Array.choose id) |> ctr.Invoke |> Some
| false -> None
)
static let current = Container()
static member Current = current
member container.Register<'TAbstract when 'TAbstract : not struct>
(concreteType:Type, lifetime:Lifetime) =
catalog.Add(typeof<'TAbstract>, (Reflected(concreteType),lifetime))
member container.Register<'TAbstract when 'TAbstract : not struct>
(concreteType:Type) =
container.Register<'TAbstract>(concreteType,Singleton)
member container.Register<'TAbstract when 'TAbstract : not struct>
(f:unit->'TAbstract, lifetime:Lifetime) =
catalog.Add(typeof<'TAbstract>, (Factory(f >> box), lifetime))
member container.Register(f:unit->'TAbstract) =
container.Register(f, Singleton)
member container.Resolve<'TAbstract when 'TAbstract : not struct>() =
let t = typeof<'TAbstract>
match resolve t with
| Some value -> value :?> 'TAbstract
| None -> sprintf "Failed to resolve %A" t |> invalidOp
member container.Release(instance:obj) =
singletons
|> Seq.filter (fun pair -> pair.Value = instance)
|> Seq.toList
|> List.iter (fun pair -> singletons.Remove(pair.Key) |> ignore)
module Usage =
let container = new Container()
let f1 = fun () -> 1
type A () =
do printfn "A"
type B () =
do printfn "B"
type C (a:A, B) =
do printfn "C"
type D (a:A, b:B, fc:unit -> C) =
let c = fc()
do printfn "D"
container.Register((fun () -> printfn "B lambda"; B()), Transient)
container.Register<C>(typeof<C>)
let c = container.Resolve<C>()
let c' = container.Resolve<C>()
container.Release(c)
let c'' = container.Resolve<C>()
container.Release(c'')
let d = container.Resolve<D>()
type ICalculate =
abstract member Incr : int -> int
type Calculator () =
interface ICalculate with
member this.Incr(x:int) = x + 1
container.Register<ICalculate>(typeof<Calculator>)
let calc = container.Resolve<ICalculate>()
printfn "%d" (calc.Incr 1)
Console.ReadLine() |> ignore
|
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Reflection
type Lifetime =
| Singleton
| Transient
Full name: Script.Lifetime
union case Lifetime.Singleton: Lifetime
union case Lifetime.Transient: Lifetime
type private Constructor =
| Reflected of Type
| Factory of (unit -> obj)
Full name: Script.Constructor
union case Constructor.Reflected: Type -> Constructor
type Type =
inherit MemberInfo
member Assembly : Assembly
member AssemblyQualifiedName : string
member Attributes : TypeAttributes
member BaseType : Type
member ContainsGenericParameters : bool
member DeclaringMethod : MethodBase
member DeclaringType : Type
member Equals : o:obj -> bool + 1 overload
member FindInterfaces : filter:TypeFilter * filterCriteria:obj -> Type[]
member FindMembers : memberType:MemberTypes * bindingAttr:BindingFlags * filter:MemberFilter * filterCriteria:obj -> MemberInfo[]
...
Full name: System.Type
union case Constructor.Factory: (unit -> obj) -> Constructor
type unit = Unit
Full name: Microsoft.FSharp.Core.unit
type obj = Object
Full name: Microsoft.FSharp.Core.obj
val t : Type
type FSharpType =
static member GetExceptionFields : exceptionType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
static member GetFunctionElements : functionType:Type -> Type * Type
static member GetRecordFields : recordType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
static member GetTupleElements : tupleType:Type -> Type []
static member GetUnionCases : unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo []
static member IsExceptionRepresentation : exceptionType:Type * ?bindingFlags:BindingFlags -> bool
static member IsFunction : typ:Type -> bool
static member IsModule : typ:Type -> bool
static member IsRecord : typ:Type * ?bindingFlags:BindingFlags -> bool
static member IsTuple : typ:Type -> bool
...
Full name: Microsoft.FSharp.Reflection.FSharpType
static member FSharpType.IsFunction : typ:Type -> bool
static member FSharpType.GetFunctionElements : functionType:Type -> Type * Type
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Multiple items
type Container =
new : unit -> Container
member Register : concreteType:Type -> unit
member Register : f:(unit -> 'TAbstract) -> unit (requires reference type)
member Register : concreteType:Type * lifetime:Lifetime -> unit
member Register : f:(unit -> 'TAbstract) * lifetime:Lifetime -> unit (requires reference type)
member Release : instance:obj -> unit
member Resolve : unit -> 'TAbstract (requires reference type)
static member Current : Container
Full name: Script.Container
--------------------
new : unit -> Container
val catalog : Dictionary<Type,(Constructor * Lifetime)>
Multiple items
type Dictionary<'TKey,'TValue> =
new : unit -> Dictionary<'TKey, 'TValue> + 5 overloads
member Add : key:'TKey * value:'TValue -> unit
member Clear : unit -> unit
member Comparer : IEqualityComparer<'TKey>
member ContainsKey : key:'TKey -> bool
member ContainsValue : value:'TValue -> bool
member Count : int
member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
member Item : 'TKey -> 'TValue with get, set
...
nested type Enumerator
nested type KeyCollection
nested type ValueCollection
Full name: System.Collections.Generic.Dictionary<_,_>
--------------------
Dictionary() : unit
Dictionary(capacity: int) : unit
Dictionary(comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : unit
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : unit
val singletons : Dictionary<Type,obj>
val resolve : (Type -> obj option)
Dictionary.TryGetValue(key: Type, value: byref<Constructor * Lifetime>) : bool
val f : (unit -> obj)
val lifetime : Lifetime
val obtain : (Type -> (unit -> obj option) -> Lifetime -> obj option)
val u : Type
val construct : (Type -> obj option)
val f : (unit -> obj option)
Dictionary.TryGetValue(key: Type, value: byref<obj>) : bool
val value : obj
val result : obj option
module Option
from Microsoft.FSharp.Core
val iter : action:('T -> unit) -> option:'T option -> unit
Full name: Microsoft.FSharp.Core.Option.iter
val store : (Lifetime -> Type -> obj -> unit)
Dictionary.Add(key: Type, value: obj) : unit
Type.GetConstructors() : Reflection.ConstructorInfo []
Type.GetConstructors(bindingAttr: Reflection.BindingFlags) : Reflection.ConstructorInfo []
type Array =
member Clone : unit -> obj
member CopyTo : array:Array * index:int -> unit + 1 overload
member GetEnumerator : unit -> IEnumerator
member GetLength : dimension:int -> int
member GetLongLength : dimension:int -> int64
member GetLowerBound : dimension:int -> int
member GetUpperBound : dimension:int -> int
member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
member Initialize : unit -> unit
member IsFixedSize : bool
...
Full name: System.Array
val tryPick : chooser:('T -> 'U option) -> array:'T [] -> 'U option
Full name: Microsoft.FSharp.Collections.Array.tryPick
val ctr : Reflection.ConstructorInfo
val ps : Reflection.ParameterInfo []
Reflection.MethodBase.GetParameters() : Reflection.ParameterInfo []
val args : obj option []
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.map
val p : Reflection.ParameterInfo
property Reflection.ParameterInfo.ParameterType: Type
active recognizer FunType: Type -> (Type * Type) option
Full name: Script.( |FunType|_| )
val arg : Type
val result : Type
val typeof<'T> : Type
Full name: Microsoft.FSharp.Core.Operators.typeof
val x : obj
type FSharpValue =
static member GetExceptionFields : exn:obj * ?bindingFlags:BindingFlags -> obj []
static member GetRecordField : record:obj * info:PropertyInfo -> obj
static member GetRecordFields : record:obj * ?bindingFlags:BindingFlags -> obj []
static member GetTupleField : tuple:obj * index:int -> obj
static member GetTupleFields : tuple:obj -> obj []
static member GetUnionFields : value:obj * unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo * obj []
static member MakeFunction : functionType:Type * implementation:(obj -> obj) -> obj
static member MakeRecord : recordType:Type * values:obj [] * ?bindingFlags:BindingFlags -> obj
static member MakeTuple : tupleElements:obj [] * tupleType:Type -> obj
static member MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:BindingFlags -> obj
...
Full name: Microsoft.FSharp.Reflection.FSharpValue
static member FSharpValue.MakeFunction : functionType:Type * implementation:(obj -> obj) -> obj
val args : obj
val forall : predicate:('T -> bool) -> array:'T [] -> bool
Full name: Microsoft.FSharp.Collections.Array.forall
val isSome : option:'T option -> bool
Full name: Microsoft.FSharp.Core.Option.isSome
val choose : chooser:('T -> 'U option) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.choose
val id : x:'T -> 'T
Full name: Microsoft.FSharp.Core.Operators.id
Reflection.ConstructorInfo.Invoke(parameters: obj []) : obj
Reflection.MethodBase.Invoke(obj: obj, parameters: obj []) : obj
Reflection.ConstructorInfo.Invoke(invokeAttr: Reflection.BindingFlags, binder: Reflection.Binder, parameters: obj [], culture: Globalization.CultureInfo) : obj
Reflection.MethodBase.Invoke(obj: obj, invokeAttr: Reflection.BindingFlags, binder: Reflection.Binder, parameters: obj [], culture: Globalization.CultureInfo) : obj
val current : Container
static member Container.Current : Container
Full name: Script.Container.Current
val container : Container
member Container.Register : concreteType:Type * lifetime:Lifetime -> unit
Full name: Script.Container.Register
val not : value:bool -> bool
Full name: Microsoft.FSharp.Core.Operators.not
val concreteType : Type
Dictionary.Add(key: Type, value: Constructor * Lifetime) : unit
member Container.Register : concreteType:Type -> unit
Full name: Script.Container.Register
member Container.Register : concreteType:Type -> unit
member Container.Register : f:(unit -> 'TAbstract) -> unit (requires reference type)
member Container.Register : concreteType:Type * lifetime:Lifetime -> unit
member Container.Register : f:(unit -> 'TAbstract) * lifetime:Lifetime -> unit (requires reference type)
member Container.Register : f:(unit -> 'TAbstract) * lifetime:Lifetime -> unit (requires reference type)
Full name: Script.Container.Register
val f : (unit -> 'TAbstract) (requires reference type)
val box : value:'T -> obj
Full name: Microsoft.FSharp.Core.Operators.box
member Container.Register : f:(unit -> 'TAbstract) -> unit (requires reference type)
Full name: Script.Container.Register
member Container.Resolve : unit -> 'TAbstract (requires reference type)
Full name: Script.Container.Resolve
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val invalidOp : message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.invalidOp
member Container.Release : instance:obj -> unit
Full name: Script.Container.Release
val instance : obj
module Seq
from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.filter
val pair : KeyValuePair<Type,obj>
property KeyValuePair.Value: obj
val toList : source:seq<'T> -> 'T list
Full name: Microsoft.FSharp.Collections.Seq.toList
Multiple items
type List<'T> =
new : unit -> List<'T> + 2 overloads
member Add : item:'T -> unit
member AddRange : collection:IEnumerable<'T> -> unit
member AsReadOnly : unit -> ReadOnlyCollection<'T>
member BinarySearch : item:'T -> int + 2 overloads
member Capacity : int with get, set
member Clear : unit -> unit
member Contains : item:'T -> bool
member ConvertAll<'TOutput> : converter:Converter<'T, 'TOutput> -> List<'TOutput>
member CopyTo : array:'T[] -> unit + 2 overloads
...
nested type Enumerator
Full name: System.Collections.Generic.List<_>
--------------------
List() : unit
List(capacity: int) : unit
List(collection: IEnumerable<'T>) : unit
val iter : action:('T -> unit) -> list:'T list -> unit
Full name: Microsoft.FSharp.Collections.List.iter
Dictionary.Remove(key: Type) : bool
property KeyValuePair.Key: Type
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
module Usage
from Script
val container : Container
Full name: Script.Usage.container
val f1 : unit -> int
Full name: Script.Usage.f1
type A =
new : unit -> A
Full name: Script.Usage.A
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
type B =
new : unit -> B
Full name: Script.Usage.B
type C =
new : a:A * B:obj -> C
Full name: Script.Usage.C
val a : A
val B : obj
type D =
new : a:A * b:B * fc:(unit -> C) -> D
Full name: Script.Usage.D
val b : B
val fc : (unit -> C)
val c : C
new : unit -> B
val c : C
Full name: Script.Usage.c
member Container.Resolve : unit -> 'TAbstract (requires reference type)
val c' : C
Full name: Script.Usage.c'
member Container.Release : instance:obj -> unit
val c'' : C
Full name: Script.Usage.c''
val d : D
Full name: Script.Usage.d
type ICalculate =
interface
abstract member Incr : int -> int
end
Full name: Script.Usage.ICalculate
abstract member ICalculate.Incr : int -> int
Full name: Script.Usage.ICalculate.Incr
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
Multiple items
type Calculator =
interface ICalculate
new : unit -> Calculator
Full name: Script.Usage.Calculator
--------------------
new : unit -> Calculator
val this : Calculator
override Calculator.Incr : x:int -> int
Full name: Script.Usage.Calculator.Incr
val x : int
val calc : ICalculate
Full name: Script.Usage.calc
abstract member ICalculate.Incr : int -> int
type Console =
static member BackgroundColor : ConsoleColor with get, set
static member Beep : unit -> unit + 1 overload
static member BufferHeight : int with get, set
static member BufferWidth : int with get, set
static member CapsLock : bool
static member Clear : unit -> unit
static member CursorLeft : int with get, set
static member CursorSize : int with get, set
static member CursorTop : int with get, set
static member CursorVisible : bool with get, set
...
Full name: System.Console
Console.ReadLine() : string
More information