使用Apworks开发基于CQRS架构的应用程序(六):创建.NET WCF服务

在本节,我们将介绍.NET WCF服务的创建过程。

  1. Solution Explorer中,右键单击TinyLibraryCQRS,然后选择Add | New Project…菜单,这将打开Add New Project对话框
  2. Installed Templates 选项卡下,选择Visual C# | WCF,然后选择WCF Service Application,确保所选.NET版本为.NET Framework 4,在Name文本框中输入TinyLibrary.Services,然后单击OK按钮
  3. 右键单击TinyLibrary.ServicesReferences节点,然后选择Add Reference…菜单,这将打开Add Reference对话框
  4. 在.NET选项卡下,选择ApworksApworks.Bus.DirectBus以及Apworks.ObjectContainers.Unity,然后单击OK按钮
  5. 右键单击TinyLibrary.ServicesReferences节点,然后选择Add Reference…菜单,这将打开Add Reference对话框
  6. Projects选项卡下,选择TinyLibrary.EventsTinyLibrary.EventHandlersTinyLibrary.CommandsTinyLibrary.CommandHandlersTinyLibrary.Domain以及TinyLibrary.QueryObjects,然后单击OK按钮
  7. 将自动生成的Service1.svc文件删掉
  8. 右键单击TinyLibrary.Services项目,然后单击Add | New Item…菜单,这将打开Add New Item对话框
  9. Installed Templates选项卡下,选择Visual C# | Web,然后选择WCF Service,在Name文本框中,输入CommandService然后单击Add按钮
  10. 编辑CommandService.svc.cs文件,输入如下代码
       1: using System;
       2: using Apworks;
       3: using Apworks.Bus;
       4: using Apworks.Generators;
       5: using TinyLibrary.Commands;
       6:  
       7: namespace TinyLibrary.Services
       8: {
       9:     public class CommandService : ICommandService
      10:     {
      11:         private readonly ICommandBus bus = ObjectContainer.Instance.GetService<ICommandBus>();
      12:  
      13:         public long CreateReader(string loginName, string name)
      14:         {
      15:             long id = (long)IdentityGenerator.Instance.Generate();
      16:             RegisterReaderCommand command = new RegisterReaderCommand(id, loginName, name);
      17:             bus.Publish(command);
      18:             bus.Commit();
      19:             return id;
      20:         }
      21:  
      22:         public long CreateBook(string title, string publisher, DateTime pubDate, string isbn, int pages)
      23:         {
      24:             long id = (long)IdentityGenerator.Instance.Generate();
      25:             CreateBookCommand createBookCommand = new CreateBookCommand(id, 
      26:                 title, publisher, pubDate, isbn, pages, false);
      27:             bus.Publish(createBookCommand);
      28:             bus.Commit();
      29:             return id;
      30:         }
      31:  
      32:         public void BorrowBook(long readerId, long bookId)
      33:         {
      34:             BorrowBookCommand borrowBookCommand = new BorrowBookCommand(readerId, bookId);
      35:             bus.Publish(borrowBookCommand);
      36:             bus.Commit();
      37:         }
      38:  
      39:         public void ReturnBook(long readerId, long bookId)
      40:         {
      41:             ReturnBookCommand returnBookCommand = new ReturnBookCommand(readerId, bookId);
      42:             bus.Publish(returnBookCommand);
      43:             bus.Commit();
      44:         }
      45:     }
      46: }
  11. 用上面相同的方法创建一个QueryService.svc,然后编辑QueryService.svc.cs文件,输入如下代码
       1: using System;
       2: using System.Collections.Generic;
       3: using Apworks;
       4: using Apworks.Queries.Storage;
       5: using Apworks.Storage;
       6: using TinyLibrary.QueryObjects;
       7:  
       8: namespace TinyLibrary.Services
       9: {
      10:     public class QueryService : IQueryService
      11:     {
      12:         private IQueryObjectStorage GetQueryStorage()
      13:         {
      14:             return ObjectContainer.Instance.GetService<IQueryObjectStorage>();
      15:         }
      16:  
      17:         public BookObject GetBook(long id)
      18:         {
      19:             using (IQueryObjectStorage queryObjectStorage = this.GetQueryStorage())
      20:             {
      21:                 var pb = new PropertyBag();
      22:                 pb.Add("AggregateRootId", id);
      23:                 return queryObjectStorage.SelectFirstOnly<BookObject>(pb);
      24:             }
      25:         }
      26:  
      27:         public ReaderObject GetReader(long id)
      28:         {
      29:             using (IQueryObjectStorage queryObjectStorage = this.GetQueryStorage())
      30:             {
      31:                 var pb = new PropertyBag();
      32:                 pb.Add("AggregateRootId", id);
      33:                 return queryObjectStorage.SelectFirstOnly<ReaderObject>(pb);
      34:             }
      35:         }
      36:  
      37:         public ReaderObject GetReaderByLogin(string loginName)
      38:         {
      39:             using (IQueryObjectStorage queryObjectStorage = this.GetQueryStorage())
      40:             {
      41:                 var pb = new PropertyBag();
      42:                 pb.Add("LoginName", loginName);
      43:                 return queryObjectStorage.SelectFirstOnly<ReaderObject>(pb);
      44:             }
      45:         }
      46:  
      47:         public IEnumerable<BookObject> GetAllBooks()
      48:         {
      49:             using (IQueryObjectStorage queryObjectStorage = this.GetQueryStorage())
      50:             {
      51:                 return queryObjectStorage.Select<BookObject>();
      52:             }
      53:         }
      54:  
      55:         public IEnumerable<RegistrationObject> GetBookRegistrationHistory(long bookId)
      56:         {
      57:             using (IQueryObjectStorage qos = this.GetQueryStorage())
      58:             {
      59:                 PropertyBag pbCriteria = new PropertyBag();
      60:                 pbCriteria.Add("BookAggregateRootId", bookId);
      61:  
      62:                 PropertyBag pbSort = new PropertyBag();
      63:                 pbSort.AddSort<DateTime>("RegistrationDate");
      64:                 return qos.Select<RegistrationObject>(pbCriteria, pbSort, Apworks.Storage.SortOrder.Descending);
      65:             }
      66:         }
      67:  
      68:  
      69:         public IEnumerable<RegistrationObject> GetReaderRegistrationHistory(long readerId)
      70:         {
      71:             using (IQueryObjectStorage qos = this.GetQueryStorage())
      72:             {
      73:                 PropertyBag pbCriteria = new PropertyBag();
      74:                 pbCriteria.Add("ReaderAggregateRootId", readerId);
      75:  
      76:                 PropertyBag pbSort = new PropertyBag();
      77:                 pbSort.AddSort<DateTime>("RegistrationDate");
      78:                 return qos.Select<RegistrationObject>(pbCriteria, pbSort, Apworks.Storage.SortOrder.Descending);
      79:             }
      80:         }
      81:     }
      82: }
  12. 右键单击TinyLibrary.Services项目,然后选择Add | New Item…菜单,这将打开Add New Item对话框
  13. Installed Templates选项卡下,选择Visual C# | Web,然后选择Global Application Class,然后单击Add按钮
  14. 修改Global.asax.cs文件如下
       1: using System;
       2: using Apworks.Application;
       3:  
       4: namespace TinyLibrary.Services
       5: {
       6:     public class Global : System.Web.HttpApplication
       7:     {
       8:         private readonly IBootStrapper bootStrapper = BootStrapperFactory.Create();
       9:  
      10:         protected void Application_Start(object sender, EventArgs e)
      11:         {
      12:             bootStrapper.BootStrap();
      13:         }
      14:  
      15:         // ... other methods omitted here.
      16:     }
      17: }

Global.asax文件中定义了,当应用程序启动时,同时会启动Apworks系统。首先,Apworks.Application.BootStrapperFactory类创建了启动实例,然后使用BootStrap方法来初始化应用程序。这个过程是在Apworks的配置文件中定义的,将在后续文章中讨论。

你可能感兴趣的:(.net)