Skip to content

Commit

Permalink
MessageManager.Domain.DomainService 调整
Browse files Browse the repository at this point in the history
  • Loading branch information
yuezhongxin committed Jul 11, 2014
1 parent 96f7a1b commit 2484932
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 325 deletions.
107 changes: 5 additions & 102 deletions MessageManager.Domain.Tests/MessageDomainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
using MessageManager.Domain.DomainService;
using MessageManager.Domain.Entity;
using MessageManager.Domain.Repositories;
using MessageManager.Domain.ValueObject;
using MessageManager.Repositories;
using MessageManager.Repositories.EntityFramework;
using System;
using System.Collections.Generic;
using Xunit;

namespace MessageManager.Domain.Tests
Expand All @@ -28,105 +25,11 @@ public class MessageDomainTest
[Fact]
public void DomainTest_SendMessage()
{
SendMessageService service = new SendMessageService(
new MessageRepository(new EntityFrameworkRepositoryContext()),
new UserRepository(new EntityFrameworkRepositoryContext()));
//Assert.NotNull(service.SendMessage("test", "test", "xiaocai", "小菜"));
Message message = service.SendMessage("test", "test", "xiaocai", "小菜");
Assert.NotNull(messageRepository.GetByKey(message.ID));
}

/// <summary>
/// 阅读单条消息
/// </summary>
[Fact]
public void DomainTest_ReadSingleMessage()
{
ReadMessageService service = new ReadMessageService(
new MessageRepository(new EntityFrameworkRepositoryContext()),
new UserRepository(new EntityFrameworkRepositoryContext()));
Message message = service.ReadSingleMessage("1", "xiaocai");
Console.WriteLine("ID:" + message.ID);
Console.WriteLine("Title:" + message.Title);
Console.WriteLine("Content:" + message.Content);
Console.WriteLine("SendTime:" + message.SendTime);
Console.WriteLine("State:" + (message.State == MessageState.Read ? "Read" : "NoRead"));
Console.WriteLine("SenderDisplayName:" + message.SendUser.DisplayName);
Console.WriteLine("ReceiverDisplayName:" + message.ReceiveUser.DisplayName);
}

/// <summary>
/// 回复消息
/// </summary>
[Fact]
public void DomainTest_ReplyMessage()
{
ReplyMessageService service = new ReplyMessageService(
new MessageRepository(new EntityFrameworkRepositoryContext()),
new UserRepository(new EntityFrameworkRepositoryContext()));
//Assert.NotNull(service.ReplyMessage("test", "test", "xiaocai", "小菜"));
Message message = service.ReplyMessage("test", "test", "xiaocai", "小菜");
Assert.NotNull(messageRepository.GetByKey(message.ID));
}

/// <summary>
/// 删除消息
/// </summary>
[Fact]
public void DomainTest_DeleteMessage()
{
DeleteMessageService service = new DeleteMessageService(
new MessageRepository(new EntityFrameworkRepositoryContext()),
new UserRepository(new EntityFrameworkRepositoryContext()));
//Assert.True(service.DeleteMessage("1", "xiaocai"));
service.DeleteMessage("1", "xiaocai");
Assert.Null(messageRepository.GetByKey("1"));
}

/// <summary>
/// 阅读发件箱
/// </summary>
[Fact]
public void DomainTest_ReadOutbox()
{
ReadMessageService service = new ReadMessageService(
new MessageRepository(new EntityFrameworkRepositoryContext()),
new UserRepository(new EntityFrameworkRepositoryContext()));
ICollection<Message> messages = service.ReadOutbox("xiaocai");
foreach (Message message in messages)
{
Console.WriteLine("ID:" + message.ID);
Console.WriteLine("Title:" + message.Title);
Console.WriteLine("Content:" + message.Content);
Console.WriteLine("SendTime:" + message.SendTime);
Console.WriteLine("State:" + (message.State == MessageState.Read ? "Read" : "NoRead"));
Console.WriteLine("SenderDisplayName:" + message.SendUser.DisplayName);
Console.WriteLine("ReceiverDisplayName:" + message.ReceiveUser.DisplayName);
Console.WriteLine("=====================================");
}
}

