First try with NancyFx and SisoDb - form insert and list product names
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.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(); | |
}; | |
} | |
} |
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
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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