如何使我的库变得简单,同时又针对不同的框架?
我创建了一个名为 Chargify.NET 的 API 包装器,我开始看到一种可能的模式开发通过针对 .NET 4 解决了这个
问题。问题是,随着 API 的增强,我需要创建越来越多的重载函数来处理一个特定操作(在本例中为创建订阅)。现在,我有很多(我认为太多)重载的 CreateSubscription 函数,并且管理不同的签名变得越来越困难。
是否可以(和/或建议)针对 .NET 4 构建该库并使用可选参数,并希望该库的用户可以使用 .NET 4 库?或者我应该继续沿着 3.5 的道路前进吗?或者以某种方式针对两者?
需要对此进行一些讨论..
I created an API wrapper called Chargify.NET and I'm starting to see a pattern develop that could potentially be solved by targeting .NET 4.
The issue is that as they enhance the API, I need to create more and more overloaded functions to handle one specific action (Creating a subscription, in this case). Right now, I have MANY (I think too many) overloaded CreateSubscription functions, and it's getting hard to manage the different signatures.
IS it possible (and/or suggested) to build the library against .NET 4 and use optional parameters and hope that the users of the library can use the .NET 4 library? Or should I continue along the path I'm on with 3.5? Or somehow target both?
Need some discussion about this ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
早在 4.0 之前,.NET 就完全支持可选参数。然而,他们最近才获得 C# 的语言支持。请记住,符合 CLS 的语言并不需要完全支持可选参数 - 编译器可以忽略您提供的默认值。
Optional parameters have been fully supported by .NET well before 4.0. However, they have only recently gotten language support from C#. Remember that optional parameters are not required to be fully supported by a CLS compliant language - the compiler is allowed to ignore the default values you provide.
确实没有正确答案。如果您开始使用 .NET 4 功能,您可能只想以 .NET 4 为目标,因为尝试同时针对这两个功能要么会限制您使用较旧的功能(意味着没有理由升级),要么需要 2 个代码路径(不利于维护) 。
使用 .NET 4 会将您的目标市场限制为那些使用 .NET 4 的人。这实际上取决于您来决定新功能是否具有额外的灵活性和强大功能,以及简化的 API(即:可选参数功能)是否适用。值得限制你的受众。
如果您的受众是商业商店,这可能是一个大问题 - 如果您的目标用户是小型开发人员,或者主要是其他开源开发人员 - 他们很可能会更愿意升级到 .NET 4 以使用您的包装器(如果它是)必需的。
There's really no right answer. If you start using .NET 4 features, you'll probably want to just target .NET 4, since trying to target both will either limit you to older features (meaning no reason to upgrade) or require 2 code paths (bad for maintenance).
Using .NET 4 will limit your target market to those people using .NET 4. It's really up to you to decide whether the extra flexibility and power that comes with the new features, and whether the simplified API (ie: optional parameter capabilities) is worth limiting your audience.
If your audience is commercial shops, this may be a big issue - if your target users are small developers, or mainly other open source developers - chances are they'll be much more willing to upgrade to .NET 4 to use your wrapper if it's required.
您也知道,当 C# 编译器编译您的程序并且您的程序使用可选参数时,编译器所做的就是用这些可选参数替换重载函数。因此,切换到可选参数不会获得速度或代码减少方面的好处。
Just so you know too, when the C# Compiler compiles your program and your program uses optional parameters, all the compiler does is substitute those optional parameters for overloaded functions. So you will gain no speed or code reduction benefit by switching to optional parameters.