1.结构
2.新建一个model类库CoreModel
建立model:Cinema.cs、Movie.cs、Sales.cs
using System; using System.Collections.Generic; using System.Text; namespace CoreModel { /// <summary> /// 电影院 /// </summary> public class Cinema { /// <summary> /// 编码 /// </summary> public int Id { get; set; } /// <summary> /// 电影院名称 /// </summary> public string Name { get; set; } /// <summary> /// 地址 /// </summary> public string Location { get; set; } /// <summary> /// 容量 /// </summary> public int Capacity { get; set; } } }
using System; using System.Collections.Generic; using System.Text; namespace CoreModel { /// <summary> /// 电影 /// </summary> public class Movie { /// <summary> /// 编码 /// </summary> public int Id { get; set; } /// <summary> /// 电影名称 /// </summary> public string Name { get; set; } /// <summary> /// 电影院编码 /// </summary> public int CinemaId { get; set; } /// <summary> /// 主演 /// </summary> public string Starring { get; set; } /// <summary> /// 上映时间 /// </summary> public DateTime ReleaseDate { get; set; } } }
using System; using System.Collections.Generic; using System.Text; namespace CoreModel { public class Sales { public int CinemaId { get; set; } public int MovieId { get; set; } public int AudienceCount { get; set; } } }
3.建立Business:
1)接口:ICinemaService.cs、IMovieService.cs
using CoreModel; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace CoreModelTwo.Services { public interface ICinemaService { Task<IEnumerable<Cinema>> GetAllAsync(); Task<Cinema> GetCinemaById(int id); Task<Sales> GetSalesAllAsync(); Task AddAsync(Cinema model); } }
using CoreModel; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace CoreModelTwo.Services { public interface IMovieService { Task<IEnumerable<Movie>> GetAllAsync(); Task AddAsync(Movie model); } }
2)Service:CinemaService.cs、MovieService.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using CoreModel; namespace CoreModelTwo.Services { public class CinemaService : ICinemaService { private readonly List<Cinema> _cinemaList = new List<Cinema>(); /// <summary> /// 初始化数据 /// </summary> public CinemaService() { _cinemaList.Add(new Cinema { Id =1, Name = "神偷奶爸", Location = "湖里万达5L", Capacity = 500 }); _cinemaList.Add(new Cinema { Id = 2, Name = "绿皮书", Location = "湖里万达5L", Capacity = 1000 }); } public Task AddAsync(Cinema model) { var maxId = _cinemaList.Max(x=>x.Id); model.Id = maxId + 1; _cinemaList.Add(model); return Task.CompletedTask; } public Task<IEnumerable<Cinema>> GetAllAsync() { return Task.Run(()=>_cinemaList.AsEnumerable()); } public Task<Cinema> GetCinemaById(int id) { return Task.Run(()=>_cinemaList.FirstOrDefault(c=>c.Id == id)); } public Task<Sales> GetSalesAllAsync() { throw new NotImplementedException(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using CoreModel; namespace CoreModelTwo.Services { public class MovieService : IMovieService { private readonly List<Movie> _movieList = new List<Movie>(); public MovieService() { _movieList.Add(new Movie { Id = 1, Name = "神偷奶爸", CinemaId = 1, Starring = "美国佬" }); _movieList.Add(new Movie { Id = 1, Name = "绿皮书", CinemaId = 2, Starring = "I Don‘t Know" }); } public Task AddAsync(Movie model) { var maxId = _movieList.Max(m=>m.Id); model.Id = maxId + 1; _movieList.Add(model); return Task.CompletedTask; } public Task<IEnumerable<Movie>> GetAllAsync() { return Task.Run(()=>_movieList.AsEnumerable()); } } }
3.修改Startup.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using CoreModelTwo.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace CoreModelTwo { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { //使用Singleton进行IoC依赖注入(每当有请求IMovieService都会返回MovieService实例) services.AddSingleton<IMovieService,MovieService>(); services.AddSingleton<ICinemaService, CinemaService>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILogger<Startup> logger) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } }
参阅:https://v.qq.com/x/page/r0744ix6a9s.html
谢谢Dave
原文地址:https://www.cnblogs.com/dzw159/p/10562276.html
时间: 2024-10-09 09:28:09