使用C#中的F#Discriminated Unions的最佳方法是什么?
我一直在深入研究这个问题,我可能找到了最简单的方法,但由于它相当复杂,可能还有其他一些我看不到的东西......
有一个有区别的联盟,例如:
type Shape =
| Rectangle of float * float
| Circle of float
Run Code Online (Sandbox Code Playgroud)
我发现C#的用法是(避免使用变量,使类型明显):
Shape circle = Shape.NewCircle(5.0);
if (circle.IsCircle)
{
Shape.Circle c = (Shape.Circle)circle;
double radius = c.Item;
}
Run Code Online (Sandbox Code Playgroud)
在C#中,NewXXXX静态方法总是创建Shape类的对象,还有一种方法IsXXXX来检查对象是否属于该类型; 当且仅当它是,它可以转换为Shape.XXXX类,并且只有它的项目可访问; Shape.XXXX类的构造函数是内部的,即无法访问.
有人知道从一个受歧视的联盟获取数据的更简单的选择吗?
我正在使用F#接口,通过创建一个简单的数据访问类.
界面是:
type IUserAccess =
abstract member GetUsers : (dbSchema.ServiceTypes.Users -> bool) -> dbSchema.ServiceTypes.Users seq
abstract member GetAllUsers : unit -> dbSchema.ServiceTypes.Users seq
Run Code Online (Sandbox Code Playgroud)
在课堂上,我用这种方式调用方法:
type UserAccess() =
let db = dbSchema.GetDataContext()
interface IUserAccess with
member this.GetUsers cond =
let query = query {
for row in db.Users do
select row }
query |> Seq.where cond
member this.GetAllUsers () =
(this:>IUserAccess).GetUsers (fun _ -> true)
Run Code Online (Sandbox Code Playgroud)
我有点担心的是我调用GetAllUsers函数的方式,特别是部分函数(this:>IUserAccess).调用在同一接口中实现的方法的最简单方法是什么?
一个简单的选择,我能想到是创造GetUsers直接在方法UserAccess()类,然后从两者的接口实现调用它GetUsers和GetAllUsers,但是这意味着实施了新的私有方法,我想避免的.还有其他选择吗?
我很感激有关使我的CAML查询工作的任何帮助.我有以下列表:
| ID | Department | Status |
Run Code Online (Sandbox Code Playgroud)
我试图让查询返回 list items where ID=1, Department=Audit, and Status= "Stopped" OR "In Progress".
有人可以帮忙吗?
问候,
如果它拥有所需的状态,我需要将一个类传递给表格单元格.
我写了这个:
%td{class: (if a.state == 'accepted' then 'positive' elsif a.state == 'proposed' then 'warning' elsif a.state == 'changed' then 'warning' else 'negative' end )}
Run Code Online (Sandbox Code Playgroud)
它有效,但看起来非常糟糕.怎么简化?或者还有其他解决方案吗?