Interface, Abstract Class, or let it be as it is for now

  • Thread starter Thread starter Belarmino Vicenzo
  • Start date Start date
B

Belarmino Vicenzo

Guest
After reading this post (I saw it on a member of this forum, nice post btw), I realized that maybe be I'm using an interface while I shouldn't.

He is my code:

public interface IDAOClients
{
List<Clients> GetAll();
void InsertClients(Clients clientes);
void UpdateClientes(Clients clientes);
void DeleteClientes(Clients clientes);
}

public interface IDAOWorkers
{
List<Workers> GetAll();
void InsertWorker(Workers funcionarios);
void UpdateWorker(Workers funcionarios);
void DeleteWorker(Workers funcionarios);
}


public interface IDAOClothes
{
List<Clothes> GetAll();
void InsertClothes(Clothes roupas);
void UpdateClothes(Clothes roupas);
void DeleteClothes(Clothes roupas);
}

public interface IDAOColors
{
List<Colors> GetAll();
void InsertColors(Colors cores);
void UpdateColors(Colors cores);
void DeleteColors(Colors cores);
}
He is my code part 2:

public class DAOClients: IDAOClients
{
public List<Clients> GetAll() { //... }
public void InsertClients(Clients clientes) { //...}
public void UpdateClientes(Clients clientes) { //...}
public void DeleteClientes(Clients clientes) { //...}
}

public class DAOWorkers: IDAOWorkers
{
public List<Workers> GetAll() {}
public void InsertWorker(Workers funcionarios) {}
public void UpdateWorker(Workers funcionarios){}
public void DeleteWorker(Workers funcionarios){}
}


public class DAOClothes: IDAOClothes
{
public List<Clothes> GetAll() {}
public void InsertClothes(Clothes roupas){}
public void UpdateClothes(Clothes roupas){}
public void DeleteClothes(Clothes roupas){}
}

public class DAOColors: IDAOColors
{
public List<Colors> GetAll() {}
public void InsertColors(Colors cores){}
public void UpdateColors(Colors cores){}
public void DeleteColors(Colors cores){}
}


That's only the basic operation they got.
As you may see, I have a lot of repeated method names, the only difference is that they make reference to a different class.

I did this all interface thing because I wanted to separated the and only have one reason to change the code for each class/interface..., I also wanted to be prepared in case I changed database type even knowing that I probably won't. Something that I saw here, made me realized that maybe I'm wrong.

I wanted to make an interface with all those basic CRUD and make all then inherit from it, and for those methods particular for each class, I wanted to write singularly for each class that may need. Something like this:

//temp name
Interface CRUD{
Insert();
Update();
Delete();

}

//I know it doesn't work this way, but...
Clients,Clothes,Workers,Colors:CRUD{

}

//and if one need another singular method
//I would just add on clients
Clients(){

string GetName();
}


The problem is... That I cannot know which class they will instantiate, I cannot make sure that they instantiate just a class
It must be the right one (or I think it must), because each class has it's own fields/properties.

Abstract Class, I also thought on make this supposed single Interface an abstract class, then I could change whenever I want. But if a method was declared as abstract the class that inherit the main class, will also need to inherit, but I could declare abstract methods carefully.

But now I don't know what to do, I don't have that much experience using abstract classes, I normally use interfaces, they are contract, well... Then I just need to implement it all.
I have the POCOs, the Interfaces and the Data Access Object, when I need to change something, most of the time I only change for one reason. If I change the IDAOClients, is just to add more code related to the DB on the client and no more.

Since I was coding the interfaces thinking on SRPWikiPedia, SRPHackernoon, "The SRP is about limiting the impact of change", I have a lot of code, maybe I'm over reacting of this SRP thing, but I feel like it's working.
I someone need to work on Worker's DAO class, he wouldn't need to be worried about nothing but that.

But the "What Is Wrong With My Interface" made me realized that maybe there's a better way to do all that.
At least if I could put all those CRUD on a single class/interface and make the user instantiated the right class, I think it would remove a lot of possible duplicates piece of code.


-


BP-LP 2005/2016 @ll rights reserved

Continue reading...
 
Back
Top