So you need to raise an event, and wanna do it in a threadsafe way, or safer way :)
Not so long ago i wrote this code :
...
public event EventHandler Save;
...
private void OnSave(EventArgs e)
{
if(Save != null) Save(this,EventArgs.Empty);
}
What is wrong here, the fact that the point between the not null check and the eventual event raising can has changes in between.
So from now just write this :
private void OnSave(EventArgs e)
{
EventHandler handlers = Save;
if (handlers != null) handlers(this, e);
}
This makes a copy into the handlers variable and cannot change between check and raise.
Definition: Method hiding or simply put if a method is not overriding the derived method, it is hiding it.
Snippet 1:
using System;
namespace Polymorphism
{
class Super { public void SomeMethod()
{
Console.WriteLine("Super Write");
}
}
class Sub : Super
{
public new void SomeMethod()
{
Console.WriteLine("Sub Write");
}
}
internal class Program
{
private static void Main(string[] args)
{
Super lSuper = new Super();
Sub lSub = new Sub();
lSuper.SomeMethod();
lSub.SomeMethod();
}
}
}
The using of the new keyword is optional as it will only generate a compiler warning. Over to example 2
Snippet 2:
namespace Polymorphism
{
public abstract class Super
{
public void SomeMethod()
{
Console.WriteLine("Super Write");
}
}
public class Sub : Super
{
public new void SomeMethod()
{
Console.WriteLine("Sub Write");
}
}
public class Program
{
private static void Main(string[] args)
{
Sub lSub = new Sub();
lSub.SomeMethod(); //Which will output "Sub Write"
}
}
}
And again the use of the new keyword is optional and will only generate a compiler warning. Now why does this concerns me as a developer.
Suppose you have developed a framework with an abstract super class like in snippet 3
Snippet 3:
using System;
using Polymorphism;
namespace Polymorphism
{
public abstract class Super
{
protected void SomeRatherImportantSuperStuff()
{
Console.WriteLine("Super Write");
//Very important code !!! -- here
DoSubStuff();
}
public abstract void DoSubStuff();
}
public class Sub : Super
{
public new void SomeRatherImportantSuperStuff()
{
Console.WriteLine("Sub Write");
}
public override void DoSubStuff()
{
Console.WriteLine("Sub Write");
}
}
public class Program
{
private static void Main(string[] args)
{
Sub lSub = new Sub();
lSub.SomeRatherImportantSuperStuff(); //Which will output "Sub Write" and NOT first Super Write like we wanted
}
}
}
So a quick word of advice here make visual studio threat the warning of the not using a new keyword as a build error, like you are used to for the public method missing comment warning.
Just yet another new feature that can cause serious issues and break more or less your inheritance. Apparantly you can even hide a sealed method !!!
Well a couple of days ago, i saw on Tweakers.Net that Mickeysoft decided to release a new .Net Language called F#. On the microsoft's research site (http://research.microsoft.com/fsharp/fsharp.aspx) following definition is used :
"Combining the efficiency, scripting, strong typing and productivity of ML with the stability, libraries, cross-language working and tools of .NET. "
Apparantly M Research decided to take it into productivity. After reading up the getting started site and the manual, etc ... i still can't see what is the point of this. But hey my Visual Studio has also something called J# that i never even opened.
Syntax :
let x = 3 + (4 * 5)
let res = (if x = 23 then "correct" else "incorrect")
And also use of "->" "-<" let f = (fun x -> x + 1)
Have Fun.
NHibernateIt is an NHibernate helper project build on the latest public release of NHibernate 1.20.GA. It provides session management for Web as well as Thread-based Session management, Transaction Management and a repository class with a lot of functionality to use it as a component in your data access layer.
When I was looking on the web for how to integrate NHibernate in an Asp.Net project, I found a lot of stuff on the httpmodules for creating a session helper for the Session-Per-View-Pattern. I stumbled upon a project called NHibernator(http://oracleatdotnet.blogspot.com/2007/04/nhibernator-simple-but-yet-powerful.html), it was not maintained by the original developer and contained too much stuff I really didn’t needed, like for example the NHibernatorTransaction.
Also I didn’t found any solutions for how to use in a commonly used architecture where we have a data access layer, a business layer and perhaps a service layer.
I especially had problems with the session management because I didn’t wanted to use the nhibernate session on the UI layer.
I started out creating a BaseManager for objects that Implemented IDisposable, for ease of use. Now it has become a SessionHelper class with a lot of functionality. A small downside of the Nhibernator Project was the default configuration file naming ‘hibernate.cfg.xml’ which was a security threat when used in a web environment, because you can open xml trough your browser. Yes, you could change it in the web.config file, but as I am a developer who likes to have the entire configuration stored in a database and populated trough a unit test, except for connectionstrings, I love to have web projects without a config file except the default one with for example tracing enabled and such.
Still a lot of credit has to go to Tomer Avissar (author of NHibernator) for starting such a great project.
Well now it's added to SourceForge.Net on http://sourceforge.net/projects/nhibernateit, with full source on svn and simple documentation.