简体   繁体   English

在同一解决方案中混合使用F#和C#的模式

[英]Patterns to mix F# and C# in the same solution

I studied few functional languages, mostly for academical purposes. 我研究了很少的函数式语言,主要用于学术目的。 Nevertheless, when I have to project a client-server application I always start adopting a Domain Driven Design, strictly OOP. 然而,当我必须投影客户端 - 服务器应用程序时,我总是开始采用域驱动设计,严格来说是OOP。

A complex solution written in a .Net framework could get advantages using more than a language and sometimes more than a paradigm. 用.Net框架编写的复杂解决方案可以使用多种语言获得优势,有时甚至超过范例。 Mixing C or C++ with LUA or Python is a common practice, sometimes embedding prolog can be very interesting. 将C或C ++与LUA或Python混合是一种常见的做法,有时嵌入prolog可能非常有趣。 I never tried to mix OOP and functional paradigm. 我从未试图将OOP和功能范例混合在一起。

F# is a newer functional and object oriented language, I see that's it's technically very easy to mix C# with F# libraries in the same solution. F#是一种更新的功能和面向对象的语言,我看到在同一解决方案中将C#与F#库混合在技术上非常容易。 But I wonder if it makes any sense: I use LINQ to satisfy many of my functional requirements. 但我想知道它是否有意义:我使用LINQ来满足我的许多功能要求。

When and how, do you think it's a good idea to mix these two languages together? 何时以及如何,您认为将这两种语言混合在一起是一个好主意吗? I wonder if exists a set of patterns that tries that. 我想知道是否存在一组试图这样做的模式。

Do you actually use F# in a C# solution? 你真的在C#解决方案中使用F#吗?

There are certain places where traditional functional techniques make a lot of sense, and lead to code that is both smaller and more concise. 在某些地方,传统的功能技术很有意义,并且导致代码更小,更简洁。 A classic example is text parsing and tree processing, both often appearing together when you're implementing a DSL. 一个典型的例子是文本解析和树处理,当你实现DSL时,它们经常出现在一起。 F# features like anonymous iterators, extensible pattern matching, and ability to define custom infix operators to serve as combinators really helps a lot here. F#特性如匿名迭代器,可扩展模式匹配以及定义自定义中缀运算符以充当组合器的能力在这里确实有很大帮助。 Meanwhile, on the C# side, LINQ is a good start, but it doesn't take you all the way there. 同时,在C#方面,LINQ是一个良好的开端,但它并没有带你到处。

I suggest you have a look at FParsec , and see for yourself how much better suited it is to advanced text processing / parsing than any library you could possibly write in C#. 我建议你看一下FParsec ,自己看看它比高级文本处理/解析更适合你用C#写的任何库。

I've written a WCF service in F# that acts as a translator plug-in for reading a WFS (geospatial data) service. 我在F#中编写了一个WCF服务,它充当翻译插件,用于读取WFS(地理空间数据)服务。 The code turned out nice and concise. 代码变得简洁明了。

While the standalone dll I compiled worked fine inside my colleague's C# solution, he did try to strangle me when I showed him the code. 虽然我编译的独立dll在我的同事的C#解决方案中运行良好,但当我向他展示代码时,他确实试图扼杀我。 Culture shock, I think. 我想文化冲击。

So did we use F# and C# in the same project? 那么我们在同一个项目中使用F#和C#吗? Yes and no. 是的,不是。 No, because I rewrote the thing in C#. 不,因为我用C#重写了这个东西。 Yes, because building and testing the prototype in F# saved me more time than it took me to translate it to C# LINQ-style. 是的,因为在F#中构建和测试原型比我花费更多时间将它翻译成C#LINQ风格。

I wouldn't want to try building everything in F#, but I'm patiently waiting for the day I can work on the data processing/algorithmic part of a mixed language solution in F# without fearing for my life. 我不想尝试在F#中构建所有东西,但我耐心地等待着我可以在F#中使用混合语言解决方案的数据处理/算法部分工作的那一天而不用担心我的生活。

