using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
public class MockNoodleRepository: Interface
{
public List<Noodle> _noodles;
public IEnumerable<Noodle> GetAllNoodles()
{
_noodles = new List<Noodle>();
return _noodles;
}
public Noodle GetNoodleById(int id)
{
Noodle no = new Noodle();
id = 2;
return no;
}
}
}
在视图中指定所使用的模型

数据与页网绑定

通过Controller的构造函数中引用依赖

在项目的不同位置需要使用它,则需要注入服务依赖

IEnumerable<T> 是 System.Collections.Generic 命名空间中的集合的基接口,如 List<T>、Dictionary<TKey,TValue>和 Stack<T> 以及其他泛型集合,如 ObservableCollection<T> 和 ConcurrentStack<T>。 可以通过使用 foreach 语句来枚举实现 IEnumerable<T> 的集合。
依赖注入
仓库注册services.AddTransient<INoodleRepository,MockNoodleResposity>();//每次发起请求后创建一个全新的仓库,请求结束后自动注销这个仓库
services.AddSingleton;//系统启动有且仅创建一个仓库,系统每次请求使用同一个仓库实例
services.AddScoped;//将一系列请求或操作整合在一个事务里,这个事务有且仅创建一个实例,事务结束后会自动注销这个实例