使用Spring.Net进行WebService开发(二)分层设计
来源:互联网 发布:wget mysql 5.7 编辑:程序博客网 时间:2024/06/06 03:58
在上一节中,我们把所有功能都放在了一个项目里,实际实施中这显然是不可取的,针对Spring.Net的特点,我们对项目进行分层设计,最终我们的Web项目里将只有配置文件,没有其他代码。
建立如下类库项目:
MemoBoxServer.IBLL 业务逻辑层接口
MemoBoxServer.IDAL 数据访问层接口
MemoBoxServer.BLL 业务逻辑层实现
MemoBoxServer.DAL 数据访问层实现
MemoBoxServer.Model 领域模型
MemoBoxServer.DTO 数据传输模型
他们之间的引用关系:
IBLL 引用 DTO
IDAL 引用 Model
BLL 引用 IBLL,IDAL,Model,DTO
DAL 引用 IDAL,Model
DTO 引用 Model
在IBAL层所开放的对外WebService接口中,所有对象的传输都应使用DTO中的定义,而与IDAL相关的操作,都应使用Model中定义的对象,我觉得主要原因有两个:一是技术上来说,如果使用ORM框架,那Model中的对象会被框架托管,生成代理对象,这时Soap进行序列化时会出问题。再来,即使不存在这种问题,直接暴露Model结构对安全性也是一个威胁,因为一般Model对象结构与数据表结构基本对应。
我们在Model中创建User对象:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MemoBoxServer.Model{ public class User { public virtual int Id { get; set; } public virtual string Username { get; set; } public virtual string Password { get; set; } }}
所有字段都是public virtual的自动属性,这是为后面使用NHibernate做准备,现在暂时可以无视。
关于User的数据操作,我们在IDAL中创建Dao接口:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using MemoBoxServer.Model;namespace MemoBoxServer.IDAL{ public interface IUserDao { User Save(User user); User SaveOrUpdate(User user); void Delete(User user); User FindById(int id); User FindByUsername(string username); }}在DAL中实现这个接口,前四个操作是所有类型的Model对象都应该具备的,我们定义在这里,暂时不去管它,我们也暂时不连接数据库,用mock数据实现FindByUsername操作:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using MemoBoxServer.IDAL;using MemoBoxServer.Model;namespace MemoBoxServer.DAL{ public class UserDao : IUserDao { public User Save(User user) { throw new NotImplementedException(); } public User SaveOrUpdate(User user) { throw new NotImplementedException(); } public void Delete(User user) { throw new NotImplementedException(); } public User FindById(int id) { throw new NotImplementedException(); } public User FindByUsername(string username) { if (username != "123456") return null; User mock = new User() { Id = 1, Username = username, Password = "123456" }; return mock; } }}
在IBLL中添加服务接口:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MemoBoxSevrer.IBLL{ public interface IPortalService { bool Login(string username, string password); }}
在BLL中实现该服务接口,实现的过程中,我们发现需要调用IUserDao来操作数据,怎么办?先不管这么多,既然我要,我就先定义一个放在这,定义一个public的自动属性,然后写完实现:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using MemoBoxSevrer.IBLL;using MemoBoxServer.IDAL;using MemoBoxServer.Model;namespace MemoBoxServer.BLL{ public class PortalService : IPortalService { public IUserDao UserDao { get; set; } public bool Login(string username, string password) { User user = UserDao.FindByUsername(username); if (user != null && user.Password == password) return true; else return false; } }}这里有一点要注意的是,这里用的不是UserDao的实现类,而是接口,并且BLL层也并没有引用DAL,而是引用的IDAL
好了,准备工作就绪,下面回到我们的Web项目进行配置,Web项目把我们新建的所有项目都引用上,创建如下目录结构和文件:
编辑Web.config
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> </sectionGroup> </configSections> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/> </httpHandlers> </system.web> <spring> <context> <resource uri="file://~Spring/Dao/user.dao.xml"/> <resource uri="file://~Spring/Service/portal.service.xml"/> </context> </spring></configuration>
编辑protal.service.xml
<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net" xmlns:aop="http://www.springframework.net/aop"> <object id="MemoBoxServer.BLL.PortalService" type="MemoBoxServer.BLL.PortalService, MemoBoxServer.BLL" scope="request"> </object> <object id="PortalService" type="Spring.Web.Services.WebServiceExporter, Spring.Web"> <property name="TargetName" value="MemoBoxServer.BLL.PortalService"/> <property name="Namespace" value="http://tempuri.org/"/> <property name="Description" value="MemoBoxServer.BLL.PortalService"/> <property name="MemberAttributes"> <dictionary> <entry key="*"> <object type="System.Web.Services.WebMethodAttribute, System.Web.Services"> </object> </entry> </dictionary> </property> </object></objects>
编辑user.dao.xml
<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net" xmlns:aop="http://www.springframework.net/aop"> <object id="UserDao" type="MemoBoxServer.DAL.UserDao, MemoBoxServer.DAL"> </object></objects>好了现在运行一下试试,调用Login,输入数据,提交。。等等,报错了!
System.NullReferenceException: 未将对象引用设置到对象的实例。 在 MemoBoxServer.BAL.PortalService.Login(String username, String password) 位置 C:\Users\zhoucong-pc\documents\visual studio 2010\Projects\MemoBoxServer\MemoBoxServer.BAL\PortalService.cs:行号 17 在 PortalService.Login(String username, String password)的确,我们只是在PortalService中把UserDao定义出来了,那这个UserDao在哪创建,谁来提供呢?当然是Spring.Net了,在Spring.Net相关的objects配置节中,每一个object配置节就对应Spring容器中的一个对象,详细的配置请查看Spring.Net文档。
我们在user.dao.xml中已经创建了UserDao对象了,他的id就是他在容器中的标识符,现在我们把它注入到PortalService中去,修改protal.service.xml的MemoBoxServer.BAL.PortalService对象:
<object id="MemoBoxServer.BLL.PortalService" type="MemoBoxServer.BLL.PortalService, MemoBoxServer.BlL" scope="request"> <property name="UserDao" ref="UserDao" /></object>
注意新加的这一行他表示把标示符(ref)为UserDao的对象注入到MemoBoxServer.BAL.PortalService的名称(name)为UserDao属性中去,运行试试,现在成功了。如果不对的话请重新生成一下项目
关于对象创建相关配置请查看Spring.Net文档的5.2. Container overview
关于依赖注入相关配置可以查看的5.3. Dependencies
下一篇将整合NHibernate
- 使用Spring.Net进行WebService开发(二)分层设计
- 使用Spring.Net进行WebService开发(一)环境搭建
- 使用Spring.Net进行WebService开发(三)Spring.Net容器相关知识
- 【WebService】使用JDK开发WebService(二)
- spring webservice (二) 客户端开发
- spring webservice (二) 客户端开发
- 初学者VS2005 .Net WebService使用(二)- 调用WebService三步曲
- webservice开发二:使用CXF开发webservice
- 使用jax-ws开发webservice(二)
- 整合asp.net mvc4,Spring.net,nhibernate实现分层开发
- spring boot 分层demo(二)
- 使用VMware VSphere WebService SDK进行开发 (二)——获取虚拟机cpu的使用情况
- Web分层设计研究(二)
- Web分层设计研究(二)
- selenium+java+testng分层设计(二)
- 基于.NET平台的分层架构实战(二)——需求分析与数据库设计
- 基于.NET平台的分层架构实战(二)——需求分析与数据库设计
- 基于.NET平台的分层架构实战(二)——需求分析与数据库设计
- HTML 字符实体
- 用Axure制作当当首页幻灯片广告
- 字节流与字符流的区别详解
- HTML 取消超链接下划线
- android代码混淆和常见问题总结
- 使用Spring.Net进行WebService开发(二)分层设计
- 多终端适配的相关记录
- 一网打尽18种主流数据库:12种SQL+6种NoSQL
- 关于HTTP服务器每个客户端2个连接的限制
- HDU 1258 Sum It Up(哈希表判重)
- Debian自动挂载U盘,移动硬盘错误解决办法
- 在xpage的表单页面打开另一表单页面
- Django 一个简单的图书管理程序(六 添加CSV文件导入导出操作)
- ubuntu使盘符不在桌面上显示