-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathAppHost.cs
More file actions
109 lines (97 loc) · 3.48 KB
/
AppHost.cs
File metadata and controls
109 lines (97 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using Funq;
using ServiceStack.Configuration;
using ServiceStack.Redis;
using ServiceStack.ServiceInterface;
using ServiceStack.Common.Web;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;
namespace Backbone.Todos
{
/// <summary>
/// Define your ServiceStack web service request (i.e. Request DTO).
/// </summary>
public class Todo
{
public long Id { get; set; }
public string Content { get; set; }
public int Order { get; set; }
public bool Done { get; set; }
}
/// <summary>
/// Create your ServiceStack rest-ful web service implementation.
/// </summary>
public class TodoService : ServiceStack.ServiceInterface.Service
{
/// <summary>
/// Gets or sets the Redis Manager. The built-in IoC used with ServiceStack autowires this property.
/// </summary>
public IRedisClientsManager RedisManager { get; set; }
public object Get(Todo todo)
{
//Return a single Todo if the id is provided.
if (todo.Id != default(long))
{
return RedisManager.ExecAs<Todo>(r => r.GetById(todo.Id));
}
//Return all Todos items.
return RedisManager.ExecAs<Todo>(r => r.GetAll());
}
/// <summary>
/// Handles creating and updating the Todo items.
/// </summary>
/// <param name="todo">The todo.</param>
/// <returns></returns>
public Todo Post(Todo todo)
{
RedisManager.ExecAs<Todo>(r =>
{
//Get next id for new todo
if (todo.Id == default(long)) todo.Id = r.GetNextSequence();
r.Store(todo);
});
return todo;
}
/// <summary>
/// Handles creating and updating the Todo items.
/// </summary>
/// <param name="todo">The todo.</param>
/// <returns></returns>
public Todo Put(Todo todo)
{
return Post(todo);
}
public object Delete(Todo todo)
{
RedisManager.ExecAs<Todo>(r => r.DeleteById(todo.Id));
return null;
}
}
/// <summary>
/// Create your ServiceStack web service application with a singleton AppHost.
/// </summary>
public class ToDoAppHost : AppHostHttpListenerBase
{
/// <summary>
/// Initializes a new instance of your ServiceStack application, with the specified name and assembly containing the services.
/// </summary>
public ToDoAppHost() : base("Backbone.js TODO", typeof(TodoService).Assembly) { }
/// <summary>
/// Configure the container with the necessary routes for your ServiceStack application.
/// </summary>
/// <param name="container">The built-in IoC used with ServiceStack.</param>
public override void Configure(Container container)
{
//Configure ServiceStack Json web services to return idiomatic Json camelCase properties.
JsConfig.EmitCamelCaseNames = true;
JsConfig.TreatEnumAsInteger = true;
//Register Redis factory in Funq IoC.
var settings = new AppSettings();
container.Register<IRedisClientsManager>(new BasicRedisClientManager(settings.GetString("Redis.HostPort")));
//Register user-defined REST Paths
Routes
.Add<Todo>("/todos")
.Add<Todo>("/todos/{Id}");
}
}
}