I guess you are aware that throwing an exception in a type initializer makes the type unusable in the AppDomain, right?
On Feb 6, 12:04 am, James Gregory <[email protected]> wrote: > The idea is a throwback to when you had to manage memory, and throwing > exceptions in constructors was a bad idea. It just kinda stuck around, I > guess. > Personally, I don't do that much passing of values into constructors that > require validation. Constructors get dependencies, and nothing else. Methods > are used for setting parameters or behavior based on alterable parameters. > I've never had to personally throw an exception from a constructor. > > On Thu, Feb 5, 2009 at 10:59 PM, Tuna Toksoz <[email protected]> wrote: > > What if the user provides invalid parameters? > > > Tuna Toksöz > >http://tunatoksoz.com > >http://twitter.com/tehlike > > > Typos included to enhance the readers attention! > > > On Fri, Feb 6, 2009 at 12:50 AM, James Gregory > > <[email protected]>wrote: > > >> There's nothing technically wrong with throwing exceptions from > >> constructors, certainly not in .Net anyway; however, it violates the > >> principal of least surprise, which makes it a bad idea. Constructors are > >> just expected to work, it's the method calls people plan for failure with. > > >> On Thu, Feb 5, 2009 at 1:42 PM, Robin Nadeau <[email protected]> wrote: > > >>> [Citation needed] > > >>> On Thu, Feb 5, 2009 at 6:20 AM, SimoneB <[email protected]> wrote: > > >>>> You shouldn't throw exceptions in type initializers. > > >>>> On Feb 4, 7:22 pm, Robin Nadeau <[email protected]> wrote: > >>>> > Here's what we use: > > >>>> > public class StaticSessionManager > >>>> > { > > >>>> > public static readonly ISessionFactory SessionFactory; > > >>>> > static StaticSessionManager() > >>>> > { > >>>> > try > >>>> > { > >>>> > Configuration cfg = new Configuration(); > > >>>> > if (SessionFactory != null) > >>>> > throw new Exception("trying to init SessionFactory > >>>> > twice!"); > > >>>> > SessionFactory = > >>>> cfg.Configure().BuildSessionFactory(); > >>>> > } > >>>> > catch (Exception ex) > >>>> > { > >>>> > Console.Error.WriteLine(ex); > >>>> > throw new Exception("NHibernate initialization > >>>> failed", ex); > >>>> > } > >>>> > } > >>>> > public static ISession OpenSession() > >>>> > { > >>>> > return SessionFactory.OpenSession(); > >>>> > } > >>>> > public static void CloseSession() > >>>> > { > >>>> > SessionFactory.Close(); > >>>> > } > > >>>> > } > >>>> > On Wed, Feb 4, 2009 at 1:19 PM, Robin Nadeau <[email protected]> > >>>> wrote: > >>>> > > Use the singleton pattern. > > >>>> > > On Wed, Feb 4, 2009 at 10:00 AM, CCL <[email protected]> wrote: > > >>>> > >> Hi, > > >>>> > >> We have introduced NHibernate in a .NET web application, using a > >>>> best > >>>> > >> pactice for handling the lifecycle of sessions etc. > > >>>> > >> After deploying the app, we noticed that the aspnet_wp process > >>>> memory > >>>> > >> kept growing until returning an OutOfMemoryExeption and recycling - > >>>> > >> about every four days. > > >>>> > >> Debugging memory dumps of the process showed that we had multiple > >>>> > >> SessionFactory objects (168 objects in one dump). > > >>>> > >> In the best practice, creation of the SessionFactory class was > >>>> > >> triggered via the "Init()" function in global.asax. But reading > >>>> about > >>>> > >> that event, I realize that it is called once for every instance of > >>>> > >> httpApplication which indeed can exist in multiple instances. So I > >>>> > >> instead moved the creation of the SessionFactory to > >>>> > >> "Application_OnStart()" in global.asax - which is only triggered > >>>> once > >>>> > >> for the entire application. > > >>>> > >> This solved my growing memory problem - and I now only have ONE > >>>> > >> SessionFactory object. > > >>>> > >> But the question remains - what is the recommended approach, is it > >>>> to > >>>> > >> create the SessionFactory instance via Init() or > >>>> Application_OnStart() > >>>> > >> in global.asax? Looking around I cad find examples of both, but no > >>>> > >> article comparing the approaches. Does anyone have a comment on > >>>> this > >>>> > >> matter? I solved my immediate problem, but perhaps I am missing > >>>> > >> something else when I do this change... > > >>>> > >> Best regards, > >>>> > >> Carl > > >>>> > > -- > >>>> > > Robin Nadeau, B.Eng. > >>>> > > Software Developer > > >>>> > -- > >>>> > Robin Nadeau, B.Eng. > >>>> > Software Developer > > >>> -- > >>> Robin Nadeau, B.Eng. > >>> Software Developer --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
