Information hiding against hidden dependencies

What are some general recommendations in a procedure (or function, module, etc.) for balancing the desire to hide information and the corresponding level of abstraction in the procedure interface with problems inherent in dependency injection? >

To be more specific, suppose I code a procedure called getEmployeePhoneNbr (employeeId). Internally, the procedure is performed by querying the database table with the key from employeeId. I want to hide these implementation details, but now the procedure depends on the external file, which prevents its use if the environment changes.

The same situation will occur at any time when the procedure uses an external resource - a file, a database, whatever. It doesn’t feel like it is somehow hard to code the use of this resource as part of the procedure, but I'm not sure what the alternative is.

Please note that I do not work in an object-oriented language as much as possible, I am most interested in answers that are widely applicable to any type of language. p>

Thanks Matt

+5
source share
5 answers

, , (aka DIP). .

OO, ( OO , ).

, , (, ), -.

, / .

OO , /, .

+3

, - ( , , )

, , - . , . , RDBMS (connect/query/fetch) .

, , , ( ) Employee, , .

, Employee, , , Employee . Employee , . ( , , , ).

, "" Employee unit test, ( , , ), . , 1000 "", (, db, ..) 1000 ( , ORM N + 1).

, , - , , , . IMHO - -, "" .

+1

- context/environment. :

type Environment = record
      DatabaseHandle: ...;
      ...
   end;

   Employee = record
      ID: integer;
      Name: string;
      ...
   end;


function OpenEnvironment (var Env: Environment): boolean;
begin
   ...
end;

procedure CloseEnvironment (var Env: Environment);
begin
   ...
end;

function GetEmployeeById (var Env: Environment; ID: integer; var Employee: Employee): boolean;
begin
   ... load employee using the data source contained in environment ...
end;

(-). , , , , , PITA, Unixish errno Window GetLastError. , API- , , - .

, API.

0

, - , getEmployeePhoneNbr (employeeId)... - , , .

.

:

  • , , .
  • API .

:

  • , .
  • API , , getEmployeePhoneNbr (employeeId), getEmployeeName (employeeId)....

, , , , .

0

. , , . , . .

:

getEmployeePhoneNbr(employeeId)
    dbName = "employeedb"
    ... SQL, logic, etc.

:

getEmployeePhoneNbr(employeeId, dbName)
    ... SQL, logic, etc.

:

getEmployeePhoneNbr(employeeId)
    dbName = getEmployeeDbName()
    ... SQL, logic, etc.

This way you can change getEmployeeDbName () and each dependent function and module will be useful.

0
source

All Articles