{"id":8216,"date":"2024-01-29T13:33:46","date_gmt":"2024-01-29T12:33:46","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=8216"},"modified":"2024-01-29T13:33:48","modified_gmt":"2024-01-29T12:33:48","slug":"devexpress-xpohelper-connection-register-types","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/devexpress-xpohelper-connection-register-types\/","title":{"rendered":"DevExpress XpoHelper Connection Register Types"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using DevExpress.ExpressApp;\r\nusing DevExpress.ExpressApp.Xpo;\r\nusing DevExpress.Xpo;\r\nusing System;\r\nusing System.Linq;\r\n\r\npublic static class XpoHelper\r\n{\r\n    private static System.Reflection.Assembly[] _assemblies;\r\n    public static void Register(params System.Reflection.Assembly[] assemblies)\r\n    {\r\n        _assemblies = assemblies;\r\n    }\r\n\r\n    public static Session GetNewSession()\r\n    {\r\n        var session = new Session(DataLayer);\r\n        session.TrackPropertiesModifications = false;\r\n        return session;\r\n    }\r\n\r\n    public static UnitOfWork GetNewUnitOfWork()\r\n    {\r\n        return new UnitOfWork(DataLayer);\r\n    }\r\n\r\n    public static XPObjectSpaceProvider ObjectSpaceProvider { get; private set; }\r\n    public static IObjectSpace DefaultObjectSpace { get; private set; }\r\n    public static IDataLayer DataLayer { get; private set; }\r\n\r\n    public static IDataLayer CreateDataLayer(string connectionString)\r\n    {\r\n        if (_assemblies == null) throw new Exception(\"XpoHelper.types is null. Please use XpoHelper.Register before using any other function from the XpoHelper class.\");\r\n\r\n        const int maxConnections = 3;\r\n        var connectionPoolString = XpoDefault.GetConnectionPoolString(connectionString, maxConnections);\r\n        var dataStoreProvider = new ConnectionStringDataStoreProvider(connectionPoolString, true);\r\n\r\n        var allTypes = _assemblies.SelectMany(a => a.GetTypes().Where(x => typeof(XPBaseObject).IsAssignableFrom(x))).ToList();\r\n        var xpoTypeInfoSource = XpoTypesInfoHelper.GetXpoTypeInfoSource();\r\n        allTypes.ForEach(x => xpoTypeInfoSource.RegisterEntity(x));\r\n        var typesInfo = XpoTypesInfoHelper.GetTypesInfo();\r\n\r\n        ObjectSpaceProvider = new XPObjectSpaceProvider(dataStoreProvider, typesInfo, xpoTypeInfoSource, true);\r\n        DefaultObjectSpace = ObjectSpaceProvider.CreateObjectSpace();\r\n        DataLayer = ObjectSpaceProvider.DataLayer;\r\n        return DataLayer;\r\n    }\r\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8216","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=8216"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8216\/revisions"}],"predecessor-version":[{"id":8217,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8216\/revisions\/8217"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=8216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=8216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=8216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}