Identifying Domain Services & Application Services when doing DDD

-I’m trying to figure out how to identify Application Services in my application. I think I can identify a Domain service by 2 things:

  1. It acts as a facade to the repository.
  2. It holds business logic that can’t be encapsulated in a single entity.

So I thought about this simple use case that I’m currently working on:

An admin should be able to ban a certain user. The operation must be logged and an email must be sent to the banned user.

I have a UserRepository which has a function getUserById(). I have a User entity which has a ban() function.
I’ll create a UserService like this:

class UserService{
    static void banUser(int userId){
          User user= UserRepository.getUserById(userId);
          user.ban();
          UserRepository.update(user);
    }
}

The userId is a POST variable that I’ll be receiving in my Controller from a web form.

Now where does the logging go? I need to log the ban process twice before and after the operation ex:

Logger.log("Attempting to ban user with id:"+userId);
//user gets banned 
Logger.log("user banned successfully.");

Second, I need to send a mail to the user. where should the call go?

I thought about putting the logging and email is the UserService class itself, but I think there are better solutions.

Third, where do Application Services fit in all of this?

4

class UserService{
    public public UserService(IUserRepository repo, INotificationService notification)
    {
      this.UserRepository = repo;
      this.Notification = notification;
    }
    public void BanUser(int userId){
          User user= UserRepository.getUserById(userId);
          user.ban();
          Notification.NotifyBanned(user);
    }
}

When it comes to logging then you may choose a lot of solutions. Just log in BanUser method. Create logger per class (like log4j/log4net users do). Or you can inject ILogger in ctor. Or maybe it is some kind of Audit trail. Then maybe IUserAuditTrail would be better?


public interface IUserAuditTrail
{
    void UserBanned(User user)
    void AccountCreated(User user)
}

or sth like that.
Or you can add loggin AOP style. Decorate UserService with attribute and use PostSharp or use IoC container and configure interceptor to handle logging for you. Unless logging operations isn’t business function and instead it’s just for programmers I would go for interceptors. Otherwise IAuditTrail or sth like that.

Domain Service vs Application Service
A domain service is a class that hold a behaviour that does not belong to any other entity or value-object in the domain. Nothing to do with repository.
If you model the user’s roles, many domain services become methods of the role itself.

Instead applicative services are the interface used by the outside world to use the domain. Thus, UserService is an applicative service.

Handling technical side effects
I use observable entities to handle such domain events.

This way:

  • the repository can observe a Banned event and update the persistence state without the need of an explict save call
  • the UserService can subscribe the event with an handler that logs and send the email just if the ban() command succeed.

Now where does the logging go?

Second, I need to send a mail to the user. where should the call go?

I would recommend Domain Events. Create two events: UserBanAttemptedEvent and UserBannedEvent (note the past time).

The logic for logging and email notifying is going inside event handlers, create two handlers UserBanAttemptedEventHandler and UserBannedEventHandler.

The *payload classes just simple DTO with useful information like: UserId etc.
This approach decouples application logic better imho

void banUser(int userId){
      DomainEvents.Raise<UserBanAttemptedEvent>(new UserBanAttemptedPayload())

      User user= UserRepository.getUserById(userId);
      user.ban();
      UserRepository.update(user);

      DomainEvents.Raise<UserBannedEvent>(new UserBannedPayload())
}

There are many implementations of these pattern, examples:

http://lostechies.com/jimmybogard/2014/05/13/a-better-domain-events-pattern/

DDDSample.Net

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật