C#:部分类和 Web 服务:分离形式和功能
我正在涉足网络服务领域,并且一直在制作一个模仿数学运算的简单网络服务。 首先,它很简单,传入两个整数,然后根据调用的方法将二元运算符应用于这些整数(加号、减号等)。
然后我决定让事情变得更复杂一点,并开始传递对象,但后来我发现 Web 服务只公开类的数据端,而不公开功能端。
有人告诉我,处理这个问题的一个好方法是使服务端的类成为分部类(类封装形式的这一侧),并在客户端有另一个分部类(类的这一侧封装功能) )。 这似乎是一种优雅的做事方式。
所以,我已经按照上面的描述设置了两个类,但它似乎并没有像我被告知的那样工作。
我正在尝试的事情可能吗? 如果是这样,我哪里出错了?
I am dabbling in the world of web services and I've been making a simple web service which mimics mathematical operations. Firstly it was simple, passing in two integers and then a binary operator would be applied to these (plus, minus etc) depending on the method called.
Then I decided to make things a little more complex and started passing objects around, but then I discovered that a web service only exposes the data side of the class and not the functional side.
I was told that a good way to deal with this is to make the class on the service side a partial class (this side of the class encapsulating form) and on the client side have another partial class (where this side of the class encapsulates functionality). This seems like an elegant way of doing things..
So, I have set up two classes as described above, but it doesn't seem to be working as I was told.
Is what I am attempting possible? If so, where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
分部类实际上是一种将自动生成的代码与开发人员代码分开的工具。
一个很好的例子是 VS 中的 Windows 窗体设计器,或者新的 DBML Linq DataContext 生成的代码。
还有一种观点认为将它们与 VSS 风格的源代码控制提供程序一起使用,其中任何时候只有一个用户可以编辑文件。
将它们用于功能的逻辑分离并不是一个好主意 - 这种划分仅存在于预编译中。 一旦编译,您就只能得到一个类,但不是一个易于调试或跟踪内部操作的类。
您所描述的听起来像是使用 WCF 合约的非常好的情况。 在这种情况下,客户端和服务器将共享一个或多个接口。
您的复杂代码将放在那里,并且可以单独进行单元测试 - 即在连接的应用程序之外。 然后,当发现错误时,您可以快速消除代码问题并转而调查与连接相关的问题。
Partial classes are really a tool to separate auto-generated code from developer code.
A good example is the windows forms designer in VS, or the new DBML Linq DataContext generated code.
There's also an argument for using them with VSS style source control providers where only one user can edit a file at any one time.
It's not a good idea to use them for logical separation of functionality - the division only exists pre-compilation. As soon as you compile you get just the one class, but not one that it's easy to debug or track operations inside.
What you've described sounds like a really good situation for using WCF contracts. In that case both client and server would share an Interface (or Interfaces).
Your complex code would go there and could be unit tested separately - i.e. outside of your connected application. Then when bugs are found you can eliminate code issues quickly and move to investigating connection related ones instead.
不适用于部分课程。 分部类是一种语法构造,使您能够在不同的源文件中拥有类的不同部分。 然而,分部类的所有部分最终都会编译成相同的二进制文件。
您可以使用扩展方法向表示数据协定的类添加功能。
您还可以尝试在共享程序集中实现该类,并使用 svcutil.exe /reference 将其导入到客户端代理中,而不是在 Web 服务命名空间中进行全新的声明。
Not with partial classes. A partial class is a syntax construct that gives you the ability to have different parts of the class in different source files. However, all parts of the partial class are ultimately compiled into the same binary.
You could use extension methods to add functionality to your class that represents the data contract.
You could also try implementing the class in a shared assembly and use the svcutil.exe /reference to get it imported in the client proxy instead of having a brand new declaration in the web service namespace.
正如 Franci 所说,它只是允许您将同一类的不同部分放入不同的文件中。
相反,你应该如何构建事物实际上取决于你正在做什么。 如果我是你,我可能会有一个相当简单的数据承载类和一个可用于处理该数据的消费者。
使用共享程序集也是一个好主意。 但是,如果您确实希望能够将代码从服务器发送到客户端,CSharpCodeProvider 就可以了。
As Franci said, it simply allows you to put separate parts of the same class into different files.
How you should structure things instead really depends on what you are doing. If I were you I would likely have a rather plain data carrying class and a consumer which could be used to process that data.
The use of a shared assembly is also a good idea. However, if you really wanted to be able to send the code from the server to the client CSharpCodeProvider would work.
(这个线程可能已经死了,但是...)我正在考虑做类似的事情,但是使用(在我的例子中)Windows 服务上的功能。
客户端程序和Windows服务都需要访问数据,但只有服务需要实际对数据执行任何操作; 它们都包含在一个 dll 中,该 dll 包含包含约定数据成员的分部类,但是我收到一条错误消息,指出该分部类与我的服务上的分部类冲突,即使它们都位于同一命名空间中,并且目前,服务器的部分类为空。
(This thread's probably dead but...) I was thinking of doing something similar, but with the functionality on the (in my case) Windows Service.
Both the client program and the Windows service need access to the data, but only the service needs to actually do anything with the data; they are both including in a dll that holds a partial class containing contracted data members, however I get an error saying this partial class conflicts with the partial class on my service even though they are both in the same namespace and at the moment, the server's partial class is empty.