像这里和整个世界的大多数开发人员一样,我多年来一直在使用面向对象编程(OOP)技术开发软件系统.因此,当我读到面向方面编程(AOP)解决传统OOP无法完全或直接解决的许多问题时,我会停下来思考,这是真的吗?
我已经阅读了很多信息,试图学习这个AOP范例的关键,并且我在同一个地方,所以,我想更好地理解它在现实世界应用程序开发中的好处.
有人有答案吗?
我的很多C#代码遵循这种模式:
void foo(string param1, string param2, string param3)
{
try
{
// do something...
}
catch(Exception ex)
{
LogError(String.Format("Error in foo(param1={0}, param2={1}, param3={2}), exception={3}", param1, param2, param3, ex.Message));
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在.NET中获取函数的键/值列表,以便我可以调用另一个函数来构造我的错误记录字符串?或者你有更通用/更好的方法吗?
每次我在这里播客或阅读关于它的博客文章,即使在这里,他们也会听起来像字符串理论或其他东西.用类固醇依赖注射来描述OOP的最佳方法是什么?
每当有人试图解释它时,就像Aspects,[花生卡通声音的成人],正交,[更多噪音],交叉问题等等.说真的,任何人都可以用外行的术语来形容它.
我正试图绕过AOP,一些Qt代码真的会有所帮助.
从维基百科这里有一些示例代码(很容易让Qt/C++程序员阅读):
void transfer(Account fromAcc, Account toAcc, int amount, User user, Logger logger)
throws Exception {
logger.info("transferring money...");
if (! checkUserPermission(user)){
logger.info("User has no permission.");
throw new UnauthorizedUserException();
}
if (fromAcc.getBalance() < amount) {
logger.info("Insufficient Funds, sorry :( ");
throw new InsufficientFundsException();
}
fromAcc.withdraw(amount);
toAcc.deposit(amount);
//get database connection
//save transactions
logger.info("Successful transaction. :) ");
}
Run Code Online (Sandbox Code Playgroud)
然后"aspectized":
void transfer(Account fromAcc, Account toAcc, int amount) throws Exception {
if (fromAcc.getBalance() < amount) {
throw new InsufficientFundsException();
}
fromAcc.withdraw(amount);
toAcc.deposit(amount);
} …Run Code Online (Sandbox Code Playgroud) 是否可以将Loggin行为注入标记的类或/和方法,如下所示:
Log("Method {0} started",GetMethodNameTroughReflection)
Call method body
Log("Method {0} Finished",GetMethodNameTroughReflection)
Run Code Online (Sandbox Code Playgroud)
我想创建自己的Attribute类,它将实现方法调用的记录行为.
我想描述app.config文件中的登录行为,可以通过config中的设置禁用它.
怎么做对了?也许为这样的任务创建了解决方案?