Skip to content

Instantly share code, notes, and snippets.

@mythz
Created February 10, 2012 07:36
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mythz/1787443 to your computer and use it in GitHub Desktop.
Save mythz/1787443 to your computer and use it in GitHub Desktop.
1 class in ServiceStack
/*
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"),
};
});
}
}
}

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...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment