1. 程式人生 > >.net core mvc初級教程(一)

.net core mvc初級教程(一)

做一個關於.net core mvc的初級教程,
第一篇的目錄
一、建立新專案,專案名為DemoCoreStudy
二、建立類庫DemoCoreStudy.Models,在其中新增Cinema,Movie,Sales類
三、建立服務,服務註冊

第一篇為準備工程

一、建立新專案,專案名為DemoCoreStudy

在這裡插入圖片描述

在這裡插入圖片描述

DemoCoreStudy專案新增引用DemoCoreStudy.Models專案

二、DemoCoreStudy.Models專案新增三個類在其中新增Cinema,Movie,Sales類

Cinema類

using System;

namespace DemoCoreStudy.Models
{
    public class Cinema
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }//地址
        public int Capacity { get; set; }//容納多少觀眾
    }
}

Movie類

using System;

namespace DemoCoreStudy.Models
{
    public class Movie
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int CinemaId { get; set; }
        public string Starring { get; set; }
        public DateTime ReleseDate { get; set; }
    }
}

Sales類

namespace DemoCoreStudy.Models
{
    public class Sales
    {
        public int CinemaId { get; set; }
        public int MovieId { get; set; }
        public int AudienceCount { get; set; }//賣出多少票
    }
}

三、建立服務,服務註冊

在DemoCoreStudy專案中新增介面ICinemaService,IMovieService與實現類CinemaMemoryService,MovieMemoryService

ICinemaService介面程式碼

using DemoCoreStudy.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DemoCoreStudy.Serivce
{
    public interface ICinemaService
    {
        Task<IEnumerable<Cinema>> GetllAllAsync();//查詢所有的Cinema值
        Task<Cinema> GetByIdAsync(int id);//查詢指定ID的Cinema
        Task AddAsync(Cinema model);//建立Cinema值
    }
}

IMovieService介面程式碼

using DemoCoreStudy.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DemoCoreStudy.Serivce
{
    public interface IMovieService
    {
        Task AddAsync(Movie model);
        Task<IEnumerable<Movie>> GetByCinemaAsync(int cinemaId);
    }
}

CinemaMemoryService實現類,並新增種子資料

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DemoCoreStudy.Models;

namespace DemoCoreStudy.Serivce
{
    public class CinemaMemoryService : ICinemaService
    {
        private readonly List<Cinema> _cinema=new List<Cinema>();

        public CinemaMemoryService()
        {
            _cinema.Add(new Cinema
            {
                Name ="天堂電影院",
                Location = "上海",
                Capacity = 1000
            });
            _cinema.Add(new Cinema
            {
                Name = "瘋人電影院",
                Location = "北京",
                Capacity = 10000
            });
        }

        public Task<IEnumerable<Cinema>> GetllAllAsync()
        {
            return Task.Run(() => _cinema.AsEnumerable());
        }

        public Task<Cinema> GetByIdAsync(int id)
        {
            return Task.Run(() => _cinema.SingleOrDefault(x => x.Id == id));
        }

        public Task AddAsync(Cinema model)
        {
            var maxId = _cinema.Max(x => x.Id);
            model.Id = maxId + 1;
            _cinema.Add(model);
            return Task.CompletedTask;
        }
    }
}

MovieMemoryService實現類,新增種子資料

using DemoCoreStudy.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DemoCoreStudy.Serivce
{
    public class MovieMemoryService : IMovieService
    {
        private readonly List<Movie> _movies=new List<Movie>();

        public MovieMemoryService()
        {
            _movie.Add(new Movie
            {
                Id = 1,
                CinemaId = 1,
                Name = "這個殺手不太冷",
                ReleseDate = new DateTime(2018, 1, 1),
                Starring = "Tommy"
            });
            _movie.Add(new Movie
            {
                Id = 2,
                CinemaId = 2,
                Name = "笑傲江湖",
                ReleseDate = new DateTime(2018, 1, 1),
                Starring = "Tommy"
            });
            _movie.Add(new Movie
            {
                Id = 2,
                CinemaId = 2,
                Name = "小蘿莉與猴神大叔",
                ReleseDate = new DateTime(2018, 1, 1),
                Starring = "Tommy"
            });
        }

        public Task<IEnumerable<Movie>> GetByCinemaAsync(int cinemaId)
        {
            return Task.Run(() => _movies.Where(x=>x.CinemaId==cinemaId));
        }

        public Task AddAsync(Movie model)
        {
            var maxId = _movies.Max(x => x.Id);

            model.Id = maxId + 1;
            _movies.Add(model);
            return Task.CompletedTask;
        }
    }
}

接下來是服務註冊

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DemoCoreStudy.Serivce;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace DemoCoreStudy
{
    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)
        {
            services.AddSingleton<ICinemaService, CinemaMemoryService>();
            services.AddSingleton<IMovieService, MovieMemoryService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

添加了這兩行
services.AddSingleton<ICinemaService, CinemaMemoryService>();
services.AddSingleton<IMovieService, MovieMemoryService>();
不註冊是無法執行的,AddSingleton是表示生命週期

注入的三種生命週期:

//瞬時(生命週期服務在它們每次請求時被建立。這一生命週期適合輕量級的,無狀態的服務。)
//services.AddTransient();
//單例(單例生命週期服務在它們第一次被請求時建立並且每個後續請求將使用相同的例項。)
//services.AddSingleton();
//作用域(作用域生命週期服務在每次請求被建立一次。)
//services.AddScoped();

程式碼放在github上
https://github.com/1045683477/github-upload

下篇https://blog.csdn.net/qq_41841878/article/details/85345903