(初识MVC Core)二、建立基础逻辑数据

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
    {
        /// <summary>
        /// 获取所有电影院数据
        /// </summary>
        /// <returns></returns>
        Task<IEnumerable<Cinema>> GetAllAsync();

        /// <summary>
        /// 根据电影院编码获取电影院详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<Cinema> GetCinemaById(int id);

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        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
    {
        /// <summary>
        /// 通过电影院编码获取电影
        /// </summary>
        /// <returns></returns>
        Task<IEnumerable<Movie>> GetByCinemaAsync(int cinemaId);

        /// <summary>
        /// 添加电影
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        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 = "万达影城2",
                Location = "湖里万达5L",
                Capacity = 1000
            });
        }

        /// <summary>
        /// 添加电影院
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Task AddAsync(Cinema model)
        {
            var maxId = _cinemaList.Max(x=>x.Id);
            model.Id = maxId + 1;
            _cinemaList.Add(model);
            return Task.CompletedTask;
        }

        /// <summary>
        /// 电影院列表
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<Cinema>> GetAllAsync()
        {
            return Task.Run(()=>_cinemaList.AsEnumerable());
        }

        /// <summary>
        /// 通过电影院编码获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<Cinema> GetCinemaById(int id)
        {
            return Task.Run(()=>_cinemaList.FirstOrDefault(c=>c.Id == id));
        }
    }
}
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 = "美国佬",
                ReleaseDate = Convert.ToDateTime("2018-03-27")
            });

            _movieList.Add(new Movie
            {
                Id = 1,
                Name = "绿皮书",
                CinemaId = 2,
                Starring = "I Don't Know",
                ReleaseDate = Convert.ToDateTime("2019-03-18")
            });

            _movieList.Add(new Movie
            {
                Id = 1,
                Name = "海王",
                CinemaId = 2,
                Starring = "波塞冬",
                ReleaseDate = Convert.ToDateTime("2019-03-12")
            });
        }

        /// <summary>
        /// 添加电影
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Task AddAsync(Movie model)
        {
            var maxId = _movieList.Max(m=>m.Id);
            model.Id = maxId + 1;
            _movieList.Add(model);
            return Task.CompletedTask;
        }

        /// <summary>
        /// 根据电影院编码获取电影
        /// </summary>
        /// <param name="cinemaId"></param>
        /// <returns></returns>
        public Task<IEnumerable<Movie>> GetByCinemaAsync(int cinemaId)
        {
            return Task.Run(()=>_movieList.Where(m=>m.CinemaId == cinemaId));
        }
    }
}

 

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

 

posted @ 2019-03-19 23:33  蜗牛的礼物  阅读(188)  评论(0编辑  收藏  举报