Dependency Inversion Principle
public
class
Customer
{
public
string
Name {
get
;
set
;}
public
void
GetCustmerName()
{
try
{
return
this
.Name;
}
catch
(Exception e)
{
FileLogger f =
new
FileLogger();
f.LogError(e);
}
}
}
public
class
FileLogger
{
public
void
LogError(Exception e)
{
//Log Error in a physical file
}
}
The above code has a problem. The Customer class is directly dependent on FileLogger type. The FileLogger and the Customer is tightly coupled and think if they belong to separate libraries, that means they are inseparable from one another.
Following Dependency Inversion principle, one should use an Interface to provide dependency, such that the concrete implementation could be separated form one another.
public
class
Customer
{
public
string
Name {
get
;
set
;}
public
ILogger Logger {
get
;
set
;}
public
void
GetCustmerName()
{
try
{
return
this
.Name;
void
GetCustmerName()
{
}
catch
(Exception e)
{
this
.Logger.LogError(e);
}
}
}
public
interface
ILogger
{
void
LogError(Exception e);
}
public
class
FileLogger:ILogger
{
public
void
LogError(Exception e)
{
//Log Error in a physical file
}
}
public
class
EventViewerLogger : ILogger
{
public
void
LogError(Exception e)
{
//Log Error in a physical file
}
}
Here the Customer class is not totally dependent with the concrete class FileLogger, rather it is dependent using an interface ILogger.
See Also