/// <summary>
/// 阅读收件箱
/// </summary>
[Fact]
public void DomainTest_ReadInbox()
{
ReadMessageService service = new ReadMessageService(
new MessageRepository(new EntityFrameworkRepositoryContext()),
new UserRepository(new EntityFrameworkRepositoryContext()));
ICollection<Message> messages = service.ReadInbox("xiaocai");
foreach (Message message in messages)
{
Console.WriteLine("ID:" + message.ID);
Console.WriteLine("Title:" + message.Title);
Console.WriteLine("Content:" + message.Content);
Console.WriteLine("SendTime:" + message.SendTime);
Console.WriteLine("State:" + (message.State == MessageState.Read ? "Read" : "NoRead"));
Console.WriteLine("SenderDisplayName:" + message.SendUser.DisplayName);
Console.WriteLine("ReceiverDisplayName:" + message.ReceiveUser.DisplayName);
Console.WriteLine("=====================================");
}
ISendMessageService servcie = new SendMessageService();
User sendUser = new User("", "");
User receiveUser = new User("", "");
Message message = new Message("test", "test", sendUser, receiveUser);
Assert.True(servcie.SendMessage(message, sendUser, receiveUser));
}
}
}
45 changes: 0 additions & 45 deletions MessageManager.Domain/DomainService/DeleteMessageService.cs

This file was deleted.

13 changes: 13 additions & 0 deletions MessageManager.Domain/DomainService/ISendMessageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* author:xishuai
* address:https://www.github.com/yuezhongxin/MessageManager
**/

using MessageManager.Domain.Entity;
namespace MessageManager.Domain.DomainService
{
public interface ISendMessageService
{
bool SendMessage(Message message, User sendUser, User receiveUser);
}
}
82 changes: 0 additions & 82 deletions MessageManager.Domain/DomainService/ReadMessageService.cs

This file was deleted.

50 changes: 0 additions & 50 deletions MessageManager.Domain/DomainService/ReplyMessageService.cs

This file was deleted.

36 changes: 3 additions & 33 deletions MessageManager.Domain/DomainService/SendMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,17 @@
**/

using MessageManager.Domain.Entity;
using MessageManager.Domain.Repositories;
using System;
namespace MessageManager.Domain.DomainService
{
/// <summary>
/// SendMessage领域服务实现
/// </summary>
public class SendMessageService
public class SendMessageService : ISendMessageService
{
#region Private Fields
private readonly IMessageRepository messageRepository;
private readonly IUserRepository userRepository;
#endregion

#region Ctor
public SendMessageService(IMessageRepository messageRepository, IUserRepository userRepository)
{
this.messageRepository = messageRepository;
this.userRepository = userRepository;
}
#endregion

public Message SendMessage(string title, string content, string senderLoginName, string receiverDisplayName)
public bool SendMessage(Message message, User sendUser, User receiveUser)
{
User sendUser = userRepository.GetUserByLoginName(senderLoginName);
if (sendUser == null)
{
throw new Exception("发送失败,未获取到发件人信息");
}
User receiveUser = userRepository.GetUserByDisplayName(receiverDisplayName);
if (receiveUser == null)
{
throw new Exception("发送失败,未获取到收件人信息");
}
Message message = new Message(title, content, sendUser, receiveUser);
if (messageRepository.GetMessageCount(sendUser, DateTime.Now) > 200)
{
throw new Exception("发件人一天之内只能发送200个短消息");
}
messageRepository.Add(message);
return message;
throw new NotImplementedException();
}
}
}
10 changes: 0 additions & 10 deletions MessageManager.Domain/Entity/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,5 @@ public User(string loginName, string displayName)
public string DisplayName { get; private set; }
public virtual ICollection<Message> SendMessages { get; set; }
public virtual ICollection<Message> ReceiveMessages { get; set; }

public void SendMessage(User receiveUser, Message message)
{
this.SendMessages.Add(message);
receiveUser.ReceiveMessage(this, message);
}
private void ReceiveMessage(User sendUser, Message message)
{
this.ReceiveMessages.Add(message);
}
}
}
4 changes: 1 addition & 3 deletions MessageManager.Domain/MessageManager.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DomainService\ReadMessageService.cs" />
<Compile Include="DomainService\DeleteMessageService.cs" />
<Compile Include="DomainService\ReplyMessageService.cs" />
<Compile Include="DomainService\ISendMessageService.cs" />
<Compile Include="Entity\Message.cs" />
<Compile Include="ValueObject\MessageState.cs" />
<Compile Include="Entity\User.cs" />
Expand Down

0 comments on commit 2484932

Please sign in to comment.