public void Update( ApplicationUser user, string title, string subtitle, string description, LanguageTypeEnum?languageType, string prerequisites, string refferences, string targetStudents, decimal?unitPrice, decimal?discountAmount, double?discountPercent, string imageUrl, IList <EntityAttribute> entityAttributes ) { // 1. varify course update CourseUpdateValidate(user); // 2. update course basic info Title = title ?? Title; Subtitle = subtitle ?? Subtitle; Description = description ?? Description; LanguageType = languageType ?? LanguageType; ImageUrl = imageUrl ?? ImageUrl; Prerequisites = prerequisites ?? prerequisites; Refferences = refferences ?? refferences; TargetStudents = targetStudents ?? targetStudents; UnitPrice = unitPrice ?? UnitPrice; DiscountAmount = discountAmount ?? DiscountAmount; DiscountPercent = discountPercent ?? DiscountPercent; LastUpdateDateUTC = DateTime.UtcNow; // 3. update course attributes if (entityAttributes != null) { // remove the old course attributes var courseAttributes = Attributes.ToList(); var deleteAttibutes = courseAttributes.Where(a => !entityAttributes.Any(e => a.EntityAttributeId == e.Id)); courseAttributes.RemoveAll(a => deleteAttibutes.Any(d => d.Id == a.Id)); // add the new course attributes var newEntityAttibutes = entityAttributes.Where(e => !courseAttributes.Any(a => e.Id == a.EntityAttributeId)); foreach (var entityAttibute in newEntityAttibutes) { courseAttributes.Add(CourseAttribute.Create(this, entityAttibute)); } Attributes = courseAttributes; } }
/// <summary> /// Domain Logic /// </summary> #region rich domain model logic public static Course Create( ApplicationUser user, string title, string subtitle, string description, LanguageTypeEnum?languageType, string imageUrl, IList <EntityAttribute> entityAttributes, string vimeoAlbumId = null ) { // 1. check user if (user.Tutor == null) { throw new CourseValidateException("Course can only be create by Tutor"); } // 2. create new course var course = new Course() { Tutor = user.Tutor, Title = title, Subtitle = subtitle, Description = description, LanguageType = languageType ?? LanguageTypeEnum.English, CreateDateUTC = DateTime.UtcNow, LastUpdateDateUTC = DateTime.UtcNow, ImageUrl = imageUrl, VimeoAlbumId = vimeoAlbumId }; // 3. add course attributes /// 虽然只有两行,但是效率较低,还是用for loop吧 //var courseAttibute = entityAttributes.Select(a => CourseAttribute.Create(course, a)); //((List<CourseAttribute>)course.Attributes).AddRange(courseAttibute); foreach (var entityAttibute in entityAttributes) { var courseAttibute = CourseAttribute.Create(course, entityAttibute); course.Attributes.Add(courseAttibute); } // 4. done return(course); }