How to share domain classes and business rules between client and server like in RIA Services by writing them only once

让人想犯罪 __ 提交于 2019-12-24 01:01:46

问题


In an ASP.NET - WCF application I want to share domain classes and business rules between client and server without rewriting them, just like in Silverlight RIA Services. Collecting these in one assembly and referencing this from client and server can solve the problem, but how: by adding service refence to client will generate these classes in client proxy, without any behaviour (methods). How can I achive this?

NOTE: RIA Services will not be used.

EDIT: After some Googling I came across CSLA.NET. Can it solve this issue?


回答1:


Ok here is how I've done it: As blowdart said I put all the domain code that I want to share between server and client into a seperate assembly. Then I had both server and client referencing to this shared assembly. Then added service reference to client, with one exception: In add service reference dialog there is a button "Advanced". There I have checked reuse types in referenced assemblies. This way I had all proxy goodies, asynchronous method calls etc., generated for me.




回答2:


You avoid using the client proxy altogether.

So first off, put your contract classes into a shared assembly, and add a reference to the project in both the server and client programs. In the client you can then use ChannelFactory to create a connection to the WCF service and exchange data; something like

ChannelFactory<IServiceContract> factory;
factory = new ChannelFactory<IServiceContract>("");

IServiceContract proxy = factory.CreateChannel();
using(proxy as IDisposable)
{
   proxy.MyMethod();
}


来源:https://stackoverflow.com/questions/1897671/how-to-share-domain-classes-and-business-rules-between-client-and-server-like-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!