Add MRC to your IoC/DI Web API Project
by clayshannon
HTML
<h2>Neologists of the Net Unite!</h2>
How suite it is to add a new triumvirate to your MRC extravaganza (MRC is an old acronym I just (recently) made up, which
stands for Model Repository Controller (a Web API project has no "View" element - that's the clients' concern/bailiwick)).
<p>You can explore that initial neologism <a href="~">here</a>, which also shows you how to create the easiest-possible
working Web API app with IoC and DI using Castle Windsor.</p>
<p>Once you've done that (you've got a Windsorized Web API app), it is easy to add on to it when/if you need another
Model/Repository/Controller triad. Here's how:</p>
<h2>Add A Model</h2>
<p>Right-click your project's Models folder, and select Add Class...</p>
<p>Name it something, such as "Peeps" (not the sickeningly sweet and fancifully-colored tooth eroder, but the cool slang word for "people")</p>
<p>Give it some members, such as:</p>
<pre lang="cs">
public class Peeps
{
public int Id { get; set; }
public string Name { get; set; }
public string Hometown { get; set; }
}
</pre>
<h2>Add A Repository Interface</h2>
<p>Create an interface for the Repository by right-clicking the Models folder and selecting Add > Interface</p>
<p>Name it "IPeepsRepository</p>
<p>Mark the interface public and give it some reasonable methods to implement so that it looks like this:</p>
<pre lang="cs">
public interface IPeepsRepository
{
int GetCount();
Peeps GetById(int ID);
IEnumerable<Peeps> GetRange(int ID, int CountToFetch);
IEnumerable<Peeps> GetAll();
Peeps Add(Peeps item);
}
</pre>
<h2>Add A Concrete Repository Class</h2>
<p>Again, right-click the Models folder, this time selecting Add > Class...</p>
<p>Name it "PeepsRepository" (or risk the wrath of Philippe Khan, who is an old friend of mine and a cousin of Shere Khan)
</p>
<p>Add code so that it implements the interface (IPeepsRepository) and then add those...