-
Notifications
You must be signed in to change notification settings - Fork 4
Quickstart
To start a new web project, start an empty ASP.NET project (do not pick any template e.g. MVC or WebAPI. They're bloated!).
Then NuGet:
Install-Package Cormo.Web
This does not add/modify any file in your project. At runtime Cormo.Web will wire up ASP.NET plumbing for you with sensible defaults (e.g. OWIN, WebAPI, Dependency Injections) so no configuration or setup code needed.
Now with the still empty project, go ahead and write your first controller:
[RestController]
public class MyController
{
[Route("/hello")]
public string Hello() { return "Hello world"; }
}That was the only file you need to have in the project. Now build and run it on the browser.
Note that extending ApiController or IHttpController is optional. At runtime Cormo.Web will inject that for you (see: Mixins). This is to promote lightweight POCOs and dependency injection principles, keeping testability and sanity of your code. ApiController class carries heavy infrastructural baggage that goes agains the spirit of CDI, so we encourage to keep that away from your POCO and let Cormo.Web take care of wiring that up from behind the stage.
Dependency Injection is fully configured for you. No additional setup needed. Example:
[RestController]
public class MyController
{
[Inject] Greeter _greeter; // <- Here
[Route("/hello")]
public string Hello() { return _greeter.Greet("world"); }
}
public class Greeter
{
public string Greet(string name) { return "Hello " + name; }
}Of course you could also use interfaces but it's not required. You could also put [Inject] on constructors, fields, methods, and properties. Open generic types and constraints are also supported (TODO: example).
Read more about Dependency Injection.
TODO: explain [Inject], [Produce], [Qualifier], [PostConstruct], [PreDeploy]
Cormo modules (e.g. Cormo.Web in this case) configure your environment with sensible defaults. If you do however want to deviate from the provided default, you can always override it by declaring your own components. For instance, to override WebApi's HttpConfiguration settings:
public class WebApiConfiguration
{
[Produces] HttpConfiguration GetConfiguration()
{
return new HttpConfiguration( /* ... */ );
}
}Here you redefine your own complete HttpConfiguration as you wish. However, in many cases you're probably happy with the default values except a few things you want to tweak. In this case, rather than redefining the component you can simply modify the existing default.
[Configuration]
public class WebApiConfiguration
{
[Inject] void InitConfiguration(HttpConfiguration config)
{
// Do something with config
}
}See Dependency Injection intro for more.
Note: Marking your class with [Configuration] ensures your class gets executed before the application starts.