views:

105

answers:

5

Guys,

I have a conceptual question.- I have a Class named ClientDAL that inserts an object on the DataBase (Im using LINQ2SQL).- But before insertinng that class checks that it is not already on the database (check if it exists already on the DB) so I have a try and a if incrementing the ciclomatic complexity.-

My question is, ClientDAL should only handle insertions and the logic to check if already exists somewhere else? This is the code:

  public OperationStatus addClient(Client c) 
       {
           try
           {
               if (!sqlAccess.sqlContext.Clients.Any(x => x.clientName.Equals(c.clienteName)))
               {
                   sqlAccess.sqlContext.Clients.InsertOnSubmit(new Client { clientName = c.clientName });
                   sqlAccess.sqlContext.SubmitChanges();
                   return OperationStatus.SUCCESS;
               }
               else
                   return OperationStatus.FAILURE;

           }
           catch { throw; }

       }

That is the class that handles the insertion, now I call that method from a Helper Class:

     public class ClientHelper
        {
            ClientDAL cDal;

            public ClientHelper()
            {
                cDal = new ClientDAL();
            }


            public OperationStatus insertClient(Client client)
            {
                try 
                {
                    return cDal.addClient(client);
                }
                catch (Exception ex) 
                {
                    return OperationStatus.ERROR; 
                }
            }
}

Now the question is, should I handle the validation on the ClientHelper, or perhaps create a private method on the DAL and handle it on the DAL? Create a private method on the Helper calling the DAL? What is the Best OOP solution for this "Where should I put things" problem...

Thanks in advanced :D

A: 

The DAL is intended to just the low-level data-access code, correct? In which case, I'd suggest to let the DAL be the DAL, and pull other concerns such as validation up into a higher layer - your ClientHelper. Have your ClientHelper do the validation; if it passes, delegate to the DAL. If not, throw an exception.

Grant Palin
Thank you Grant, but since the DAL will only talk to the DB I guess I would create a method on the DAL checking if certain object already exists on the DB. I will simply call it from the Helper. Nothing changes much... I don't know.. I guess I will do it
MRFerocius
A: 

you definitely can add method in you DAL but I think if you are writing DAL the same would be used to add duplicate client name for some other table, so better would be to define a condition class or attribute and pass on the parameter in constructor that should not be duplicate.

Vinay Pandey
+1  A: 

'Helper' classes almost instantly makes your code kind a procedural.

In your case - i wouldn't bother much because project likely is in progress (on the other hand - take a look about Domain Driven Design for example. Might shift your mind a bit).

Therefore - i would call DA method and name it SaveIfNotExists(obj) or create 2 methods Save & Exists in case i need to check if object exists from elsewhere w/o saving it.

Arnis L.
A: 

Personally, and it's a matter of taste, but if it's a data constraint I'd add that to the database schema and let the DAL handle the exception when it tries to add a duplicate client. That way, if you ever have to share the database with another application, you can be sure that they won't be adding duplicates because they forgot the validation.

Trevor Tippins
+1  A: 

DAL must encapsulate the data access problem, if that validations must be done in the data access domain It must be in the dal. If it is about business you must put it in the business layer (your helper?).

Look into DDD´s repository pattern. http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/09/02/ddd-repository-implementation-patterns.aspx

http://martinfowler.com/eaaCatalog/repository.html

Pablo Castilla