tags:

views:

66

answers:

4

I have a clumsy way to get the max length of a column in the database, but it's so bad that it seems wrong:

var length = new DataNS.WidgetTable(provider).description.MaxLength

Do I really have to instantiate some object and go digging through it? I even have to pass a provider to it explicitly.

If this really is the right way to do it, what's the best to get the current provider to pass to it? If it's not, what is the best way?

+1  A: 

Yes this does seem a bit clumsy but I can't see any less clumsy way of getting the MaxLength values. To get the current provider you can just use the GetProvider method of theSubSonic.DataProviders.ProviderFactory class. So your example would look something like:

var length = new DataNS.WidgetTable(ProviderFactory.GetProvider()).description.MaxLength;
Adam
A: 

In subsonic 2.2 I do the following. Does this not work in 3?

var length = Template.Schema.GetColumn("Name").MaxLength;

Mike Surel
+1  A: 

If you use ActiveRecord then your object has a [COLUMN_NAME]Column property which has all of this information. So if you have a Post object instantiated (like "post") you could use post.TitleColumn.MaxLength.

If you're using SubSonic 3 you can head in to Structs.tt and spin up whatever you need as well.

Rob Conery
A: 

I got something like this to work in SubSonic 3 using ActiveRecord, it works:

new AccountsTable(null).ContactEmail.MaxLength

I'm using it in an ASP.NET MVC view:

<%= Html.TextBoxFor(model => model.ContactEmail,
new { maxlength = new AccountsTable(null).ContactEmail.MaxLength  })%>

But not sure if passing null in the AccountsTable constructor is going to trip things up at some point -- does anyone know if this is NOT advisable? I tried to find another way, as concise as possible, but this is where I ended up.

djuth