I have a service layer with a logger class
Service layer references DAL for Logger class to get its email credentials to use while sending email for critical log items.
I want开发者_开发知识库 to use this logger inside the DAL since there is stuff that needs to be logged there also - but with my current architecture I cannot.
I see this could somewhat be handled via IOC but my concern with that is if that way I will be addressing the root of the problem, or hiding it using another layer of abstraction.
What would be good good ways to handle this problem?
In general, circular dependencies are undesirable - they complicate debugging, maintenance, and impose limits on how your code can be structured and extended. Eliminate circular dependencies when possible.
If the logger needs very little information to function - then I would suggest reliminating the dependency of the DAL from the logger. Putting emails into a configuration file, or using a separate mechanism embedded within the logger to access them. Logging is a fairly low-level utility function in most systems - you should avoid making the logger dependent on your data access model. Particularly since you want to be able to log information even if the database is unavailable. It's somewhat useless to have a logger that can't function when a data tier is unavailable.
NOTE: Is there a reason you can't use an existing logging library like log4net or NLog?
If, for some reason, that's not possible - then an Inversion of Control (Dependency Injection) solution may be appropriate. Just realize you would need to factor out into a some third assembly the interface that the logger exposes - so that you can safely invoke it from the DAL.
精彩评论