public
Last active

1 class in ServiceStack

  • Download Gist
AuthorService.cs
C#
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
/*
With no other C# or config other than OrmLite DB config in AppHost below - this web service provides all the screenshots attached, out-of-the-box, for free.
 
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("AppDb"), //ConnectionString in Web.Config
SqlServerOrmLiteDialectProvider.Instance) {
ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current) });
 
 
-- Code-first Simplicity at Great Speed - http://www.servicestack.net/benchmarks/
 
*/
 
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using ServiceStack.ServiceInterface.ServiceModel;
 
namespace MyWebServices
{
//Request DTO
[RestService("/authors")]
public class Author
{
[AutoIncrement]
[Alias("AuthorID")]
public int Id { get; set; }
[Index(Unique = true)]
public string Name { get; set; }
public DateTime Birthday { get; set; }
public DateTime? LastActivity { get; set; }
public Decimal? Earnings { get; set; }
public bool Active { get; set; }
[Alias("JobCity")]
public string City { get; set; }
public string Comments { get; set; }
public Int16 Rate { get; set; }
}
 
//Response DTO
public class AuthorResponse : IHasResponseStatus
{
public List<Author> Results1 { get; set; }
public List<Author> Results2 { get; set; }
public List<Author> Results3 { get; set; }
public List<Author> Results4 { get; set; }
public List<Author> Results5 { get; set; }
public List<Author> Results6 { get; set; }
public List<Author> Results7 { get; set; }
public List<Author> Results8 { get; set; }
public ResponseStatus ResponseStatus { get; set; } //Where Exceptions get auto-serialized
}
 
//Can be called via any endpoint or format, see: http://servicestack.net/ServiceStack.Hello/
public class AuthorService : ServiceBase<Author>
{
public IDbConnectionFactory DbFactory { get; set; } //Injected by IOC
 
//Get's called by all HTTP Verbs (GET,POST,PUT,DELETE,etc) and endpoints (json,xml,html,jsv,csv,soap)
protected override object Run(Author request)
{
return DbFactory.Exec(dbCmd =>
{
dbCmd.CreateTable<Author>(overwrite: true); //Drops and re-creates the RDBMS Author table using the C# POCO for the schema
 
var authors = new List<Author> {
new Author { Name = "Demis Bellot", Birthday = DateTime.Today.AddYears(-20), Active = true, Earnings = 99.9m, Comments = "CSharp books", Rate = 10, City = "London" },
new Author { Name = "Angel Colmenares", Birthday = DateTime.Today.AddYears(-25), Active = true, Earnings = 50.0m, Comments = "CSharp books", Rate = 5, City = "Bogota" },
new Author { Name = "Adam Witco", Birthday = DateTime.Today.AddYears(-20), Active = true, Earnings = 80.0m, Comments = "Math Books", Rate = 9, City = "London" },
new Author { Name = "Claudia Espinel", Birthday = DateTime.Today.AddYears(-23), Active = true, Earnings = 60.0m, Comments = "Cooking books", Rate = 10, City = "Bogota" },
new Author { Name = "Libardo Pajaro", Birthday = DateTime.Today.AddYears(-25), Active = true, Earnings = 80.0m, Comments = "CSharp books", Rate = 9, City = "Bogota" },
new Author { Name = "Jorge Garzon", Birthday = DateTime.Today.AddYears(-28), Active = true, Earnings = 70.0m, Comments = "CSharp books", Rate = 9, City = "Bogota" },
new Author { Name = "Alejandro Isaza", Birthday = DateTime.Today.AddYears(-20), Active = true, Earnings = 70.0m, Comments = "Java books", Rate = 0, City = "Bogota" },
new Author { Name = "Wilmer Agamez", Birthday = DateTime.Today.AddYears(-20), Active = true, Earnings = 30.0m, Comments = "Java books", Rate = 0, City = "Cartagena" },
new Author { Name = "Rodger Contreras", Birthday = DateTime.Today.AddYears(-25), Active = true, Earnings = 90.0m, Comments = "CSharp books", Rate = 8, City = "Cartagena" },
new Author { Name = "Chuck Benedict", Birthday = DateTime.Today.AddYears(-22), Active = true, Earnings = 85.5m, Comments = "CSharp books", Rate = 8, City = "London" },
new Author { Name = "James Benedict II", Birthday = DateTime.Today.AddYears(-22), Active = true, Earnings = 85.5m, Comments = "Java books", Rate = 5, City = "Berlin" },
new Author { Name = "Ethan Brown", Birthday = DateTime.Today.AddYears(-20), Active = true, Earnings = 45.0m, Comments = "CSharp books", Rate = 5, City = "Madrid" },
new Author { Name = "Xavi Garzon", Birthday = DateTime.Today.AddYears(-22), Active = true, Earnings = 75.0m, Comments = "CSharp books", Rate = 9, City = "Madrid" },
new Author { Name = "Luis garzon", Birthday = DateTime.Today.AddYears(-22), Active = true, Earnings = 85.0m, Comments = "CSharp books", Rate = 10, City = "Mexico" },
};
dbCmd.InsertAll(authors);
 
var agesAgo = DateTime.Today.AddYears(-20).Year;
return new AuthorResponse {
Results1 = dbCmd.Select<Author>(q => q.Birthday >= new DateTime(agesAgo, 1, 1) && q.Birthday <= new DateTime(agesAgo, 12, 31)),
Results2 = dbCmd.Select<Author>(q => Sql.In(q.City, "London", "Madrid", "Berlin")),
Results3 = dbCmd.Select<Author>(q => q.Name.StartsWith("A")),
Results4 = dbCmd.Select<Author>(q => q.Name.EndsWith("garzon")),
Results5 = dbCmd.Select<Author>(q => q.Name.ToUpper().EndsWith("GARZON")),
Results6 = dbCmd.Select<Author>(q => q.Name.Contains("Benedict")),
Results7 = dbCmd.Select<Author>(q => q.Eaqings <= 50),
Results8 = dbCmd.Select<Author>(q => q.Rate == 10 && q.City == "Mexico"),
};
});
 
}
}
}
output.md
Markdown

Metadata pages for all registered formats

Metadata pages

With links to WSDLs and XSDs and C# client examples.

Human friendly HTML report view

HTML Format

Single self-contained html page snapshot (i.e. no external refs). Includes embedded JSON web service response - allows programmatic access to data snapshots.

Built-in Mini Profiler

MVC Mini Profiler

Includes a port of the excellent MVC Mini Profiler: http://code.google.com/p/mvc-mini-profiler/

Sql Profiling

MVC Mini Profiler

Enabled via ProfiledDbConnection() above. Same IDbConnectionFactory is used by both Dapper and OrmLite as both Micro ORMs are just extension methods over ADO.NET's underlying System.Data.x interfaces.

JSON

JSON Format

JSONP is automatically available with ?callback=cb

XML

XML Format

Can request different formats with ?format=type query string or Accept: Content-Type HTTP Header.

And more...

  • JSV, CSV, SOAP formats / endpoints.
  • Strong-typed API (without Code-gen) in MonoTouch / Silverlight .NET/Mono using Generic C# Service Clients for JSON, XML and JSV formats
  • InMemory / Distributed Session / Caching
  • Fluent Validation / Automated Exception handling
  • Registration & Authentication: Basic / Credentials / OAuth Twitter + Facebook
  • Request / Response filters
  • see http://www.servicestack.net for more...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.