SubSonic 3.0 SimpleRepository = sweetness
September 14th, 2009
MVC ASP.NET is great. Add SQLite and you get easy deployment for a small site. Add SubSonic 3.0 SimpleRepository and you get sweetness.
The kind of sweetness that you get when you need to crank out a site quickly and with least amount of configuration hassle on the web host (I’m looking at you, SQL Server) and you find the tools to make everything just fall in place so you can concentrate on business logic. The kind of sweetness that makes updating schema on servers that don’t give you access to the SQL Server via control panel a breeze! The kind of sweetness… it’s pretty cool, is all I’m saying…
It all boils down to the following code:
var repo = new SimpleRepository("DBConnString", SimpleRepositoryOptions.RunMigrations);
What this does is, every time you use this repository and your DB doesn’t have a table or properties of the class you are trying to persist, it will create them on the fly! So you can create a blank database, SimpleRepository object and go to town:
Order order = new Order(); order.LastName = collection["LastName"]; order.FirstName = collection["FirstName"]; order.DateEntered = DateTime.Now; repo.Add(order);
That’s it! SubSonic will create an Order table with LastName, FirstName, DateEntered properties and add a new order.
Now, obviously we do need to tell SubSonic something about our class… So, here’s what my Order class looks like:
public class Order
{
[SubSonicPrimaryKey]
public Int64 OrderID { get; set; }
public DateTime? DateEntered { get; set; }
[SubSonicNullString]
public string FirstName { get; set; }
[SubSonicNullString]
public string LastName { get; set; }
}
As you can see, I just told SubSonic which item is the primary key (Int64 being a requirement for SQLite 3.0 compatibility) and that I want my strings to be nullable. Note, though, that it will actually figure out that my DateTime will be nullable as well — all by itself.
So, now I want to retrieve something. SubSonic’s Linq provider makes that a snap also:
var ordersPerUser = repo.Find(x => x.LastName == "Lastname");
Anyway, check out the new version of SubSonic, it’s awesome.
Entry Filed under: Uncategorized


