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

 

 

目录
相关文章
|
Shell 网络安全 开发工具
服务器已经搭建好的项目如何关联至gitee对应仓库并且将服务器的项目代码推送至gitee-优雅草卓伊凡
服务器已经搭建好的项目如何关联至gitee对应仓库并且将服务器的项目代码推送至gitee-优雅草卓伊凡
846 5
|
Unix 应用服务中间件 索引
服务器数据恢复—LUN映射出错导致文件系统共享冲突的数据恢复案例
SUN光纤存储系统中有一组由6个硬盘组建的RAID6,划分为若干LUN,MAP到跑不同业务的服务器上,这些服务器上运行的是SOLARIS操作系统。 服务器不存在物理故障。由于公司业务变化,需要增加一台服务器跑新的应用。服务器管理员在原服务器在线的状态下,将其中一个lun映射到一台新服务器上。实际上,这个刚映射过去的卷已经map到了solaris生产系统上的某个lun上了。映射到新服务器后,服务器对这个卷进行初始化的操作,原solaris系统上的磁盘报错,重启服务器后这个卷已经无法挂载。 服务器管理员寻求sun原厂工程师的帮助。sun工程师检测后执行了fsck操作。执行完成后文件系统挂载成功。查
|
弹性计算 Ubuntu Linux
阿里云服务器镜像怎么选?公共/自定义/共享/云市场/社区镜像区别与适用场景梳理
在购买阿里云服务器的过程中,选择合适的镜像(即云服务器的操作系统)是至关重要的一步。阿里云服务器镜像涵盖了公共镜像、自定义镜像、共享镜像、云市场镜像(镜像市场)和社区镜像等多种类型,对于新手用户来说,面对这些不同类型的镜像,往往会感到困惑,不知道它们之间的区别,更不知道如何根据自身需求进行选择。本文为大家解析这些镜像的特点、区别,并为大家提供选择参考。
2064 60
|
存储 开发工具 git
[Git] 深入理解 Git 的客户端与服务器角色
Git 的核心设计理念是分布式,每个仓库既可以是客户端也可以是服务器。通过 GitHub 远程仓库和本地仓库的协作,Git 实现了高效的版本管理和代码协作。GitHub 作为远程裸仓库,存储项目的完整版本历史并支持多客户端协作;本地仓库则通过 `.git` 文件夹独立管理版本历史,可在离线状态下进行提交、回滚等操作,并通过 `git pull` 和 `git push` 与远程仓库同步。这种分布式特性使得 Git 在代码协作中具备强大的灵活性和可靠性。
[Git] 深入理解 Git 的客户端与服务器角色
|
人工智能 搜索推荐 程序员
用 Go 语言轻松构建 MCP 客户端与服务器
本文介绍了如何使用 mcp-go 构建一个完整的 MCP 应用,包括服务端和客户端两部分。 - 服务端支持注册工具(Tool)、资源(Resource)和提示词(Prompt),并可通过 stdio 或 sse 模式对外提供服务; - 客户端通过 stdio 连接服务器,支持初始化、列出服务内容、调用远程工具等操作。
3449 5
|
存储 人工智能 自然语言处理
ChatMCP:基于 MCP 协议开发的 AI 聊天客户端,支持多语言和自动化安装 MCP 服务器
ChatMCP 是一款基于模型上下文协议(MCP)的 AI 聊天客户端,支持多语言和自动化安装。它能够与多种大型语言模型(LLM)如 OpenAI、Claude 和 OLLama 等进行交互,具备自动化安装 MCP 服务器、SSE 传输支持、自动选择服务器、聊天记录管理等功能。
3278 16
ChatMCP:基于 MCP 协议开发的 AI 聊天客户端,支持多语言和自动化安装 MCP 服务器
|
网络协议 开发者 Python
Socket如何实现客户端和服务器间的通信
通过上述示例,展示了如何使用Python的Socket模块实现基本的客户端和服务器间的通信。Socket提供了一种简单且强大的方式来建立和管理网络连接,适用于各种网络编程应用。理解和掌握Socket编程,可以帮助开发者构建高效、稳定的网络应用程序。
881 10
|
机器学习/深度学习 人工智能 开发者
DeepSeek服务器繁忙?拒绝稍后再试!基于阿里云PAI实现0代码一键部署DeepSeek-V3和DeepSeek-R1大模型
阿里云PAI平台支持零代码一键部署DeepSeek-V3和DeepSeek-R1大模型,用户可轻松实现从训练到部署再到推理的全流程。通过PAI Model Gallery,开发者只需简单几步即可完成模型部署,享受高效便捷的AI开发体验。具体步骤包括开通PAI服务、进入控制台选择模型、一键部署并获取调用信息。整个过程无需编写代码,极大简化了模型应用的门槛。
827 7
|
开发框架 .NET C#
在 ASP.NET Core 中创建 gRPC 客户端和服务器
本文介绍了如何使用 gRPC 框架搭建一个简单的“Hello World”示例。首先创建了一个名为 GrpcDemo 的解决方案,其中包含一个 gRPC 服务端项目 GrpcServer 和一个客户端项目 GrpcClient。服务端通过定义 `greeter.proto` 文件中的服务和消息类型,实现了一个简单的问候服务 `GreeterService`。客户端则通过 gRPC 客户端库连接到服务端并调用其 `SayHello` 方法,展示了 gRPC 在 C# 中的基本使用方法。
717 5
在 ASP.NET Core 中创建 gRPC 客户端和服务器
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
1093 10

热门文章

最新文章