Translating C # object chain to F #
Quite a stupid question, but I can't seem to find the correct terminology for it, so all my searches won't work.
I have the following C # method call chain:
container.Register(Component.For<IMyInterface>().ImplementedBy<MyClass>().Named("MyInstance").LifeStyleSingleton);
How do I write the same thing in F #?
I can do it:
let f0 = Component.For<IMyInterface> () in
let f1 = f0.ImplementedBy<MyClass> () in
let f2 = f1.Named "MyInstance" in
let f3 = f2.LifestyleSingleton () in
ignore (container.Register f3)
But, of course, there must be some other, more convenient way to structure such a call. Not?
Adding
Early answers led me to a solution that worked (I removed all mentions ignore
since it doesn't matter and only confused readers):
container.Register (Component.For<IMyInterface>().ImplementedBy<MyClass>().Named("MyInstance").LifestyleSingleton())
However, one answer indicates that this should work:
container.Register <| Component.For<IMyInterface>().ImplementedBy<MyClass>().Named("MyInstance").LifestyleSingleton()
but this is not the case. The last part, the expression after <|
, generates a type error
This expression was expected to be of type unit, but here it is of type ComponentRegistration <IMyInterface>.
source to share
You can do almost the same thing in F #:
container.Register <|
Component
.For<IMyInterface>()
.ImplementedBy<MyClass>()
.Named("My Instance")
.LifeStyleSingleton()
Here with added sugar ( <|
). You can put the calls on a single line or as I did (I prefer this since it mirrors the F # pipeline perfectly). Keep in mind that arguments must be enclosed in parentheses and no spaces between the function name and parens (pretty much "C # style").
source to share