铁轨 + C# - 重用模型
我正在开发一个项目,该项目有一个 Rails 网站和一个使用相同数据库和数据模型的 C# GUI。我想分享两个部分之间的(活动)模型。关于这如何可能的任何想法?
I'm working on a project that has a website in Rails and a C# GUI that use the same database and data models. I'd like to share the (active)models between the two parts. Any ideas on how this is possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IronRuby 正是为此而设计的。
您需要使用 IronRuby 运行 Ruby on Rails(您也可以通过 IIS 执行此操作),然后您可以像调用 Ruby 库一样调用 C# 程序集。
一些帮助您入门的资源:
IronRuby is exactly for that.
You will need to run Ruby on Rails with IronRuby (you can do that via IIS too) and then you can call your C# assemblies like they were Ruby libraries.
Some resources to get you started:
简短的回答是,如果不在 C# gui 上重新定义它们,就无法做到这一点。
您可以将控制器公开为 WSDL/SOAP 而不是其余的东西,然后您可以让 Visual Studio 为您生成代理。 C# 在动态方面确实很糟糕,因此您不能只采用 ruby 模型并在 C# 中重用它们,因为它们需要编译。
您当然可以为 Rails 的“宁静”资源编写自己的代理生成工具,但最终您将不得不以某种方式复制模型代码。
就像 shay 所说的那样,您可以使用 IronRuby 来弥补差距,但这取决于您是否想要重用 ActiveRecord 类,或者您是否使用网站作为获取数据的点?
当您想要重用 activerecord 模型时,IronRuby 是与数据库对话的方法(但为了与 C# 良好集成,您必须围绕模型创建强类型包装器,所以我真的不明白这一点) 。
The short answer is you can't without redefining them on the C# gui.
You can expose the controllers as WSDL/SOAP instead of the restful stuff, then you can have visual studio generate the proxies for you. C# is really bad at dynamic stuff so you can't just take the ruby models and reuse them in C# because they need to be compiled.
You can of course write your own proxy generation tool for the "restful" resources of rails but at the end of the day you'll have to duplicate the model code somehow.
Like shay says you can use IronRuby to bridge the gap but it depends do you want to reuse the ActiveRecord classes or are you using the website as your point to get to the data?
When you want to reuse the activerecord models then IronRuby is the way to go to talk to your database (but to get good integration with C# you would have to create strong typed wrappers around the models, so I don't really see the point).
我首先想到的是公开 RESTful Web 服务 来自 Rails 应用程序,并在 C# 客户端应用程序中使用这些服务。如果您希望在 C# 应用程序中使用 POCO,我不知道有什么方法可以避免重复 - 也许可以编写一个 CodeSmith 或 T4 模板来读取 schema.rb 并生成 POCO。
First thing that comes to mind for me would be to expose RESTful web services from the Rails application, and consume those services in your C# client application. I don't know of a way to avoid duplication if you want POCO's in your C# application - maybe write a CodeSmith or T4 template that reads schema.rb and generates POCO's.