Some ‘high risk’ data operations need to be logged. In this case, the ‘high risk’ operations are defined as writes to our ERP system. It happens that we are logging those events to our SQL Server database.
Pseudo-code:
Public Class MyCompany.DAL.ERP { Public void WriteToERP(string msg) { // ... do the write MyCompany.Logging.Write('Wrote msg: ' + msg); } } Public Class MyCompany.Logging { Public void Write(string msg) { MyCompany.DAL.ExecuteSQL('Insert INTO EventLog VALUES ' + msg); } }
What is the best practice to eliminate this tight coupling?
Hmm, IMHO logging is an infrastructure concern. You can use it in your DAL, but your logger should not use your DAL.
If you remove the dependency your logger has on your DAL, then you should be able to use your logger in other projects as well.