You got me thinking, and I tried to decide where I would do it. 你让我思考,我试着决定在哪里做。 There are two situations that spring to mind: 有两种情况需要考虑:

  1. If I am making a castle (MVC) project I would probably have controllers in C# while all the BL and models are in F# (I tend to do domain driven design and wire the BL into the models, or via injected components [ala DI]) 如果我正在制作城堡(MVC)项目,我可能会在C#中使用控制器,而所有BL和模型都在F#中(我倾向于进行域驱动设计并将BL连接到模型中,或通过注入组件[ala DI] )
  2. Starting a new project but incorporating existing libraries so as to not reinvent the wheel. 启动一个新项目,但合并现有的库,以便不重新发明轮子。

Further, I'm a big advocate of 'right tool for the job', so if I think one or the other would suit better, I'd use that. 此外,我是“工作的正确工具”的大力倡导者,所以如果我认为其中一个或哪个更适合,我会使用它。

We have an application which gets all of its functionality by loading MEF parts. 我们有一个应用程序通过加载MEF部件获得其所有功能。 Most of the parts are written in C#, but there is one part which does binary data processing which is written in F#. 大多数部分都是用C#编写的,但有一部分是用F#编写的二进制数据处理。 In this case F# was simply better suited for the job. 在这种情况下,F#更适合这项工作。

The F# part conforms to an interface defined in C# and the C# application has no idea it's dealing with an F# part (other than the dependency on fsharp.dll). F#部分符合C#中定义的接口,C#应用程序不知道它处理F#部分(除了对fsharp.dll的依赖)。

Of course this doesn't just apply to F#, if someone wants to write or rewrite a part (addon, module, whatever you want to call it) in a different language it will be picked up by MEF and provided to our application without any disadvantages. 当然,这不仅仅适用于F#,如果有人想用另一种语言编写或重写一个部分(插件,模块,无论你想要什么),它将被MEF选中并提供给我们的应用程序而不需要任何缺点。

We choose the language that is best suited to solve a specific problem and MEF abstracts all the specifics away from us, so we have nothing to worry about. 我们选择最适合解决特定问题的语言,MEF将所有细节抽象出来,因此我们无需担心。

I use F# for exploratory programming in my C# solutions. 我在C#解决方案中使用F#进行探索性编程。 Eg I fire up my C# WPF app through visual studios F# interactive console and poke around in the running application. 例如,我通过可视化工作室F#交互式控制台启动我的C#WPF应用程序,并在正在运行的应用程序中查找。 Can save you a lot of edit-compile-debug sessions.. 可以为您节省大量的编辑 - 编译 - 调试会话..

Languages are just a tool. 语言只是一种工具。 Like Luke, I'm a big fan of using the right tool for the job. 像Luke一样,我非常喜欢使用合适的工具来完成工作。 If a particular app would benefit from using both C# and F#, then mixing seems reasonable to me. 如果一个特定的应用程序将受益于同时使用C#和F#,那么混合对我来说似乎是合理的。

As for how to do it, see: 至于如何做,请参阅:

Can you mix .net languages within a single project? 你能在一个项目中混合使用.net语言吗?

Bottom line: you can merge two DLLs that use different languages into a single DLL as a post-build step. 结论:您可以将使用不同语言的两个DLL合并为一个DLL作为构建后步骤。 Or, you can use multiple languages in a web site that's compiled on the server-side. 或者,您可以在服务器端编译的网站中使用多种语言。

Yes, in future I think that we must combine these kinds of languages like OOP (C#) with Functional language (F#) to take advantage of multicore processing. 是的,将来我认为我们必须将OOP(C#)等语言与功能语言(F#)结合起来,以利用多核处理。 C# 4 has classes to support this but some things are done better in F#. C#4有类支持这个,但有些事情在F#中做得更好。

Have not tried it yet, but one place where I sure would like to use functional language, even though this might not be a reason to use it, is so that I can avoid what has been describe here 还没有尝试过,但是我肯定想使用函数式语言的地方,尽管这可能不是使用它的理由,这样我就可以避免这里描述的内容了

http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

I would like to just name this as a verb, and execute it 我想把它命名为动词,并执行它

calculateSomething() instead of SomtingCalculator.Execute() calculateSomething()而不是SomtingCalculator.Execute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM