项目架构开发:服务层(上)

 

上一次(即昨天),我们完成了业务逻辑层的演示;传送门:项目架构开发:业务逻辑层之领域驱动失血模型

设计上虽然还不是太优雅,但是距离真正的OO编程又更近了一步,而且也保留了事务脚本模式的简单快捷的特性

只要后续把Model层丰富起来,那就算是掌握了另一种软件开发模式,而且也是整个思想上的转变。

 

本章我们来完成在业务逻辑层之上的服务层;

服务层并不是必须的,在有一定复杂度的应用程序中,服务层非常有必要创建;而与之相反的就是,

在一些业务逻辑简单的系统中,服务层可能仅仅充当分发功能,没有多余的工作需要组织,这时服务层就显得多余;

而且,服务层一般还与客户端有关系(Web、Winform);如果是Web,在同一系统内,服务层就没有大多必要,除非是跨系统的服务调用;

如果是Winform,那服务层就比较常用,因为Winform是C/S模式,物理层上天生与服务器端分离。

若是从C端远程调用应用逻辑,那有可能在长距离的远程调用中花费太多的时间,而加入服务层情况就会不同

 

服务层就是为UI何应用逻辑层之间提供一个数据交互的契约,简单点说就是从C端发送一组参数,在S端进行逻辑运算,

因为一般情况下S端与数据库在同一局域网下,访问速度比前者快很多,效率也大大提高

若没有服务层,则需要从UI直接与后端应用程序通信,交互次数大大的增加,通信距离也大大的影响程序性能

 

我们开始吧。

 

1、应用逻辑,创建新登录用户

 1         public bool Add(AddRequest entity)
 2         {
 3             this.repository.Add(new LoginUser()
 4             {
 5                 Id = Guid.NewGuid(),
 6                 LoginName = entity.LoginName,
 7                 Password = entity.Password,
 8                 IsEnabled = entity.IsEnabled,
 9                 CreateTime = DateTime.Now
10             });
11             this.unitOfWork.Commit();
12 
13             return true;
14         }
15 
16         public bool Check(string loginName)
17         {
18             return this.repository.Count(w => w.LoginName == loginName) > 0;
19         }

 

这里添加了2个方法,功能不解释了

现在,我们假设UI层与应用层相距甚远,那直接调用显然是不妥的,我们创建一个服务项目来完成这个功能

 

2、新建WCF项目

 

IUserService.cs

 1     [ServiceContract]
 2     public interface IUserService
 3     {
 4         [OperationContract]
 5         bool Add(AddRequest entity);
 6 
 7         [OperationContract]
 8         UserList GetAll();
 9     }
10 
11     [DataContract]
12     public class UserList
13     {
14         [DataMember]
15         public IEnumerable<GetAllResponse> Items { get; set; }
16     }

UserService.svc

 1     public class UserService : IUserService
 2     {
 3         public bool Add(AddRequest entity)
 4         {
 5             var loginUserApp = new LoginUserApplication();
 6             if (loginUserApp.Check(entity.LoginName))
 7             {
 8                 throw new Exception("登录名称已被占用!");
 9             }
10 
11             loginUserApp.Add(entity);
12             return true;
13         }
14 
15         public UserList GetAll()
16         {
17             var loginUserApp = new LoginUserApplication();
18             var wcf = new UserList();
19             wcf.Items = loginUserApp.GetAll();
20 
21             return wcf;
22         }
23     }

 

也很简单明了,在Add方法中我们完成以后的校验与新增

 

3、新建客户端测试项目

 

 

UserService.cs

这个类是系统自动生成的,不用我们自己写

Program.cs

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             var userClient = new UserServiceClient();
 6             var addRequest = new Business.DTO.Request.AddRequest()
 7             {
 8                 LoginName = "LoginName" + Guid.NewGuid().ToString(),
 9                 Password = "Password",
10                 IsEnabled = 1
11             };
12             userClient.Add(addRequest);
13             Console.WriteLine(string.Format("Add finish: {0}", addRequest.LoginName));
14 
15             var result = userClient.GetAll();
16             foreach (var item in result.Items)
17             {
18                 if(item.LoginName == addRequest.LoginName)
19                     Console.WriteLine(string.Format("Found you: {0}", item.LoginName));
20             }
21 
22             Console.WriteLine("totle:" + result.Items.Count());
23             Console.WriteLine();
24 
25             Console.ReadKey();
26         }
27     }

看看效果

 

成功了啊

 

上边演示的是适合C/S架构,我们再来看看B/S的

 

4、新建WebApi

UserController.cs

 1     public class UserController : ApiController
 2     {
 3         public IEnumerable<GetAllResponse> GetAll()
 4         {
 5             var loginUserApp = new LoginUserApplication();
 6             return loginUserApp.GetAll();
 7         }
 8 
 9         public HttpResponseMessage Get(Guid id)
10         {
11             var loginUserApp = new LoginUserApplication();
12             LoginUser loginUser = loginUserApp.Get(id);
13             if (loginUser == null)
14             {
15                 return Request.CreateResponse(HttpStatusCode.NotFound);
16             }
17 
18             return Request.CreateResponse(loginUser);
19         }
20 
21         public bool Add(AddRequest user)
22         {
23             var loginUserApp = new LoginUserApplication();
24             if (loginUserApp.Check(user.LoginName))
25             {
26                 throw new Exception("登录名称已被占用!");
27             }
28 
29             return loginUserApp.Add(user);
30         }
31 }

UserControllerTest.cs

 1         public void Add()
 2         {
 3             var userController = new UserController();
 4             var flag = userController.Add(new AddRequest()
 5             {
 6                 LoginName = "LoginName" + Guid.NewGuid().ToString(),
 7                 Password = "Password",
 8                 IsEnabled = 1
 9             });
10 
11             Assert.AreEqual(true, flag);
12         }
13 
14         [TestMethod]
15         public void GetAll()
16         {
17             var userController = new UserController();
18             var list = userController.GetAll();
19             int count = list.Count();
20 
21             Assert.AreEqual(true, count > 0);
22         }

运行看看效果

看来没什么问题,成功了

 

看看完整架构

 

posted @ 2017-03-09 23:49  猫在看  阅读(3215)  评论(6编辑  收藏  举报