Beispiel #1
0
        public void SaveStudentRequestWithEnrollmentsWithoutRules2()
        {
            IFlowManager      flowManager      = serviceProvider.GetRequiredService <IFlowManager>();
            ISchoolRepository schoolRepository = serviceProvider.GetRequiredService <ISchoolRepository>();
            var student = flowManager.SchoolRepository.GetAsync <StudentModel, Student>
                          (
                s => s.FullName == "Carson Alexander",
                selectExpandDefinition: new LogicBuilder.Expressions.Utils.Expansions.SelectExpandDefinition
            {
                ExpandedItems = new List <LogicBuilder.Expressions.Utils.Expansions.SelectExpandItem>
                {
                    new LogicBuilder.Expressions.Utils.Expansions.SelectExpandItem {
                        MemberName = "enrollments"
                    }
                }
            }
                          ).Result.Single();

            student.FirstName   = "First";
            student.EntityState = LogicBuilder.Domain.EntityStateType.Modified;
            student.Enrollments.ToList().ForEach(enrollment =>
            {
                enrollment.Grade       = Domain.Entities.Grade.A;
                enrollment.EntityState = LogicBuilder.Domain.EntityStateType.Modified;
            });
            SaveEntityRequest saveStudentRequest = new SaveEntityRequest {
                Entity = student
            };

            System.Diagnostics.Stopwatch stopWatch = System.Diagnostics.Stopwatch.StartNew();
            StudentModel       studentModel        = (StudentModel)saveStudentRequest.Entity;
            SaveEntityResponse saveStudentResponse = new SaveEntityResponse();

            saveStudentResponse.Success = schoolRepository.SaveGraphAsync <StudentModel, Student>(studentModel).Result;

            if (!saveStudentResponse.Success)
            {
                return;
            }

            studentModel = schoolRepository.GetAsync <StudentModel, Student>
                           (
                f => f.ID == studentModel.ID,
                null,
                new LogicBuilder.Expressions.Utils.Expansions.SelectExpandDefinition
            {
                ExpandedItems = new List <LogicBuilder.Expressions.Utils.Expansions.SelectExpandItem>
                {
                    new LogicBuilder.Expressions.Utils.Expansions.SelectExpandItem {
                        MemberName = "enrollments"
                    }
                }
            }
                           ).Result.SingleOrDefault();

            saveStudentResponse.Entity = studentModel;

            int Iteration_Index = 0;

            flowManager.CustomActions.WriteToLog
            (
                string.Format
                (
                    "EnrollmentCount: {0}. Index: {1}",
                    new object[]
            {
                studentModel.Enrollments.Count,
                Iteration_Index
            }
                )
            );

            EnrollmentModel enrollmentModel = null;

            while (Iteration_Index < studentModel.Enrollments.Count)
            {
                enrollmentModel = studentModel.Enrollments.ElementAt(Iteration_Index);
                Iteration_Index = Iteration_Index + 1;
                flowManager.CustomActions.WriteToLog
                (
                    string.Format
                    (
                        "Student Id:{0} is enrolled in {1}.",
                        new object[]
                {
                    studentModel.ID,
                    enrollmentModel.CourseTitle
                }
                    )
                );
            }

            stopWatch.Stop();
            this.output.WriteLine("WithoutRulesUsingExpressiionsInCode = {0}", stopWatch.Elapsed.TotalMilliseconds);
        }
Beispiel #2
0
        private SaveEntityResponse SaveStudentWithoutRules(SaveEntityRequest saveStudentRequest)
        {
            Domain.Entities.StudentModel studentModel        = (StudentModel)saveStudentRequest.Entity;
            SaveEntityResponse           saveStudentResponse = new SaveEntityResponse()
            {
                ErrorMessages = new List <string>()
            };

            #region Validation
            if (string.IsNullOrWhiteSpace(studentModel.FirstName))
            {
                saveStudentResponse.ErrorMessages.Add("First Name is required.");
            }
            if (string.IsNullOrWhiteSpace(studentModel.LastName))
            {
                saveStudentResponse.ErrorMessages.Add("Last Name is required.");
            }
            if (studentModel.EnrollmentDate == default)
            {
                saveStudentResponse.ErrorMessages.Add("Enrollment Date is required.");
            }

            if (saveStudentResponse.ErrorMessages.Any())
            {
                flowManager.CustomActions.WriteToLog("An error occurred saving the request.");
                saveStudentResponse.Success = false;
                return(saveStudentResponse);
            }
            #endregion Validation

            #region Save and retrieve
            saveStudentResponse.Success = schoolRepository.SaveGraphAsync <Domain.Entities.StudentModel, Data.Entities.Student>(studentModel).Result;
            if (!saveStudentResponse.Success)
            {
                return(saveStudentResponse);
            }

            studentModel = schoolRepository.GetAsync <Domain.Entities.StudentModel, Data.Entities.Student>
                           (
                f => f.ID == studentModel.ID,
                null,
                new LogicBuilder.Expressions.Utils.Expansions.SelectExpandDefinition
            {
                ExpandedItems = new List <LogicBuilder.Expressions.Utils.Expansions.SelectExpandItem>
                {
                    new LogicBuilder.Expressions.Utils.Expansions.SelectExpandItem {
                        MemberName = "enrollments"
                    }
                }
            }
                           ).Result.SingleOrDefault();

            saveStudentResponse.Entity = studentModel;
            #endregion Save and retrieve

            #region Log Enrollments
            int Iteration_Index = 0;

            flowManager.CustomActions.WriteToLog
            (
                string.Format
                (
                    "EnrollmentCount: {0}. Index: {1}",
                    new object[]
            {
                studentModel.Enrollments.Count,
                Iteration_Index
            }
                )
            );

            Domain.Entities.EnrollmentModel enrollmentModel = null;
            while (Iteration_Index < studentModel.Enrollments.Count)
            {
                enrollmentModel = studentModel.Enrollments.ElementAt(Iteration_Index);
                Iteration_Index = Iteration_Index + 1;
                flowManager.CustomActions.WriteToLog
                (
                    string.Format
                    (
                        "Student Id:{0} is enrolled in {1}.",
                        new object[]
                {
                    studentModel.ID,
                    enrollmentModel.CourseTitle
                }
                    )
                );
            }
            #endregion Log Enrollments

            return(saveStudentResponse);
        }