Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/ASP.NET/CS/ASPxGridViewDemos/App_Code/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/ASP.NET/CS/ASPxGridViewDemos/App_Code/XpoHelper.cs

using System;
using System.Configuration;
using System.Threading;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Metadata;


public static class XpoHelper {
    public static Session GetNewSession() {
        return new Session(DataLayer);
    }

    public static UnitOfWork GetNewUnitOfWork() {
        return new UnitOfWork(DataLayer);
    }

    private readonly static object _lockObject = new object();

    static object _dataLayer;
    static IDataLayer DataLayer {
        get {
            if(_dataLayer == null) {
                lock(_lockObject) {
                    if(Thread.VolatileRead(ref _dataLayer) == null) {
                        Thread.VolatileWrite(ref _dataLayer, CreateDataLayer());                        
                    }
                }
            }
            return (IDataLayer)_dataLayer;
        }
    }

    static IDataLayer CreateDataLayer() {
        XpoDefault.Session = null;        
        XPDictionary dict = new ReflectionDictionary();
        IDataStore store = XpoDefault.GetConnectionProvider(GetConnectionString(), AutoCreateOption.SchemaAlreadyExists);
        dict.GetDataStoreSchema(typeof(XpoEmailEntity), typeof(XpoPersonEntity));
        IDataLayer dl = new ThreadSafeDataLayer(dict, store);
        return dl;
    }

    static string GetConnectionString() {
        string result = ConfigurationManager.ConnectionStrings["LargeDatabaseConnectionString"].ConnectionString;        
        result += ";XpoProvider=MSSqlServer";
        return result;
    }
}