Created
May 21, 2012 17:36
-
-
Save AlecTaylor/2763468 to your computer and use it in GitHub Desktop.
Address complete OrmLite + ServiceStack example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using System.Data.SqlClient; | |
using ServiceStack.DataAnnotations; | |
using ServiceStack.OrmLite; | |
using ServiceStack.ServiceHost; | |
using ServiceStack.ServiceInterface; | |
using ServiceStack.ServiceInterface.ServiceModel; | |
namespace SomeLargerProject.ServiceInterface { | |
// Request DTO | |
[RestService("/addresses")] | |
public class Address { | |
public string Line1 { get; set; } | |
public string Line2 { get; set; } | |
public string PostCode { get; set; } | |
public string City { get; set; } | |
public string Country { get; set; } | |
} | |
// Response DTO | |
public class AddressResponse : IHasResponseStatus { | |
public List<Address> TableOfAddresses { get; set; } | |
} | |
// Can be called via any endpoint or format (json,xml,html,jsv,csv,soap), see: http://servicestack.net/ServiceStack.Hello/ | |
public class AddressService : ServiceBase<Address> { | |
public IDbConnectionFactory DbFactory { get; set; } | |
// Get's called by all HTTP Verbs (GET,POST,PUT,DELETE,etc) and endpoints JSON,XMl,JSV,etc | |
protected override object Run(Address request) { | |
return DbFactory.Exec(dbCmd => { | |
dbCmd.DropTable<Address>(); | |
dbCmd.CreateTable<Address>(overwrite: true); | |
var Addresses = new List<Address> { | |
new Address { | |
PostCode="2000", City="Sydney", Country="Australia", Line1="440 Phillip St", Line2="CBD" | |
}, | |
new Address { | |
PostCode="6008", City="Perth", Country="Australia", Line1="44 Hank St", Line2="Subiaco" | |
}, | |
new Address { | |
PostCode="2000", City="Sydney", Country="Australia", Line1="40 Phillip St", Line2="CBD" | |
}, | |
new Address { | |
PostCode="2000", City="Sydney", Country="Australia", Line1="340 Phillip St", Line2="CBD" | |
}, | |
new Address { | |
PostCode="7416", City="Melbourne", Country="Australia", Line1="440 Crescent St", Line2="South Melbourne" | |
}, | |
new Address { | |
PostCode="2000", City="Sydney", Country="Australia", Line1="454 Phillip St", Line2="CBD" | |
}, | |
new Address { | |
PostCode="2000", City="Sydney", Country="Australia", Line1="480 Phillip St", Line2="CBD" | |
}, | |
new Address { | |
PostCode="6160", City="Perth", Country="Australia", Line1="424 Trent St", Line2="Fremantle" | |
}, | |
new Address { | |
PostCode="6160", City="Perth", Country="Australia", Line1="44 Trent St", Line2="Fremantle" | |
}, | |
new Address { | |
PostCode="6160", City="Perth", Country="Australia", Line1="4234 Trent St", Line2="Fremantle" | |
} | |
}; | |
dbCmd.InsertAll(Addresses); | |
return new AddressResponse { | |
TableOfAddresses = dbCmd.Select<Address>() | |
}; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment