一个包装类型提供程序是否应该包含在类内部具有副作用的值? [英] Should one wrap type providers containing values that have side effects inside a class?

查看:76
本文介绍了一个包装类型提供程序是否应该包含在类内部具有副作用的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在代码中实现F# coding conventions页面上的出色建议

I am trying to implement in my code the excellent advice in the F# coding conventions page

https://docs.microsoft.com/en -us/dotnet/fsharp/style-guide/conventions .

Use classes to contain values that have side effects部分特别有趣.它说

There are many times when initializing a value can have side effects, such as instantiating a context to a database or other remote resource. It is tempting to initialize such things in a module and use it in subsequent functions.

并提供了一个示例.然后指出了这种做法的三个问题(由于空间不足,我忽略了这些问题,但可以在链接的文章中看到它们),并建议使用一个简单的类来保存依赖项.

and provides an example. Then it points out three problems with this practice (I omit those for lack of space, but they can be seen at the linked article) and recommends using a simple class to hold dependencies.

我想知道应该如何对待类型提供程序?例如,如果我有以下代码,

I wonder how should type providers be treated? For example, if I have the following code,

[<Literal>]
let projDataPath = __SOURCE_DIRECTORY__ + @"\data\"

[<Literal>]
let configPath = projDataPath + "config.json"

type Cnfg = JsonProvider<Sample=configPath>
let config = Cnfg.Load(configPath)

使用类型提供程序初始化值是否会遇到与本文所述的副作用相关的值初始化问题?

using the type provider to initialize a value is subject to the problems associated to value initialization with side effects described in the article?

换句话说,我应该将类型提供程序包装在一个类中吗?

In other words, should I wrap the type provider inside a class?

推荐答案

通常,您根本不应该向使用者公开类型提供程序的实例或它们提供的类型.除了可能由非F#.NET使用者引起的潜在互操作性问题之外,类型提供程序的实例通常还表示您的应用程序正在管理的某些私有状态或资源的接口.通常,最好从用户那里提取基础资源,并提出最适合问题域的模型.

You should generally not expose instances of type providers or their provided types to consumers at all. Aside from the potential interoperability issues that may arise from non-F# .NET consumers, instances of Type Providers often represent an interface to some private state or resource that your application is managing. As a general rule, it is best to abstract the underlying resources from your consumer and present a model that best fits the problem domain.

链接的文章特别警告不要捕获具有有限生命周期的类的实例作为模块中的绑定,因为绑定是不可变的,并且当实例的生命周期结束时,实例将变得无效.这等效于在C#类中具有DbContext或类似实例作为static readonly成员.它会由静态构造函数初始化,但是即使数据库连接已关闭并且DbContext实例不再有用,也永远不会更改.

The linked article is specifically warning against capturing instances of classes with a finite lifecycle as bindings in a module, because the bindings are immutable and the instance will become invalid when its lifecycle ends. This would be equivalent to having a DbContext or similar instance as a static readonly member in a C# class. It would be initialized by the static constructor, but could never change, even if the database connection becomes closed and the DbContext instance is no longer useful.

这篇关于一个包装类型提供程序是否应该包含在类内部具有副作用的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