One possible way to compose application using OWIN spec

When composing your server apps with OWIN there are many options. OWIN spec is very flexible and it doesn’t  force you into particular application architecture (this is a good thing). So, using standard OWIN middleware components to compose your application is pretty easy and straightforward.

I prefer to clearly (at the assembly level) separate middleware from the server and the application. That makes it easier to work with for a bigger team, and when potentially, application teams are not the same as server and/or middleware team. Organizing your application in this way could look something like this, starting with middleware components project:

namespace Simplr.Middleware {
public class ResourceResolver : MiddlewareBase {
public override Task Process(IContext context) {...}
}
public class HypermediaValidator : MiddlewareBase {
public override Task Process(IContext context) {...}
}
//...
//additional middleware components
}

and then application is configured and ran like this:

using Simplr.Middleware;
namespace App.Runner {
internal static class Program {
static void Main() {
var _pipeline = new Pipeline()
.Use(new ResourceResolver())
.Use(new HypermediaValidator())
.Use(new ApiCall())
.Use(new HypermediaUpdater())
.Use(new HALSerializer());
var host = new Host(pipeline);
host.AddServer("RequestResponse", new NowinServer(8080));
host.Run();
Console.WriteLine("Listening on port 8080. Enter to exit.");
Console.ReadLine();
host.Stop();
}
}
}

So, having this in place, application developers – potentially different team(s) then middleware team – would write:

using SimplR.Resources;
using SimplR.Hypermedia;
using SimplR.Services;
using App.Domain.Users;
namespace App.Resources.Users {
public class UsersResource : Resource<User> {
public override List<User> Get() {...}
public override User Get(int id) {...}
public override User Post(User user) {...}
public override User Put(User user) {...}
public override void Delete(int id) {...}
}
//...
// additional resources
//..
}
view raw owin.simplr.app.r.cs hosted with ❤ by GitHub

which models application interface as resources, and exposes them via standard HTTP verbs…

If you prefer CQRS – you can have middleware dispatch to handlers and then write your applications something like this:

using SimplR.Resources;
using SimplR.Hypermedia;
using SimplR.Services;
using App.Domain.Users;
namespace App.Handlers.Users {
public class UsersHandler : IHandler<User> {
public List<User> Query(Expression expr) {...}
public void CreateCommand(User user) {...}
public void UpdateCommand(User user) {...}
public void DeleteCommand(User user) {...}
}
//...
}

Bottom line, OWIN is awesome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: