2

I am trying to apply some OOP principles to some C Abstract Data Type model. Given a ADT that abstract a Employee, where a Employee instance is inmutable (when instancing a Employee it gives a unique primary key Name and Last Name and this primary key keeps inmutable during the Employee object life) i wonder if this ADT should care about saving his data in the Database.

So given this piece of code of the ADT:

static char* address; //Address of the Employee   

TakeNewAddress(char* newaddress)
{
  .....  
} 

GiveAddress(char* address)
{
  ....
}

Should SetAddress method copy the content of newaddress into his address attribute and save it into Database or should it only update his address attribute? Should GiveAddress return address variable or should it retrieve it from the database?

I think this ADT representing a Employee should not care about internal database (it is not something you ask to a employee), so saving his data into the Database should be performed outside this ADT by a special handler or user interface for the system.

MABC
  • 576
  • 2
  • 11
  • 29

1 Answers1

0

I'm not sure that having an Employee defined as ADT will bring you great practical benefits. I may be wrong, but I'm used to thinking about ADTs as data structures, not definitions of business-specific entities.

Aside from that comment, in my opinion your logic is absolutely right. An ADT should be about operations, values and constraints and it seems to me that it should be agnostic to persistence. How you store the type as a whole is none of its concern and I'd not couple this to a database.

Plamen G
  • 4,729
  • 4
  • 33
  • 44
  • Why do you think ADT is about data structures? I think about it as the implementation of OOP 'Class' using C, in the sense it exposes public operations to interact with the entity and it encapsulates the implementation. I dont think about ADT as only a 'dataholder' and i use it to implement business-specific entities. About the ADT being agnostic about persistence, assuming admin add a new employee using a Web interface, who should be care about the persistence of employee data the web interface? It seems to me wrong again. – MABC Apr 19 '15 at 16:49
  • I think you got it wrong, mate. See this thread on the topic (there are multiple great explanations of ADT): http://stackoverflow.com/questions/1692933/what-is-an-abstract-data-type-in-object-oriented-programming. For me Employee seems like just a class a business entity, a class in the domain object model. I cannot find a reason to relate the two concepts. About the website - you'll have a data access layer, and a model and a controller that initiates writing/reading - if you're writing an employee row into a table, you will do it from other objects, not the Employee object itself. – Plamen G Apr 19 '15 at 18:16
  • Im using C as procedural language, so i can not define a Employee class in terms of OOP. From the Wikipedia:Formally, an ADT may be defined as a "class of objects whose logical behavior is defined by a set of values and a set of operations". – MABC Apr 19 '15 at 18:42
  • Well, your case is pretty specific, so maybe I'm in the wrong, but I just wanted to point this out in case you misinterpreted the idea altogether. – Plamen G Apr 19 '15 at 18:46
  • I posted last comment without having complete it, sorry. As i said Im using C as procedural language, so i can not define a Employee class in terms of OOP. From the Wikipedia So for declaring a Employee in my system, i use a header file declaring a struct Employee* Employee_t that encapsulates Employee internal data and at the same time the header declare the methods to instanciate and interact with a Employee entity. – MABC Apr 19 '15 at 18:52
  • I am interested in: "you'll have a data access layer, and a model and a controller that initiates writing/reading." Does it mean i should create some global class that invokes the GiveAddress method of the employee class and at the same time save the new employee in the Database?. Does this class contain all the objects domain and his interface would contain CreateNewEmployee() and CreateNewDepartment() methods and this methods call specific methods of Employee and Department class and save them into database? – MABC Apr 19 '15 at 18:55
  • I thought you're using C as procedural language only? If so I'm not sure what would be the best approach to design DAL, but by all means it is good to abstract the data access somehow. Otherwise you will have tight coupling and it will be a pain to change from one type of data store to another. – Plamen G Apr 19 '15 at 19:17