Ejemplo n.º 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();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Assigns the values of a Recipe entity's 
 /// properties to the properties of a Recipe
 /// data model.
 /// </summary>
 /// <param name="recipe">The Recipe entity
 /// containing the property values to assign.
 /// </param>
 /// <param name="dbRecipe">The Recipe data
 /// model to receive the property values.
 /// </param>
 private void AssignEntityToModel(RecipeEntity recipe, Recipe dbRecipe)
 {
     dbRecipe.FinalGravity = recipe.FinalGravity;
     dbRecipe.GrainBill = recipe.GrainBill;
     dbRecipe.Instructions = recipe.Instructions;
     dbRecipe.Name = recipe.Name;
     dbRecipe.OriginalGravity = recipe.OriginalGravity;
     dbRecipe.RecipeId = recipe.RecipeId;
 }