Using Enterprise Library Unity through FSharp

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > The advantage of using the IOC concept of Unity Library is simply to further decouple the dependencies of the components of the system. Client-side code can be developed quickly by simply relying on the interface (service) that needs to be used. </span>
1. Basic process, define class--Configure Container--> parsing class, and use---destructor (by policy)
2. Type of registration
Registering instances, similar to single-piece mode
Registration type, various extensions
Constructor classes
1. The Injectionconstructor declaration type at the time of registration, not previously registered, cannot be resolved; An error occurs when the object is generated. (Can be resolved during the parsing process)
let container = new UnityContainer () type tenantstore () = interface Itenantstore with member this. MSG () = Printfn "Hello, it ' s Tenantstore" interface IDisposable with member this. Dispose () = PRINTFN "Tenantstore hase been cleaned" type Managementcontroller (St:itenantstore, _name:string) = member th Is. Tstore = St member Val Name = _name with GET, set interface IDisposable with member this. Dispose () = printfn "Control%A hase been cleaned" this. Nameusing (New Tenantstore ()) (fun x, X:>itenantstore). MSG ()) container. Registertype<itenantstore, tenantstore> () container. Registertype<managementcontroller> (new Injectionconstructor (TYPEOF<ITENANTSTORE>, typeof<string >)) using (container. Resolve<managementcontroller> (New Parameteroverride ("_name", (box "Hello"))) (Fun Myctrl, printfn "%A" myct Rl. Tstore) 
2. The parameters in the function can be overloaded during the parsing process (during resolve).
3. Generic class registration, in the form of parameters to indicate the registration class, you can ignore the generic type at the time of registration, specify when parsing
Using typeof to obtain a type defaults to replacing the generic class with obj, which does not produce the effect of a delay definition. You need to use TYPEDEFOF to get a type with a generic type
[Reference]http://stackoverflow.com/questions/6783284/how-to-get-generic-type-definition-for-crtp-type
Type imessage< ' a> = abstract member Send: ' A-unittype message< ' a> () = Interface imessage< ' a& Gt With member this. Send (msg: ' a) = PRINTFN "Send Message%A" Msg//not Work//container. Registertype (TYPEOF&LT;IMESSAGE&LT;_&GT;&GT;, typeof<message<_>>) container. Registertype (TYPEDEFOF&LT;IMESSAGE&LT;_&GT;&GT;, typedefof<message<_>>)//Observe registration class container. Registrations |> Seq.iter (fun i-printfn "regist type:%a" i.registeredtype) let y = container.                                     Resolve<imessage<int>> () y.send (413) Let z = container. Resolve<imessage<bool>> () Z.send (true) <span style= "Font-f Amily:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > </span>
4. Attribute Injection
The FSharp property cannot be set directly to a null value, so there are some tricks to be used in the property injection process.
If a lightweight object can default to an object instance for padding, this is certainly not a good idea.
Type MyClass () as x= let     t = new Tenantstore ():> itenantstore    [<dependency>]    member Val pproperty:i Tenantstore = T with GET, set
Append a null field to do the padding. This implementation is closest to C #, but the interface is more, the unfinished bits and pieces of small things is also a pile of mind.
Type MyClass () as x=     [<defaultvalue>]    val mutable t:itenantstore    [<dependency>]    member Val Pproperty:itenantstore = x.t with GET, set
In fact, NULL is not a good thing, the source of the anomaly! FSharp to get rid of NULL gives the option solution, can you use it here? I tried.
Type MyClass () as x=     [<dependency>]    member val pproperty:itenantstore option = None with GET, set
The results are all very well! It's pretty much, isn't it? And absolutely no NULL participation. I think the object-oriented philosophy is that all objects should have a definite identity. Null is only a legacy of the system and is less used as a good result. But there is also a drawback here. Get pproperty when you need to get the value behind, one more step.

Five kinds of declaration cycle exploration
Default is
0. Short-term life cycle (Transient LifeTime)
produce a different object
Container. Registertype<itenantstore, tenantstore> () let A = container. Resolve<itenantstore> () Let B = container. Resolve<itenantstore> () printfn "is a eqaul to B:%A" (A.equals (b))
</pre>false1. Container life cycle (Container Lifetimemanger) is approximate to single-piece mode <pre name= "code" class= "plain" >container. Registertype<itenantstore, tenantstore> (New ContainerControlledLifetimeManager ()) Let A = container. Resolve<itenantstore> () Let B = container. Resolve<itenantstore> () printfn "is a eqaul to B:%A" (A.equals (b))

True
2. Hierarchical lifecycle (Hierarchical life time)
Brain complements a tree diagram
Container. Registertype<itenantstore, tenantstore> (New Hierarchicallifetimemanager ()) Let Childcontainer = container. Createchildcontainer () Let A = container. Resolve<itenantstore> () Let B = childcontainer. Resolve<itenantstore> () printfn "is a eqaul to B:%A" (A.equals (b))

False
3 Individual Solution Lifecycle (Preresolve life time)
It is understood that when multiple different classes depend on the same class (The resource Class), the class is not parsed repeatedly when the object is fetched.
Must be more than three layers of the object structure, a single layer can not produce such an effect
Type Itenantstore = abstract member Msg:unit->unittype Tenantstore () as x= do printfn "new Tenantstore%A" (x . GetHashCode ()) interface Itenantstore with member this. MSG () = Printfn "Hello, it ' s Tenantstore" type Managementcontroller (St:itenantstore, _name:string) = Member this. Tstore = St member Val Name = _name with GET, set interface IDisposable with member this. Dispose () = printfn "Control%A hase been cleaned" this. NameType Exmanagementcontroller (St:itenantstore, _name:string) = Member this. Tstore = St member Val Name = _name with GET, set interface IDisposable with member this. Dispose () = printfn "Control%A hase been cleaned" this. NameType combinemanagement (M1:managementcontroller, M2:exmanagementcontroller) = Member this. M1 = M1 member this. M2 = m2//Registered container. Registertype<itenantstore, tenantstore> (New Perresolvelifetimemanager ()) container. Registertype<managementcontroller> (New Injectionconstructor (typeof<itenantstore&gt, "xx")) container. Registertype<exmanagementcontroller> (new Injectionconstructor (typeof<itenantstore>, "yy")) container. Registertype<combinemanagement> ()
Get Let o = container. Resolve<combinemanagement> () printfn "%d,%d" (o.m1. Tstore.gethashcode ()) (o.m2. Tstore.gethashcode ())

Also thread-related, network-related, external control of the life cycle, not yet delve into

Using Enterprise Library Unity through FSharp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.