문제

I have decided to undertake a relatively large project with F# together with MVC4 and Nhibernate.

Now, in C#, my usual practice with ORM's is the have private setters for certain properties (E.g. auto-incremented/identity properties, timestamps, etc). I.e

public class Guide
{
    public int Id { get; private set; }
    public DateTime Created { get; private set; }

    public Guide()
    {
        Created = DateTime.Now;
    }
}

Here id is an "identity column", and the ORM will handle setting its value.

In F# here is what I have so far

type public Guide() =
    member val public Id = 0 with get, set
    member val public Created = DateTime.MinValue with get, set

But the problem I have encountered is that the getters/setters cannot have access modifiers!

I am new with F#, so I would like to know the best way to perform this sort of thing. However, I do not just want to rewrite C# code in F#! I would like to know the correct (functional) approach to this. Maybe use some other construct??

Edit: For NHibernate, replace private with protected in the setters :)

도움이 되었습니까?

해결책

According to the Properties (F#) page on MSDN, you can have access modifiers on your getters/setters. You can also use different access modifiers for the getter and setter (e.g., a public get and a private set).

What you can't do is use difference access modifiers for automatically-implemented properties. So, if you want to use different access modifiers you'll need to manually implement the backing field (with a let) and the getter/setter methods.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top