F# 中的重写构造函数

发布于 2024-08-27 03:17:33 字数 941 浏览 7 评论 0原文

如何在 F# 中编写以下 C# 代码?

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>();
        }
    }
}

据我所知,但我无法在声明我自己的默认构造函数的同时继承 PageRegistry

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

富有的

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2024-09-03 03:17:33

我不确定我是否理解你的问题;你上面写的看起来应该可以正常工作。如果您询问将构造函数的其余逻辑放在哪里,请尝试以下操作:

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

如果您想单独定义每个构造函数,您也可以这样做:

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

或者,您可以对主构造函数使用可选参数:

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

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.
凉城已无爱 2024-09-03 03:17:33

您的 C# 类使用带有默认值的参数,这与重载构造函数略有不同。无论如何,F# 支持重载构造函数和默认参数。

使用参数的默认值,代码将如下所示:

type SharedRegistry(?useCache: bool) = 
    do 
      // constructor logic
    inherit PageRegistry(defaultArg useCache true) 

现在您可以按如下方式创建实例:

let r1 = new SharedRegistry() // using the default value
let r2 = new SharedRegistry(false) // specified explicitly
let r3 = new SharedRegistry(useCache=false) // using named parameter

我相信在 F# 中使用命名参数稍微优雅一些​​。它的工作方式是参数 useCache 在幕后变成 option (如果您想使用 C# 中的类,这可能是一个问题

) strong>重载构造函数 - 您的 F# 代码应该是正确的(请参阅 kvb 的答案)。一般来说,最好至少有一个隐式构造函数(因为它允许您自动访问类体内的构造函数参数,使用 let 声明字段并使用 实现构造函数逻辑做)。隐式构造函数应该是接受所有参数的构造函数。在某些情况下,您可能希望将其设为私有,可以这样做:

type SharedRegistry private (useCache: bool) = 
    inherit PageRegistry(useCache) 
    do 
      // constructor logic
    new () = SharedRegistry(true)

Your C# class uses parameters with default value which is slightly different than overloaded constructors. In any case, F# supports both overloaded constructors and default parameters.

Using default values of a parameters, the code would look like this:

type SharedRegistry(?useCache: bool) = 
    do 
      // constructor logic
    inherit PageRegistry(defaultArg useCache true) 

Now you can create an instance as follows:

let r1 = new SharedRegistry() // using the default value
let r2 = new SharedRegistry(false) // specified explicitly
let r3 = new SharedRegistry(useCache=false) // using named parameter

I belive that using named parameters is slightly more elegant in F#. The way it works is that the parameter useCache becomes an option<bool> under the cover (This may be a problem if you wanted to use the class from C#)

Regarding overloaded constructors - Your F# code should be correct (see the answer from kvb). In general, it is probably better to have at least one implicit constructor (as that allows you to access constructor parameters inside the body of the class automatically, declare fields using let and implement constructor logic using do). The implicit constructor should be the one that takes all the parameters. In some cases, you may want to make it private, which can be done like this:

type SharedRegistry private (useCache: bool) = 
    inherit PageRegistry(useCache) 
    do 
      // constructor logic
    new () = SharedRegistry(true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文