For my reference
http://blogs.msdn.com/b/yash/archive/2007/10/12/pdb-files-what-are-they-and-how-to-generate-them.aspx
Project > Properties > Build > Advanced > Output > Debug Info
Always create PDB
Wednesday, 28 September 2011
Tuesday, 13 September 2011
Events vs Delegates
This rather confused me for a long time, but found a great article
http://blog.monstuff.com/archives/000040.html
Briefly
Events & Interfaces, Events Invocation, Event Signature and Event accessors
1. Events are ALWAYS multi-cast.
2. Events can be done away with delegates, but it is similar to exposing a Public member field than a Property.
3. Helps encapsulation - So interfaces don't support fields, but events - We can't declare delegate fields in Interface.
4. Only defined class can raise the event, but fields(delegates) can be raised outside of the class.
5. Standard signature of (sender, eventargs) - we can use EventHandler.
6. Event accessors - have add and remove - may be used for logging?
So my two cents with an example
public class MoneyWithdrawnEvent : EventArgs
{
public decimal Amount { get; set; }
public MoneyWithdrawnEvent(decimal amount)
{
this.Amount = amount;
}
}
public class BankService
{
public delegate void Notify(EventArgs e);
public event Notify amountWithdrawn;
public void Withdraw(decimal amount)
{
//Business
amountWithdrawn(new MoneyWithdrawnEvent(amount));
}
}
public class ATMService
{
IServiceFactory factory;
public ATMService(IServiceFactory factory)
{
}
public void Withdraw(decimal amount)
{
BankService service = factory.Create();
BankService.Notify notifyUser = new BankService.Notify(this.AlertUser);
notifyUser(new MoneyWithdrawnEvent(amount)); //This is wrong, as we don't the state(outcome) of the transaction, and
// allows this kind of business errors where alert before
// withdrawing money.
//service.amountWithdrawn(); 1. You can't invoke event, so it will be the responsiblity
// of bank to raise the money withdrawn.
service.Withdraw(amount);
}
private void AlertUser(EventArgs alert)
{
}
}
public interface IServiceFactory
{
BankService Create();
}
http://blog.monstuff.com/archives/000040.html
Briefly
Events & Interfaces, Events Invocation, Event Signature and Event accessors
1. Events are ALWAYS multi-cast.
2. Events can be done away with delegates, but it is similar to exposing a Public member field than a Property.
3. Helps encapsulation - So interfaces don't support fields, but events - We can't declare delegate fields in Interface.
interface Temp
{
event EventHandler notify;
EventHandler eventHandler;
}
{
event EventHandler notify;
EventHandler eventHandler;
}
4. Only defined class can raise the event, but fields(delegates) can be raised outside of the class.
5. Standard signature of (sender, eventargs) - we can use EventHandler
6. Event accessors - have add and remove - may be used for logging?
So my two cents with an example
public class MoneyWithdrawnEvent : EventArgs
{
public decimal Amount { get; set; }
public MoneyWithdrawnEvent(decimal amount)
{
this.Amount = amount;
}
}
public class BankService
{
public delegate void Notify(EventArgs e);
public event Notify amountWithdrawn;
public void Withdraw(decimal amount)
{
//Business
amountWithdrawn(new MoneyWithdrawnEvent(amount));
}
}
public class ATMService
{
IServiceFactory factory;
public ATMService(IServiceFactory factory)
{
}
public void Withdraw(decimal amount)
{
BankService service = factory.Create();
BankService.Notify notifyUser = new BankService.Notify(this.AlertUser);
notifyUser(new MoneyWithdrawnEvent(amount)); //This is wrong, as we don't the state(outcome) of the transaction, and
// allows this kind of business errors where alert before
// withdrawing money.
//service.amountWithdrawn(); 1. You can't invoke event, so it will be the responsiblity
// of bank to raise the money withdrawn.
service.Withdraw(amount);
}
private void AlertUser(EventArgs alert)
{
}
}
public interface IServiceFactory
{
BankService Create();
}
Subscribe to:
Comments (Atom)