Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to put the validate logic? In Service or Repository?

I have some logic like this, before save the stock into the db, i will check whether there is stock has the same stock code in the database. My question is where should I put the logic, in the service layer or repository layer. here is the sample code:
option 1: put in the service layer, i put the IsAccountAlreadyExists Method in the service layer

public override void Save(AccountInfo accountInfo)
{
    using (var scope = new TransactionScope())
    {
        if(this.IsAccountAlreadyExists(accountInfo))
        {
            throw new AccountAlreadyExistedException(
                "Account Code : " + accountInfo.AccountCode +
                " already existed.");
        }

        accountRepository.Save(accountInfo);
            scope.Complete();
    }
}

option 2: I will move the IsAccountAlreadyExists logic to the repository layer.

public override void Save(AccountInfo accountInfo)
{
    try
    {
        using (var scope = new TransactionScope())
        {
            accountRepository.Save(accountInfo);
            scope.Complete();
        }
    }
    catch(AccountAlreadyExistedException e)
    {
        ...
    }
}

what's your opinion?

like image 240
user192415 Avatar asked Nov 08 '09 08:11

user192415


People also ask

Where should validation take place?

Data validation should occur in two locations: The point where data is acted upon, for example validating input parameters to an SQL query. General validation at the point where data is submitted, for example in a web application some validation should occur on the client.

Should I validate controller or service?

As a general rule of thumb, I would say that business logic of this sort should be in the service. Controllers should be light-weight and pass on requests. Further, there may be other clients of your service, not just controllers, so this allows you to keep validation in one place.

Which layer contains code to perform validation operation?

The service layer contains business logic. In particular, it contains validation logic.

Which layer is responsible for validation before writing on the database?

A service layer is a layer in an application that facilitates communication between the controller and the persistence layer. Additionally, business logic is stored in the service layer. It includes validation logic in particular.


1 Answers

I consider this to be three tiers (with an interface defined to connect each piece):

  • Data Repository - only for storing and retrieving raw data. As little logic goes here as possible.
  • Business Model - all the smarts goes here, including validation.
  • Service (i.e. client access) - a very thin piece, just enough to provide a connection to the client.

This way, if you choose to store your data in some other fashion, the validation logic doesn't get lost with it.

Similarly, if you decide to provide a different form of client access, you do it without needing to replicate a bunch of logic.

like image 143
Mike Hanson Avatar answered Sep 26 '22 02:09

Mike Hanson