1
2
type IFacet<'a> =
    ...
By on 12/15/2012 11:39 AM ()

Yes<'a>! That helps and some modifications later it works. Complete code:

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
// Facet Pattern 
// Used for Principle of Least Authority (POLA)
// Inspired by: "The Lazy Programmer's Guide to Secure Computing"
//              http://www.youtube.com/watch?v=eL5o4PFuxTY
type IFacet<'a> =
    abstract Set    : 'a   -> unit
    abstract Get    : unit -> 'a Option
    abstract Revoke : unit -> unit

type OnOff = On | Off

let allowWrite access f = if access=On then f()
let allowRead  access f = if access=On then Some( f() ) else None

/// Revocable Facet Maker
let makeFacet x =
    let access = ref On
    { new IFacet<_> with 
        member this.Revoke() =             access := Off             
        member this.Get()    = allowRead  !access (fun () -> !x )
        member this.Set(v)   = allowWrite !access (fun () ->  x := v)
    } 

// usage
let my = ref "Revocable Facet"
let facet = makeFacet my

facet.Get()             = Some "Revocable Facet"
facet.Set( "changed" )
facet.Get()             = Some "changed"
facet.Revoke()        
facet.Get()             = None
facet.Set( "changed2" )
facet.Get()             = None
my.Value                = "changed"
By on 12/15/2012 11:56 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper