Private / Secure Setters with F #

I decided to run a relatively large project with F # along with MVC4 and Nhibernate.

Now, in C #, my usual practice with ORM is private settings for certain properties (e.g. auto-increment / identification 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 the "identity column", and ORM will handle the setting of its value.

In F # this is what I still have

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

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

I am new to F #, so I would like to know how best to accomplish such things. 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 design?

Edit: for NHibernate, replace private with secure in setters :)

+4
source share
1 answer

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

What you cannot do is use delta access modifiers for automatically implemented properties. Thus, if you want to use different access modifiers, you need to manually implement the support field (using let ) and getter / setter methods.

+4
source

Source: https://habr.com/ru/post/1487641/


All Articles