R – How should I create the subsonic 3.0 DB context class

recursionsubsonic

I'm new to SubSonic (of all flavours), but thought I might as well start with 3.0, because I'd like to use Linq, and I get the impression 3.0 is not that far away from release.

I tried the alpha download .zip, but that seems pretty old and didn't singularize table class names, so I'm now running from the latest trunk SVN version (rev62).

I've run the 'simple' templates, from SubSonic.Templates\Simple against my database and everything seems ok, but the DB context class which the templates create starts like this:

public partial class DB : IQuerySurface
{
    static DB _db;

    public DB() { 
        _db = new DB();
    }

    public static DB CreateDB()
    {
        if (_db == null)
        {
            _db = new DB();
            _db.Init();
        }
        return _db;
    }
    ... etc

Unsurprisingly, when I call DB.CreateDB, the ctor recurses endlessly and crashes everything with a stack overflow.

I don't really understand the ctor at all – it doesn't look like it should be there, but both the 'simple' and the 'advanced' templates create something similar, and there's a humongous test suite which I imagine is verifying this somehow.

Clearly I have the wrong end of the stick here – what blindingly obvious point have I missed?

Update: The simple and advanced templates aren't similar, and the advanced ones don't have this problem. Thanks for the help.

Another Update: It looks like this is fixed in the simple templates in SVN r66

Best Answer

Don't know if you have the latest bits from SVN with a bug, but my version from a few days ago appears to be working fine. Here is what my DB class starts off with:

public partial class DB : IQuerySurface
{
    BatchQuery _batch = null;

    public IDataProvider DataProvider;
    public DbQueryProvider provider;

    private IDatabaseSchema _schema;
    public IDatabaseSchema Schema
    {
        get
        {
          return _schema;
        }
    }

    public DB() 
    { 
        DataProvider = ProviderFactory.GetProvider("Northwind");
        Init();

    }

    public DB(string instanceName, string connectStr)
    {
        SubSonic.DataProviders.ConnectionStringProvider.Instance.AddLocalConnectionString(
              instanceName, connectStr, "System.Data.SqlClient");

        DataProvider = ProviderFactory.GetProvider(instanceName);

        Init();

    }

... etc...

I used the advanced version of the templates.