Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created April 6, 2011 09:47
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joeriks/905400 to your computer and use it in GitHub Desktop.
First try with NancyFx and SisoDb - form insert and list product names
using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using SisoDb;
/// <summary>
/// Requires the NuGet packages Nancy.Hosting.Aspnet and SisoDb
/// and a local Database in .\Sqlexpress called sisodemo
/// </summary>
///
public class NancyStart : Nancy.NancyModule
{
public class Product
{
public Guid SisoId { get; set; }
public string Name { get; set; }
public Product(string name)
{
this.Name = name;
}
}
public static SisoDb.ISisoDatabase db;
public static string InsertProduct(string name)
{
var retval = "";
using (var uow = db.CreateUnitOfWork())
{
var p = new Product(name);
uow.Insert(p);
uow.Commit();
retval = "<p>Insert success, new product <strong>" + p.Name + "</strong>, new id : " + p.SisoId.ToString() + "</p>";
}
return retval;
}
string htmlListProductNames()
{
IEnumerable<Product> products;
var retval = "";
using (var uow = db.CreateUnitOfWork())
{
products = uow.GetAll<Product>();
retval = "<ul>";
foreach (var p in products)
{
retval += "<li>" + p.Name + "</li>\n";
}
retval += "</ul>";
}
return retval;
}
string htmlForm = @"
<form method=""Post"">
<input type=""text"" name=""name"" id=""name""/>
<input type=""submit""/>
</form><br/>
";
public NancyStart()
{
if (db == null)
{
var cnInfo = new SisoConnectionInfo(
@"sisodb:provider=Sql2008||plain:Data source=.\sqlexpress;
Initial catalog=sisodemo;Integrated security=SSPI;");
db = new SisoDbFactory().CreateDatabase(cnInfo);
}
Get["/"] = x =>
{
return htmlForm + htmlListProductNames();
};
Post["/"] = x =>
{
var htmlResult = InsertProduct(Context.Request.Form["name"]);
return htmlResult + htmlForm + htmlListProductNames();
};
}
}
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpHandlers>
<add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/>
</httpHandlers>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/>
</handlers>
</system.webServer>
</configuration>
@joeriks
Copy link
Author

joeriks commented Apr 6, 2011

Besides adding the database to your .\Sqlexpress, here's a beginners video to show how to get the solution up and running in Visual Studio + getting the Nugets: Video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment