nancy & simple.data from prognet 11
DESCRIPTION
These are the slides from the tutorial I gave at Skills Matter's Progressive .NET Tutorials 2011.Feel free to use content from these slides in your own presentations, but please mention the source in your talk. :)TRANSCRIPT
Nancy & Simple.Data
The Super-Duper-Happy Path
Nancy• Inspired by Sinatra
o www.sinatrarb.com
• Low ceremony• High productivity
Hello NancyDemo
namespace NancyDemo{ public class MainModule : Nancy.NancyModule { public MainModule() { Get["/hi"] = _ => "Hello World!"; } }}
Simple.Data• Inspired by DataMapper & ActiveRecord
o datamapper.orgo rubyonrails.org
• Dynamic• Flexible
Hello Simple.DataDemo
namespace Hello{ class Program { static void Main() { var db = Simple.Data.Database.Open(); var hello = db.Hello.FindById(1); Console.WriteLine("{0}, {1}!", hello.Greeting, hello.Subject); } }}
Nancy, meetSimple.Data
Simple.Data, meet Nancy
public class MainModule : NancyModule{ private readonly dynamic _db = Database.OpenNamedConnection("ProgNet");
public MainModule() { Get["/"] = _ => { var scrawls = _db.Graffiti.All().OrderByAddedDescending(); return View["index", scrawls]; };
Post["/"] = _ => { _db.Graffiti.Insert(Text: Request.Form.Text); return new RedirectResponse("/"); }; }}
Authentication• Nancy.Authentication.Basic
o Basic HTTP authentication
• Nancy.Authentication.Formso Proper authenticationo Also works as basis for Social login (e.g. Facebook, Twitter)
public class MyBootstrapper : DefaultNancyBootstrapper{ protected override void InitialiseInternal(TinyIoC.TinyIoCContainer container) { base.InitialiseInternal(container);
FormsAuthentication.Enable( this, new FormsAuthenticationConfiguration { RedirectUrl = "~/login", UsernameMapper = container.Resolve<IUsernameMapper>() }); }}
public class MyModule : NancyModule{ public MyModule() : base("/secure") { this.RequiresAuthentication();
Get["/"] = _ => "Secure!"; }}
Exercise• A Super-Duper-Happy ToDo app• Download: SQL CE DB and EncryptionHelper:
o http://bit.ly/ProgNet-NSD
• Database schema:
• Users • ToDos
• Id: Guid (PK)• Email: nvarchar(128)• EncryptedPassword:
binary(32)
• Salt] binary(32)
• Id Guid (PK)• UserId Guid (FK)• Text nvarchar(1024)• Added] datetime
DEFAULT GETDATE()• Done datetime