Example #1
0
        public SandboxTests()
        {
            Globals.WrapperSupports         = WrappingSupport.Notifications;
            Globals.WrappingClassNamespace  = null;
            Globals.WrapperClassNamePattern = "{0}Wrapped";
            CEF.AddGlobalService(DBService.Create(new MSSQLProcBasedProvider($@"Data Source={DB_SERVER};Database=CodexMicroORMTest;Integrated Security=SSPI;MultipleActiveResultSets=true", defaultSchema: "CEFTest")));
            CEF.AddGlobalService(new AuditService());

            KeyService.RegisterKey <Person>(nameof(Person.PersonID));
            KeyService.RegisterKey <Phone>("PhoneID");

            KeyService.RegisterRelationship <Person>(TypeChildRelationship.Create <Person>("ParentPersonID").MapsToChildProperty(nameof(Person.Kids)));
            KeyService.RegisterRelationship <Person>(TypeChildRelationship.Create <Phone>().MapsToParentProperty(nameof(Phone.Owner)).MapsToChildProperty(nameof(Person.Phones)));
        }
        /// <summary>
        /// This is intended to be called once on app startup. The goal is to have this be 99% code generated.
        /// </summary>
        private void InitializeFramework()
        {
            // Tells CEF that we do have code gen available that supports notifications
            // The {0}Wrapped pattern tells CEF that "Person" as a POCO class is wrapped by the "PersonWrapped" class - and/or you can use namespace differences
            Globals.WrapperSupports         = WrappingSupport.Notifications;
            Globals.WrappingClassNamespace  = null;
            Globals.WrapperClassNamePattern = "{0}Wrapped";

            // Default audit service would use Environment's username but doing this to illustrate; plus for last updated date, default is to use UTC date/time but we've changed here to use local time
            // DBservice in this case initialized based on a single connection string to use throughout, future examples will demo multiple databases; default schema is "dbo", we'll override for demo purposes
            CEF.AddGlobalService(new AuditService(() => { return("Me: " + Environment.UserName); }));
            CEF.AddGlobalService(new DBService(new MSSQLProcBasedProvider($@"Data Source={DB_SERVER};Database=CodexMicroORMTest;Integrated Security=SSPI;MultipleActiveResultSets=true", defaultSchema: "CEFTest")));

            // Establish primary keys based on types (notice for Phone we don't care about the PhoneID in the object model - do in the database but mCEF handles that!)
            KeyService.RegisterKey <Person>(nameof(Person.PersonID));
            KeyService.RegisterKey <Phone>("PhoneID");

            // Establish all the relationships we know about via database and/or object model
            KeyService.RegisterRelationship <Person>(TypeChildRelationship.Create <Person>("ParentPersonID").MapsToChildProperty(nameof(Person.Kids)));
            KeyService.RegisterRelationship <Person>(TypeChildRelationship.Create <Phone>().MapsToParentProperty(nameof(Phone.Owner)).MapsToChildProperty(nameof(Person.Phones)));

            // Restrict person age to a range
            ValidationService.RegisterCustomValidation((Person p) =>
            {
                if (p.Age < 0 || p.Age > 120)
                {
                    return("Age must be between 0 and 120.");
                }
                return(null);
            }, nameof(Person.Age));

            // This will construct a new test database, if needed - if the script changes, you'll need to drop the CodexMicroORMTest database before running
            using (CEF.NewConnectionScope(new ConnectionScopeSettings()
            {
                IsTransactional = false, ConnectionStringOverride = $@"Data Source={DB_SERVER};Database=master;Integrated Security=SSPI;MultipleActiveResultSets=true"
            }))
            {
                CEF.CurrentDBService().ExecuteRaw(File.ReadAllText("setup.sql"), false);
            }
        }