I'm trying out the new SubSonic 3 preview, but I'm not sure about the patterns I should be using for the basic CRUD operations in my MVC project.
I'm trying to keep as much of my data logic in my models, so I added some static CRUD methods to each model's partial class.
For example, let's say I have a configuration table, which should only have a single record. So, my partial class may look something like this:
public partial class Configuration{
// cut down; mocking hooks here IRL
private static IRepository<Configuration> Table = new MyRepository<Configuration>();
public static Configuration Retrieve()
{
var config = Table.GetAll().FirstOrDefault();
if (config == null)
{
config = new Configuration();
Table.Add(config);
}
return config;
}
public static void Update(Configuration target)
{
Table.Update(target);
}
}
Currently, this doesn't work as the config table has an identity column for a primary key, and Add-ing a new record to the table throws the standard "Cannot insert explicit value for identity column" error. SubSonic 3 doesn't seem to generate classes that, upon new-ing them up, play nice with the rules of the database schema (i.e., no default values, no nullable primitives for values that are nullable in the database, etc).
Now, I can alter my table and pattern to get around these issues, but I'm wondering about when I cannot get around this issue--when I have to add a new record to the database and have an identity as my primary key.
I'm also wondering if this pattern is even correct or not. SubSonic gives you a number of different ways you can go about your repository business, so I'm not sure which one I should be using. I'd LIKE to use my models as MUCH as possible (otherwise why not just Linq to Sql?), so I don't want to use SubSonic's query building goodness when trying to CRUD my models.
What should I do here? Any advice on CRUD patterns for using SubSonic 3 in my MVC project would be welcome and +'d. Also welcome are links to websites that cover this subject for SubSonic 3 but that don't rank high in Google searches...
Asked Rob directly (link here). For my DB, at least, there's a showstopper bug in the generated code. Aaah, alpha software.
UPDATE
With the release of Subsonic3, can we have a little bump to reconsider this question?