由于本文主要是讲VSCode开发等, 所以相关等一些angular/.net core的知识就相对少讲点.
我把需求改一下, 如图:
由于efcore目前还是使用中间表来建立多对多的关系, 所以分类标签这个需求就没什么特别的了, 就去掉吧. 还有电视剧分季这个技术上也是重复的, 也删掉.
目前只剩下电视台和电视剧的维护, 还剩下的知识点是:
集合的CRUD操作
对项目结构进行整理, 使用Unit Of Work 以及 Repository 模式
上传文件
由于CRUD画面做起来比较简单, 我相信大家使用该技术的都会, 所以我直接把我写完的代码传上来. 此时页面的操作效果请看视频:
这时的代码:
这里面又一个比较常见的问题, 就是
针对集合的增删改:
上述业务的电视剧的增删改就会出现这种情况:
数据库里面原来有4条数据, 而前台操作完, 删除了第3条数据, 并且增加了一条数据, 其余数据的内容可能有变化.
这种集合类增删改的思路应该是这样的:
1.从传进来的集合找出所有新添加的数据(新添加的数据Id通常并不存在, 这是条件), 放在一个临时的集合, 然后添加到context.
2.从数据库集合找出所有需要删除的数据(有一些id在传进来的集合找不到的就是), 放在一个集合, 然后从conetxt一次性移除.
3.两个集合都有的数据就是只需要修改内容的数据, 更新即可.
下面开始实现这个功能:
首先确保Put方法里, 把电视台下所有的电视剧都读取出来:
接下来, 找到MappingProfile.cs, 使用AutoMapper来实现这个功能.
首先要忽略默认的集合映射:
然后调用AfterMap方法做后期手动处理, 代码的意思大概是这样:
或者, 也可以用Linq做一些重构:
回到画面试一下编辑功能:
然后添加和删除:
查看结果:
OK.
项目重构
下面, 我们使用Unit Of Work以及Repository Pattern对项目进行重构:
由于这部分也很简单, 并且也不是这篇文章的重点, 我就直接贴出重构后的代码吧:
Database/TvRepostiory.cs:
usingSystem.Collections.Generic;usingSystem.Threading.Tasks;usingMicrosoft.EntityFrameworkCore;usingTv.Models;namespaceTv.Database
{public classTvRepository : ITvRepostitory
{private readonlyTvContext context;publicTvRepository(TvContext context)
{this.context =context;
}public async Task>GetTvNetworksAsync()
{return await context.TvNetworks.Include(x =>x.TvShows).ToListAsync();
}public async Task GetTvNetworkByIdAsync(int id, bool includeRelated = true)
{if(includeRelated)
{return await context.TvNetworks.Include(x => x.TvShows).SingleOrDefaultAsync(x => x.Id ==id);
}return awaitcontext.TvNetworks.FindAsync(id);
}public voidAddTvNetwork(TvNetwork model)
{
context.TvNetworks.Add(model);
}public voidRemoveTvNetwork(TvNetwork model)
{
context.TvNetworks.Remove(model);
}
}
}
Database/ITvRepository.cs:
usingSystem.Collections.Generic;usingSystem.Threading.Tasks;usingTv.Models;namespaceTv.Database
{public interfaceITvRepostitory
{
Task>GetTvNetworksAsync();
Task GetTvNetworkByIdAsync(int id, bool includeRelated = true);voidAddTvNetwork(TvNetwork model);voidRemoveTvNetwork(TvNetwork model);
}
}
Database/UnitOfWork.cs:
usingSystem.Threading.Tasks;namespaceTv.Database
{public classUnitOfWork : IUnitOfWork
{private readonlyTvContext context;publicUnitOfWork(TvContext context)
{this.context =context;
}public asyncTask SaveAsync()
{awaitcontext.SaveChangesAsync();
}
}
}
Database/IUnitOfWork.cs:
usingSystem.Threading.Tasks;namespaceTv.Database
{public interfaceIUnitOfWork
{
Task SaveAsync();
}
}
Startup.cs:
public voidConfigureServices(IServiceCollection services)
{
services.AddAutoMapper();//services.AddDbContext(opt => opt.UseSqlServer(Configuration["ConnectionStrings:Default"]));
services.AddDbContext(opt => opt.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddScoped();
services.AddScoped();
services.AddMvc();
}
TvController.cs:
usingSystem.Collections.Generic;usingSystem.Threading.Tasks;usingAutoMapper;usingMicrosoft.AspNetCore.Mvc;usingMicrosoft.EntityFrameworkCore;usingTv.Database;usingTv.Models;usingTv.ViewModels;namespaceTv.Controllers
{public classTvController : Controller
{private readonlyITvRepostitory repostiory;private readonlyIUnitOfWork unitOfWork;private readonlyIMapper mapper;publicTvController(ITvRepostitory repostiory, IUnitOfWork unitOfWork, IMapper mapper)
{this.repostiory =repostiory;this.unitOfWork =unitOfWork;this.mapper =mapper;
}
[HttpGet("api/tvnetworks")]public async Task>GetTvNetworks()
{var models = awaitrepostiory.GetTvNetworksAsync();var vms = mapper.Map, List>(models);returnvms;
}
[HttpGet("api/tvnetworks/{id}")]public async Task Get(intid)
{var model = awaitrepostiory.GetTvNetworkByIdAsync(id);var vm = mapper.Map(model);returnOk(vm);
}
[HttpPost("api/tvnetworks")]public async TaskPost([FromBody]TvNetworkUpdateViewModel vm)
{if (!ModelState.IsValid)
{returnBadRequest(ModelState);
}var model = mapper.Map(vm);
repostiory.AddTvNetwork(model);awaitunitOfWork.SaveAsync();var result = mapper.Map(model);returnOk(result);
}
[HttpPut("api/tvnetworks/{id}")]public async Task Put(intid, [FromBody]TvNetworkUpdateViewModel vm)
{if (!ModelState.IsValid)
{returnBadRequest(ModelState);
}var dbModel = awaitrepostiory.GetTvNetworkByIdAsync(id);if (dbModel == null)
{returnNotFound();
}var model = mapper.Map(vm, dbModel);awaitunitOfWork.SaveAsync();var result = mapper.Map(model);returnOk(result);
}
[HttpDelete("api/tvnetworks/{id}")]public async Task Delete(intid)
{var model = await repostiory.GetTvNetworkByIdAsync(id, includeRelated: false);if (model == null)
{returnNotFound();
}
repostiory.RemoveTvNetwork(model);awaitunitOfWork.SaveAsync();returnNoContent();
}
}
}
再操作下画面, 没有任何问题.
今天先写到这, VSCode的开发速度还是非常快的.
还剩下最后一部分--上传文件.