How to make a validation of a singleton class in a session per request

210 Views Asked by At

I have a configuration class for the first User access in the base will be necessary to place settings and served almost basis using this if firebird or oracle, the settings are saved in an encrypted file and every time I start my application check these settings are, so far so good.

private static Settings _data;
public static Settings Data
        {
            get
            {
                if (_data == null)
                {
                    _data = new Settings();
                    if (_data.FilePath != null && !Directory.Exists(Path.GetDirectoryName(_data.FilePath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(_data.FilePath));
                    }
                    _data.Load(_data.FilePath);
                }
                return _data;
            }
        }

However I was using a session for all requests, I decided to change that was having a synchronization problem, then decided to implement session-per-request, but with every time is beginning my application a session is created and the need to create the session served data that are created in the singleton class. One way to implement would be to make the validation before creating my session in the Global.asax.

protected void Application_BeginRequest()
        {
            if (!Settings.Data.Valid())
                throw new SingletonException();

            var session = SessionManager.SessionFactory.OpenSession();
            if (!session.Transaction.IsActive)
                session.BeginTransaction(IsolationLevel.ReadCommitted);
            CurrentSessionContext.Bind(session);
        }

This is my SessionManager.

private SessionManager()
{
    if (Settings.Data.SGDB == 1)
    {
        sessionFactory = Fluently.Configure()
            .Database(new FirebirdConfiguration().ConnectionString(GetConnectionString())
                .ShowSql())
            .Mappings(GetMappings())
            .ExposeConfiguration(c => c.SetProperty(Environment.CurrentSessionContextClass, "web"))
            .BuildSessionFactory();
    }

    if (Settings.Data.SGDB == 0)
    {
        sessionFactory = Fluently.Configure()
             .Database(OracleConfiguration
             .Oracle10
             .ConnectionString(GetConnectionString())
             .ShowSql()
             .Driver<OracleManagedDataClientDriver>()
             .Dialect<Oracle10gDialect>()
             )
             .Mappings(GetMappings())
             .ExposeConfiguration(c => c.SetProperty(Environment.CurrentSessionContextClass, "web"))
             .BuildSessionFactory();
    }
}

However for every request it anger do the validation, which would be a better way for me to do this validation of singleton class?

1

There are 1 best solutions below

0
On BEST ANSWER

Validate the settings during the application startup process and if it not valid do not start application, because it can't work propertly without that.