Is it possible to ensure that every developer in a VS project that uses a certain method (which returns Nhibernate session) will be enforced the using pattern. For example:
public static ISession GetSession()
{
ISessionFactory holder = ActiveRecordMediator.GetSessionFactoryHolder();
ISessionScope activeScope = holder.ThreadScopeInfo.GetRegisteredScope();
ISession session = null;
var key = holder.GetSessionFactory(typeof(ActiveRecordBase));
if (activeScope == null)
{
session = holder.CreateSession(typeof(ActiveRecordBase));
}
return session;
}
I want to ensure that every developer that calls this method surrounds it with the using statement, otherwise it will be treated as an error by Visual Studio and will not compile. (Or maybe by some tool that analyze managed code).
Must be used only as follows:
using (var session = Nh.GetSession())
{
//use the session
}
Is it possible to ensure that every developer in a VS project that uses a certain method (which returns Nhibernate session) will be enforced the using pattern?
Simple answer is no.
More broader question would be, is it possible to warn user if they don't Dispose any Disposable object before they go out of scope. CodeRush and FxCop provide some kind of warnings in that regard.
I haven't see such a warning out of box even with the latest resharper version. Here and here are some discussions on resharper.