做一个关于.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