Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Constructors in F#

How would I write the following C# code in F#?

namespace Shared {
    public class SharedRegistry : PageRegistry {
        public SharedRegistry(bool useCache = true)
            : base(useCache) {
            // Repositories
            ForRequestedType<IAddressRepository>().TheDefaultIsConcreteType<SqlAddressRepository>();
            ForRequestedType<ISharedEnquiryRepository>().TheDefaultIsConcreteType<SharedEnquiryRepository>();

            // Services
            ForRequestedType<IAddressService>().TheDefaultIsConcreteType<AddressService>();
            ForRequestedType<ISharedEnquiryService>().TheDefaultIsConcreteType<SharedEnquiryService>();
        }
    }
}

As is as far as I have managed, but I can't work out to inherit from PageRegistry at the same time as declaring my own default constructor.

type SharedRegistry(useCache: bool) =
    inherit PageRegistry(useCache)
    new() = new SharedRegistry(true)

Rich

like image 261
kim3er Avatar asked Dec 29 '22 21:12

kim3er


1 Answers

I'm not sure that I understand your question; what you've written above looks like it ought to work fine. If you're asking where to put the rest of the constructor logic, try this:

type SharedRegistry(useCache) as this =
  inherit PageRegistry(useCache)
  do
    this.ForRequestedType<IAddressRepository>().TheDefaultIsConcreteType<SqlAddressRepository>()
    // etc.
  new() = SharedRegistry(true)

If you want to define each constructor individually, you can do that too:

type SharedRegistry =
  inherit PageRegistry
  new(useCache) as this = 
    { inherit PageRegistry(useCache) } then
    this.ForRequestedType<IAddressRepository>().TheDefaultIsConcreteType<SqlAddressRepository>()
    // etc.
  new() = SharedRegistry(true)

Or, you could use an optional argument to your main constructor:

type SharedRegistry(?useCache) as this =
  inherit PageRegistry(defaultArg useCache true)
  do
    this.ForRequestedType<IAddressRepository>().TheDefaultIsConcreteType<SqlAddressRepository>()
    // etc.
like image 53
kvb Avatar answered Jan 13 '23 01:01

kvb