public Section(Guid sectionId, Term term, Course course, string sectionNumber) : base(sectionId) { var termData = term.BuildMememto(); var courseData = course.BuildMemento(); if (courseData.IsCredit && string.IsNullOrEmpty(courseData.ApprovalNumber) && string.IsNullOrEmpty(courseData.CIP)) throw new InvalidStateException( "Your attempt to create the section failed. Set approval number or CIP at the course level first."); if (!courseData.IsCredit && courseData.CreditType != CreditTypes.SpecialInterests && string.IsNullOrEmpty(courseData.ApprovalNumber) && string.IsNullOrEmpty(courseData.CIP)) throw new InvalidStateException( "Your attempt to create a section failed. The course doesn't have an approval number or CIP, and it's not a special interests course."); var createEvent = courseData.IsCredit ? (IEvent) new CreditSectionCreatedEvent( sectionId, courseData.Id, courseData.Rubric, courseData.CourseNumber, termData.Id, termData.Abbreviation, termData.Name, sectionNumber) : new ContinuingEducationSectionCreatedEvent( sectionId, courseData.Id, courseData.Rubric, courseData.CourseNumber, termData.Id, termData.Abbreviation, termData.Name, sectionNumber); ApplyEvent(createEvent); if (courseData.IsCredit) ChangeDates(termData.Start, termData.End); ApplyEvent(new SectionTitleChangedEvent( sectionId, null, courseData.Title)); if (!string.IsNullOrEmpty(courseData.ApprovalNumber)) ApplyEvent(new SectionApprovalNumberChangedEvent( sectionId, courseData.ApprovalNumber)); if (!string.IsNullOrEmpty(courseData.CIP)) ApplyEvent(new SectionCIPChangedEvent( sectionId, courseData.CIP)); foreach (var courseType in courseData.CourseTypes) ApplyEvent(new SectionCourseTypeAddedEvent( sectionId, courseType, _courseTypes.Union(new[] {courseType}).Distinct())); ChangeCreditType(courseData.CreditType); ApplyEvent(new SectionMadePendingEvent(sectionId)); if (!courseData.IsCredit) ApplyEvent(new SectionCEUsChangedEvent( sectionId, courseData.CEUs)); if (courseData.TopicCodeId != default(Guid)) { var uow = UnitOfWorkContext.Current; var topicCode = uow.GetById<TopicCode>(courseData.TopicCodeId); var topicCodeData = topicCode.BuildMemento(); ApplyEvent(new SectionTopicCodeChangedEvent( sectionId, topicCodeData.Id, topicCodeData.Abbreviation, topicCodeData.Description)); } }