コード例 #1
0
        public Blog AddBlog(VoBlog voBlog)
        {
            var blog = new Blog
            {
                CreatedDate = voBlog.CreatedDateTime,
                UpdatedDate = voBlog.CreatedDateTime,
                Overview    = voBlog.Overview,
                UserId      = voBlog.UserId
            };

            BloggerDbContext.Blogs.Add(blog);
            return(blog);
        }
コード例 #2
0
        public void AddBlog(VoBlog voBlog)
        {
            //Demonstration of creating an AmbientDbContext in transaction mode
            using (var dbContextScope = new DbContextScopeFactory().CreateAmbientDbContextInTransactionMode <BloggerDbContext>())
            {
                bool exceptionOccured = false;
                var  user             = _userOperations.GetUser(voBlog.UserId);
                try
                {
                    Console.WriteLine("BlogPostCreationFailureCount :" + user.BlogPostCreationFailureCount);
                    var blog = _blogOperations.AddBlog(voBlog);
                    var post = _postOperations.AddPost(voBlog.Post);
                    blog.BlogPost = post;
                    dbContextScope.SaveChanges();
                    dbContextScope.Commit();
                }
                catch (Exception)
                {
                    exceptionOccured = true;
                    throw;
                }
                finally
                {
                    if (exceptionOccured)
                    {
                        var userOperations = new UserOperations();
                        using (
                            var nonAmbientdbContextScope =
                                new DbContextScopeFactory().CreateNonAmbientDbContextInTransactionMode <BloggerDbContext>())
                        {
                            //Setting the user operations dbContext with the dbContext from DbContextScope just created
                            //else we would be using ambient DbContext.
                            userOperations.UpdateBlogCreationFailureCount(voBlog.UserId);

                            var updateduser = userOperations.GetUser(voBlog.UserId);
                            nonAmbientdbContextScope.RefreshParentCacheWithUpdatedEntities(new List <User> {
                                updateduser
                            });
                            Console.WriteLine("BlogPostCreationFailureCount :" + user.BlogPostCreationFailureCount);
                        }
                    }
                }
            }
        }
コード例 #3
0
 public VoBlog GetBlog(int blogId)
 {
     //Demonstration of creating an AmbientDbContext in readonly mode
     using (new DbContextScopeFactory().CreateAmbientDbContextInReadonlyMode <BloggerDbContext>())
     {
         var blog   = _blogOperations.GetBlog(blogId);
         var voBlog = new VoBlog
         {
             Overview        = blog.Overview,
             CreatedDateTime = blog.CreatedDate,
             PostId          = blog.PostId
         };
         var post = _postOperations.GetPost(blog.PostId);
         voBlog.Post = new VoPost
         {
             Content          = post.Content,
             Meta             = post.Meta,
             ShortDescription = post.ShortDescription,
             Title            = post.Title
         };
         return(voBlog);
     }
 }
コード例 #4
0
 public VoBlog GetUserRecentBlog(long userId)
 {
     //Demonstration of creating an AmbientDbContext in readonly mode
     using (new DbContextScopeFactory().CreateAmbientDbContextInReadonlyMode <BloggerDbContext>())
     {
         var userRecentBlog = _blogOperations.GetUserRecentBlog(userId);
         var blogDto        = new VoBlog
         {
             CreatedDateTime = userRecentBlog.CreatedDate,
             Overview        = userRecentBlog.Overview,
             PostId          = userRecentBlog.PostId
         };
         var post = _postOperations.GetPost(userRecentBlog.PostId);
         blogDto.Post = new VoPost
         {
             Content          = post.Content,
             Meta             = post.Meta,
             ShortDescription = post.ShortDescription,
             Title            = post.Title
         };
         return(blogDto);
     }
 }
コード例 #5
0
        static void Main()
        {
            Console.WriteLine("Creating Database first");
            //Creating database first
            var dropCreateDatabaseAlways = new DropCreateDatabaseAlways <BloggerDbContext>();

            Database.SetInitializer(dropCreateDatabaseAlways);
            var context = new BloggerDbContext();

            dropCreateDatabaseAlways.InitializeDatabase(context);

            //Creating an user
            Console.WriteLine("Creating an user");
            IUserServices userRepository = new UserServices();
            var           userId         = userRepository.AddUser(new VoUser
            {
                Name       = "TestUser",
                Occupation = "Software Developer"
            });

            //Creating a blog and its post
            Console.WriteLine("Creating a blog post");
            IBlogServices blogRepository = new BlogServices();
            var           blog           = new VoBlog
            {
                CreatedDateTime = DateTime.Now,
                Overview        = "This is a sample overview.",
                Post            = new VoPost
                {
                    Meta             = "Sample, Test",
                    Content          = "This is a sample overview",
                    ShortDescription = "This is a sample short description",
                    Title            = "Test Title"
                },
                UserId = userId
            };

            blogRepository.AddBlog(blog);

            //Retrieving the user in an async mode. This is where thread switching happens
            Console.WriteLine("Retrieving the created user");
            var user = userRepository.GetUserAsync(userId).ConfigureAwait(false);

            Console.WriteLine("Name of the created user is " + user.GetAwaiter().GetResult().Name);

            //Get the user's most recent blog
            Console.WriteLine("Retrieving the user most recent blog");
            var recentBlog = blogRepository.GetUserRecentBlog(userId);

            Console.WriteLine("Overview " + recentBlog.Overview);
            Console.WriteLine("BlogPost Meta " + recentBlog.Post.Meta);
            Console.WriteLine("BlogPost Short Description " + recentBlog.Post.ShortDescription);
            Console.WriteLine("BlogPost Content " + recentBlog.Post.Content);

            //Create a blog and try to save it. If the creation fails , update the  creation failure count in the user profile.
            var blog2 = new VoBlog
            {
                CreatedDateTime = DateTime.Now,
                Post            = new VoPost
                {
                    Meta             = "Sample, Test",
                    Content          = "This is a sample overview",
                    ShortDescription = "This is a sample short description",
                    Title            = "Test Title"
                },
                UserId = userId
            };

            try
            {
                blogRepository.AddBlog(blog2);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.Message);
            }
            Console.ReadKey();
        }