Пример #1
0
        /// <summary>
        /// Saves the state of a recipe to persistent
        /// storage.
        /// </summary>
        /// <param name="recipeEntity">The recipe to persist
        /// in storage.</param>
        public void Save(RecipeEntity recipeEntity)
        {
            if (recipeEntity == null)
            {
                throw new ArgumentNullException("recipe",
                    "You must specify a recipe to save.");
            }

            if (recipeEntity.Style == null)
            {
                throw new InvalidOperationException(
                    "Recipes require a style.");
            }

            if (recipeEntity.Contributor == null)
            {
                throw new InvalidOperationException(
                    "Recipes require a contributor.");
            }

            // Load the style assigned to the recipe.  We load this because
            // it may change from whatthe current db model is.
            var existingStyleModel = new Style();
            AssignEntityToModel(recipeEntity.Style, existingStyleModel);

            this
                .Context
                .Styles
                .Attach(existingStyleModel);

            // If it's a new recipe we have to do a decent bit of work.
            if (recipeEntity.RecipeId == 0)
            {
                // Create the recipe.
                var newRecipeModel = new Recipe();
                // Add the recipe to the context for change tracking.
                this.Context.Recipes.Add(newRecipeModel);

                // Assumes the user already exists and the domain has validated
                // this is the user that created the model
                var existingUserModel = new UserProfile();
                AssignEntityToModel(recipeEntity.Contributor, existingUserModel);

                this
                    .Context
                    .UserProfiles
                    .Attach(existingUserModel);

                // Assign the properties that can only be assigned on creation.
                newRecipeModel.Style = existingStyleModel;
                newRecipeModel.Slug = recipeEntity.Slug;
                newRecipeModel.Contributor = existingUserModel;
                AssignEntityToModel(recipeEntity, newRecipeModel);
                this.Context.SaveChanges();

                recipeEntity.RecipeId = newRecipeModel.RecipeId;

                return;
            }

            var recipeModel = this
                .Context
                .Recipes
                .Include("Style")
                .Include("Contributor")
                .FirstOrDefault(r => r.RecipeId == recipeEntity.RecipeId);

            if (recipeModel == null)
            {
                throw new InvalidOperationException(
                    "The recipe being modified does not exist.");
            }

            AssignEntityToModel(recipeEntity, recipeModel);
            recipeModel.Style = existingStyleModel;

            this.Context.SaveChanges();
        }
Пример #2
0
 /// <summary>
 /// Assigns the values of a Style entity's 
 /// properties to the properties of a Style
 /// data model.
 /// </summary>
 /// <param name="style">The Style entity
 /// containing the property values to assign.
 /// </param>
 /// <param name="dbStyle">The Style data
 /// model to receive the property values.
 /// </param>
 private void AssignEntityToModel(StyleEntity style, Style dbStyle)
 {
     dbStyle.Name = style.Name;
     dbStyle.StyleId = style.StyleId;
     dbStyle.Category = ConvertToModel(style.Category);
     dbStyle.Slug = style.Slug;
 }