向您的 ASP.NET Web API Castle Windsorized (IoC/DI) 项目添加 Model、Repository 和 Controller 组件






4.85/5 (4投票s)
如何为附加的 Web API 数据点添加必要的代码
网络新词创造者们,团结起来!
向您的 MRC 盛会添加新的三驾马车是多么棒啊(MRC 是我最近编造的一个旧首字母缩略词,代表 Model Repository Controller (Web API 项目没有“View”元素 - 这是客户端的责任/管辖范围 *))。* 对于熟悉老电视剧“Dragnet”的人,您可能会想象客户端对 REST 方法说:“只说事实,女士”。服务器无需关心演示文稿 - 那是客户端的工作。服务器提供的只是原始的、未加修饰的、纯粹的数据。然后,客户端可以解析它、格式化它、美化它、忽略它或任何它想做的事情。
对于那些不熟悉“Dragnet”的人,请记住古老的格言“年龄优先于美丽!”无论如何,您可能可以大致了解它的全部内容。
现在,回到我们安排的节目: 您可以在此处探索最初的新词,其中还向您展示了如何使用 Castle Windsor 创建最简单的可行的 Web API 应用程序,其中包含 IoC 和 DI。
完成此操作后(您已经拥有一个 Windsorized Web API 应用程序),当/如果您需要另一个 Model/Repository/Controller 三元组时,很容易在其上添加。 这是方法
添加 Model
右键单击您的项目的Models文件夹,然后选择添加类...
将其命名为诸如“Peeps”之类的名称(不是那种令人作呕的甜腻且色彩艳丽的牙齿腐蚀剂,而是“人们”的酷俚语)。
给它一些成员,例如
public class Peeps
{
public int Id { get; set; }
public string Name { get; set; }
public string Hometown { get; set; }
}
添加 Repository 接口
通过右键单击Models文件夹并选择添加 > 接口,为 Repository 创建一个接口
将其命名为“IPeepsRepository
”。
将接口标记为public
,并为其提供一些合理的实现方法,使其看起来像这样
public interface IPeepsRepository
{
int GetCount();
Peeps GetById(int ID);
IEnumerable<peeps> GetRange(int ID, int CountToFetch);
IEnumerable<peeps> GetAll();
Peeps Add(Peeps item);
}
添加具体的 Repository 类
再次,右键单击Models文件夹,这次选择添加 > 类...
将其命名为“PeepsRepository
”(否则可能会冒着Philippe Kahn的愤怒,他是我的一位老朋友,也是Shere Khan的表弟)。
添加代码,使其实现接口(IPeepsRepository
),然后添加这些方法并用代码填充它们,甚至在构造函数中添加一些测试数据,使其看起来像这样
public class PeepsRepository : IPeepsRepository
{
private readonly List<peeps> Peeps = new List<peeps>();
public PeepsRepository()
{
Add(new Peeps
{
Id = 1, Name = "T. Russell", Hometown = "Zenia, California"
});
Add(new Peeps
{
Id = 2,
Name = "A. Rosalie Kollenborn",
Hometown = "Dug Hill, Arkansas"
});
Add(new Peeps
{
Id = 3,
Name = "Theodore P. Deck",
Hometown = "Lombardi, Illinois"
});
Add(new Peeps
{
Id = 4,
Name = "Jonathan Kelly",
Hometown = "Mokelumne Hill, California"
});
Add(new Peeps
{
Id = 5,
Name = "Trish Baugus",
Hometown = "La Contenta, California"
});
Add(new Peeps
{
Id = 6,
Name = "Cherri Flowers",
Hometown = "Sonora, California"
});
Add(new Peeps
{
Id = 7,
Name = "Kelvin Caleb Mordecai",
Hometown = "Jackson, California"
});
Add(new Peeps
{
Id = 8,
Name = "Morgan Tell Mackenzie",
Hometown = "San Andreas, California"
});
Add(new Peeps
{
Id = 9,
Name = "Kamrin Marinoff",
Hometown = "Huntington Beach, California"
});
}
public int GetCount()
{
return Peeps.Count;
}
public Peeps GetById(int ID)
{
return Peeps.FirstOrDefault(p => p.Id == ID);
}
public IEnumerable<peeps> GetRange(int ID, int CountToFetch)
{
return Peeps.Where(i => i.Id >= ID).Take(CountToFetch);
}
public IEnumerable<peeps> GetAll()
{
return Peeps;
}
public Peeps Add(Peeps item)
{
if (item == null)
{
throw new ArgumentNullException("Peeps");
}
Peeps.Add(item);
return item;
}
}
添加 Controller
右键单击您的Controllers文件夹(您的 Web API 项目确实有一个Controllers文件夹,不是吗? - 如果没有,则添加一个,或添加一个名为“api”或任何对您有效的文件),然后选择添加 > Controller... > Web API 2 Controller - 空。
将其命名为“PeepsController
”。
添加 IoC/DI/Castle Windsor 风格的 repository 变量和接口参数的构造函数
private readonly IPeepsRepository _peepsRepository;
public PeepsController(IPeepsRepository peepsRepository)
{
if (peepsRepository == null)
{
throw new ArgumentNullException
("peepsRepository popped its peeper or pepper pot");
}
_peepsRepository = peepsRepository;
}
一旦您习惯了这项艰苦的活动,就可以在不流汗(或对于数量极其稀少的女性程序员来说,没有闪闪发光)的情况下完成它
永远不要说骑士精神已死!
现在,在深吸一口气并伸展身体后(打哈欠/打哈欠是可选的),添加 Controller 代码的其余部分
[Route("api/Peeps/Count")]
public int GetCountOfPeepsRecords()
{
return _peepsRepository.GetCount();
}
[Route("api/Peeps/GetAll")]
public IEnumerable<peeps> GetAllPeeps()
{
return _peepsRepository.GetAll();
}
[Route("api/Peeps/{ID:int}")]
public Peeps GetPeepsById(int ID)
{
return _peepsRepository.GetById(ID);
}
[Route("api/Peeps/{ID:int}/{CountToFetch:int}")]
public IEnumerable<peeps> GetRangeOfPeepsByStartingID(int ID, int CountToFetch)
{
return _peepsRepository.GetRange(ID, CountToFetch);
}
}
Recordar el Castillo de Arena
Castle Windsor 不是沙堡,但我喜欢那句西班牙语“Castillo de Arena”;无论如何,最后,在您的 Repositories Installer 中添加一行代码(如果您按照上面引用的文章进行操作,它位于您的DIInstallers文件夹中),以便该类看起来像这样(新条目添加到中间)
public class RepositoriesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<ispy>().ImplementedBy<spy>().LifestylePerWebRequest(),
. . .
Component.For<ipeepsrepository>().ImplementedBy<peepsrepository>().LifestylePerWebRequest(),
. . .
Component.For<iofthetyger>().ImplementedBy<ofthetyger>().LifestylePerWebRequest());
}
}
检查一下
自己尝试一下;在浏览器中输入适当的 URI(例如“https://:28642/api/peeps/GetAll[^]”等),您将看到返回了期望的数据。
看!这比从一堆日志文件上掉下来掉进泥坑里还要容易(而且几乎一样有趣)。