DDD in Symfony 2 - How to interact with the Validator service?

Some time ago I started playing with DDD. So far, all my POPO classes. Almost everything is completed, but I want to confirm my essence to perseverance.

I already know where to put the verification (commands / use case objects), but ideally I would like to use the authentication service at the application / infrastructure level. For me, validation should be part of the domain, but if I put it there, I will have a lot of duplication.

Have you encountered such a problem? Is there any reasonable solution for this? Thanks everyone!

+4
source share
1 answer

, , , .

-, - / ( ) , , URL- , , Symphony /, .

, - . , , , , - , , . , , ( ) .

, , URL- , ( ), , URL- . , URL-. ( , ), :

// This would belong to your domain...
interface IValidator
{
    public function IsValidURL($url);
}

class Foo
{
    public function SaveURL($url, $validator)
    {
        if (!$validator instanceof IValidator)
            throw new Exception("Invalid validator providen to Foo!");
        if (!$validator->IsValidURL($url))
            throw new Exception("The URL $url is not valid!");
        // Do logic
    }
}

// ...and this to your Application Layer
class SymphonyValidator implements IValidator
{
    public function IsValidURL($url)
    {
        // use Symphony validator or any other framework/plugin
    }
}


var foo = new Foo();
var validator = new SymphonyValidator();
foo->SaveURL("invalidUrl", validator);

, , . , - : " , URL- ? ", ( , URL- , infra/app)

, , URL- , - (, Validator true?), . , , , , , ( , -, , , API ..).

+1

All Articles