silverlight:共享客户端和服务器之间的代码

简介: 原文转自 http://xamlgeek.net/2011/04/10/share-code-between-client-and-server/ Share code between client and server   One of the challenges of wr...

原文转自 http://xamlgeek.net/2011/04/10/share-code-between-client-and-server/

Share code between client and server

 

One of the challenges of writing managed components for Silverlight is the fact that it is not possible to use a normal .NET assembly in Silverlight projects. One way to get around this issue is to use linked and partial classes in Silverlight projects.

How to use linked classes in Silverlight

Let’s say I have a solution with 3 projects: a Silverlight client (client), a web application (web) and a .NET class library (lib). The lib contains entities and business logic that I wish to share between the client and the web.

It would be great to reference the lib from the client but that is not an option in Silverlight. Of course I can copy the content of the lib to the client, but that is really not an option either; nobody wants to maintain to sets of the same business logic.

Instead I can link the files from the lib to the client. In this sample I have a Person entity in the lib project that I wish to share with the client. In the client I choose to add an existing item and find the class from the lib project I wish to add. I click on the arrow next to the add button and select "Add as link".

clip_image001

clip_image002

This will add the Person entity class to my client project and it’s nothing more then a link to the class from the lib project. The link will make sure that the class is compiled into both projects at compile time.

The downside of this solution is that even though I can share the classes, it’s not possible to put logic into the classes that is server side or client side only. One way to get around that is to use partial classes.

Partial classes on the client and the server

In the lib project I can create a server side partial class of our business logic and I can do the same on the client. In this sample I have create a server side only version of the Person entity classes "Person.server.cs" and a client side only version of the Person entity called "Person.client.cs". The Person entity itself is still linked between the client and the server. Remember that for this to work the namespace must be the same between all partial files! This enables the separation of logic running only server or client side.

clip_image003

This solution enables me to run and share classes like business logic code on the client and the server.

You can download my sample here.

by xamlgeek
@thomasmartinsen

 

 

目录
相关文章
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
2月前
|
消息中间件 运维 网络协议
客户端和服务器之间的通信
客户端和服务器之间的通信
32 0
|
2月前
|
监控 关系型数据库 Linux
|
18天前
|
网络协议 Python
pythonTCP客户端编程连接服务器
【4月更文挑战第6天】本教程介绍了TCP客户端如何连接服务器,包括指定服务器IP和端口、发送连接请求、处理异常、进行数据传输及关闭连接。在Python中,使用`socket`模块创建Socket对象,然后通过`connect()`方法尝试连接服务器 `(server_ip, server_port)`。成功连接后,利用`send()`和`recv()`进行数据交互,记得在通信完成后调用`close()`关闭连接,确保资源释放和程序稳定性。
|
1月前
|
自然语言处理 Shell 网络安全
Liunx服务器搭建SVN服务,并通过钩子实现代码自动部署
Liunx服务器搭建SVN服务,并通过钩子实现代码自动部署
32 3
|
2月前
|
网络协议 Java API
【JavaEE初阶】 TCP服务器与客户端的搭建
【JavaEE初阶】 TCP服务器与客户端的搭建
|
2月前
|
自然语言处理 Java 编译器
【JavaEE初阶】 UDP服务器与客户端的搭建
【JavaEE初阶】 UDP服务器与客户端的搭建
|
2月前
|
安全 Shell 网络安全
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
131 0