Exemple #1
0
 public XpoDatabase(XpoDatabaseOptions[] options)
 {
     for (int i = 0; i < options.Length; i++)
     {
         AddDataLayer(options[i]);
         if (i == 0)
         {
             this.options = options[i];
         }
     }
 }
Exemple #2
0
 protected void AddDataLayer(XpoDatabaseOptions option)
 {
     if (string.IsNullOrEmpty(option.ConnectionString))
     {
         throw new ArgumentNullException("ConnectionString");
     }
     if (string.IsNullOrEmpty(option.Name))
     {
         throw new ArgumentNullException("Name");
     }
     GetDataLayer(option);
 }
Exemple #3
0
 public XpoDatabase(Action <XpoDatabaseOptions>[] setupActions)
 {
     for (int i = 0; i < setupActions.Length; i++)
     {
         var o = new XpoDatabaseOptions();
         setupActions[i](o);
         AddDataLayer(o);
         if (i == 0)
         {
             this.options = o;
         }
     }
 }
Exemple #4
0
 public static IDataLayer GetDataLayer(XpoDatabaseOptions options)
 {
     if (!dataLayers.TryGetValue(options.Name, out IDataLayer result))
     {
         if (!string.IsNullOrWhiteSpace(options.ConnectionString))
         {
             result = CreateDataLayer(options);
             dataLayers.AddOrUpdate(options.Name, result, (s, l) => l);
         }
     }
     if (result == null)
     {
         throw new Exception("DataLayer was not found!");
     }
     return(result);
 }
Exemple #5
0
        //public static T Execute<T>(string dataLayerName, Func<Session, T> work, bool transactional = true, bool commit = true)
        //{

        //}

        #endregion

        private static IDataLayer CreateDataLayer(XpoDatabaseOptions options)
        //string connectionString, string datalayerName, bool? updateSchema, bool enableCache)
        {
            if (String.IsNullOrEmpty(options.ConnectionString))
            {
                throw new ArgumentNullException("connectionString");
            }
            // set XpoDefault.Session to null to prevent accidental use of XPO default session
            XpoDefault.Session = null;
            //ReflectionClassInfo.SuppressSuspiciousMemberInheritanceCheck = true;
            // Needed to run in Medium Trust Security Context
            XpoDefault.UseFastAccessors    = false;
            XpoDefault.IdentityMapBehavior = IdentityMapBehavior.Strong;

            // autocreate option in connectionstring
            AutoCreateOption createOption;
            bool             enableCachingNode;

            try
            {
                createOption      = Conversion.GetConfigOption <AutoCreateOption>(options.ConnectionString, "AutoCreateOption", options.UpdateSchema);
                enableCachingNode = Conversion.GetConfigOption(options.ConnectionString, "EnableCachingNode", options.EnableCaching);
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("XpoDatabase was unable to parse connectionString!\n{0}",
                                                  ex.InnerException == null ? ex.Message : ex.InnerException.Message));
            }

            XPDictionary dataDictionary = new ReflectionDictionary();
            IDataStore   dataStore      = XpoDefault.GetConnectionProvider(XpoDefault.GetConnectionPoolString(options.ConnectionString), createOption);

            // Initialize the XPO dictionary
            dataDictionary.GetDataStoreSchema(GetDataTypes(options.Name));

            // make sure everything exists in the db
            if (createOption == AutoCreateOption.DatabaseAndSchema)
            {
                using (SimpleDataLayer dataLayer = new SimpleDataLayer(dataStore))
                {
                    using (Session session = new Session(dataLayer))
                    {
                        // place code here to patch metadata
                        session.UpdateSchema();
                        session.CreateObjectTypeRecords();
                        XpoDefault.DataLayer = new ThreadSafeDataLayer(session.Dictionary, dataStore);
                    }
                }
            }
            IDataLayer result;

            if (enableCachingNode)
            {
                result = new ThreadSafeDataLayer(dataDictionary, new DataCacheNode(new DataCacheRoot(dataStore)));
            }
            else
            {
                result = new ThreadSafeDataLayer(dataDictionary, dataStore);
            }

            return(result);
        }
Exemple #6
0
 public XpoDatabase(XpoDatabaseOptions options) :
     this(new XpoDatabaseOptions[] { options })
 {
 }