Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle user sessions in Domain Driven Design

How are user sessions handled in domain driven design (in a MVC framework)?

I've got a User domain object, a UserRepository and a UserService.

I've got this method in my UserService class that logs users in.

public function login($email, $password, $remember = false)
{
    $user = $this->userRepo->findByEmail($email);

    if ($user && $user->getPassword() === $password) {
        return $user;
    }

    return false;
}

How do I keep them logged in with sessions?

How would I automatically load the user based on a session user id?

Can somebody give me an example with code how I could sustain the user in my application in DDD?

like image 692
502 Error Avatar asked Jun 29 '14 00:06

502 Error


People also ask

How can a user session can be identified?

Answer: d) All of the above Explanation: The user session can be identified with the help of authenticating users, IP address, and user agent as well.

Which of approach we can use for domain driven design?

Domain-Driven Design is a concept introduced by a programmer Eric Evans in 2004 in his book Domain-Driven Design: Tackling Complexity in Heart of Software. It is an approach for architecting software design by looking at software in top-down approach.

What problem does Domain Driven Design Solve?

This method also uses various principles and patterns to close the gap between business reality and software code. Domain-driven design is most helpful in solving domain complexities as it works to maintain the project's primary focus as the core domain.

What is event storming in DDD?

Event storming is a rapid, lightweight, and underappreciated group modeling technique that is intense, fun, and useful for accelerating development teams. The brainchild of Alberto Brandolini, it's a synthesis of facilitated group learning practices from Gamestorming and the principles of domain-driven design (DDD).


1 Answers

From a DDD perspective, managing sessions is a distinct set of behaviors, therefor deserves a dedicated service. So create such a service.

You can pass that service to your UserService as a dependency, so the UserService can use the session manager for storing authentication information.

Better yet, the concept of authentication might also be seen as a distinct set of behaviors, so create a service for that too. Pass your UserService and session manager to this authentication service as dependencies. (So the session manager is no longer a dependency of UserService.)

But even authentication could be broken down into several distinct parts, it depends on how far you want to go.

I unfortunately can't show you any code, because that would highly depend on what kind of authentication you want to perform (HTTP Basic, Form login, OAuth, etc), what level of abstraction you want to achieve, and your personal preferences.

But if you want to see what a complex system can look like, have a look at the Security Component of Symfony 2, here in the documentation and here on github.

And if you would consider using this component, you can look at how Silex implements it (github) to get a feel for how you can use it.

Side note

DDD is about much more than writing your code in a certain way. If you want to learn DDD, I suggest you read the Domain-Driven Design: Tackling Complexity in the Heart of Software (the blue book), Implementing Domain-Driven Design (the red book), or you can start of with Domain Driven Design Quickly which is available for download.

like image 123
Jasper N. Brouwer Avatar answered Sep 21 '22 11:09

Jasper N. Brouwer