コード例 #1
0
 public List <CommentModel> GetAll()
 {
     using (var dbContext = dbContextFactory.CreateDbContext())
     {
         var commentMapper = new CommentMapper();
         return(dbContext.Comments.Select(e => commentMapper.EntityToModel(e)).ToList());
     }
 }
コード例 #2
0
 public CommentModel GetById(int id)
 {
     using (var dbContext = dbContextFactory.CreateDbContext())
     {
         var entity = dbContext.Comments.First(t => t.Id == id);
         var mapper = new CommentMapper();
         return(mapper.EntityToModel(entity));
     }
 }
コード例 #3
0
        public void CommentEntityModelTest()
        {
            var commentMapper = new CommentMapper();

            Assert.Null(commentMapper.EntityToModel(null));
            Assert.Null(commentMapper.ModelToEntity(null));

            var UserEntityObject = new UserEntity()
            {
                Id       = 1,
                Name     = "Jan",
                Email    = "*****@*****.**",
                Password = "******",
            };

            var CommentEntityObject = new CommentEntity()
            {
                Id         = 1,
                Author     = UserEntityObject.Id,
                AuthorName = UserEntityObject.Name,
                Text       = "NONI?"
            };

            var UserModelObject = new UserDetailModel()
            {
                Id       = 1,
                Name     = "Jan",
                Email    = "*****@*****.**",
                Password = "******",
            };

            var CommentModelObject = new CommentModel()
            {
                Id         = 1,
                Author     = UserModelObject.Id,
                AuthorName = UserModelObject.Name,
                Text       = "NONI?"
            };

            Assert.Equal(CommentModelObject, commentMapper.EntityToModel(CommentEntityObject));
            Assert.Equal(CommentEntityObject, commentMapper.ModelToEntity(CommentModelObject));
        }
コード例 #4
0
 public CommentModel Create(CommentModel model)
 {
     using (var dbContext = dbContextFactory.CreateDbContext())
     {
         var commentMapper = new CommentMapper();
         var entity        = commentMapper.ModelToEntity(model);
         dbContext.Comments.Add(entity);
         dbContext.SaveChanges();
         return(commentMapper.EntityToModel(entity));
     }
 }