views:

36

answers:

1

I'm quite comfortable with VB and have a very large project i need to do. I've come across SubSonic and it looks awesome.

I am not clear if i can use it in VB. I've seen a a couple of posts that suggest they did but on the site it specifically shows C#.

Can I use SubSonic with VB.NET?

+3  A: 

SubSonic itself is entirely written in C#, but the code generation for your tables and views is also available for vb.net.

For SubSonic3 you will need to add the VB-Templates to your project

http://github.com/subsonic/SubSonic-3.0-Templates/tree/master//SubSonic.TemplatesVB/

For SubSonic2 you have to add a /lang vb parameter to subcommander (sonic.exe) However, I would personally stick with the C# code generation, since it is more tested because of the bigger user base, I suppose.

You can add another c# class library project to your solution (you should create a seperate project for your DAL anyways) and link that in you vb.net website project. I run this kind of setup, too.

There are 3 drawbacks in this scenario:

1) You can't create solutions with mixed programming language projects with the visual studio express editions.

2) if you wan't to extend the generated classes (with partial classes, not inheritance) you have to do that in c# since partial classes have to be in the same project.

3) If you change something to your c# project the design time errors are only shown after the next compile (e.g. if you change a column name that is used in your vb project and regenerate your DAL the error window only shows this error if you compile your solution/the c# project once.

At the end of the day I would encourage you to stick with c# since it is way more fun to code with subsonic (linq, lamdas, ...). But if you don't want to, you should be fine with vb.

Some examples, that are simplier to achive in c#

// in vb.net you have to add the _ modifier at the end of each line
// one thing that gets annoying for large linq queries or 
// when using subsonics query tool
var query = from p in products.All()
            join c in prodctcategories.All() on p.categoryId equals c.id
            where c.categoryName == "Food"
            select new {p.productName, c.categoryName}


// from the subsonic3 docs:
//find a single product by ID
var product = Product.SingleOrDefault(x => x.ProductID == 1);

//get a list of products based on some criteria
var products = Product.Find(x => x.ProductID <= 10);


// You can write lamdas in vb.net but they don't look as nice as in c#
Dim product = Product.SingleOrDefault(Function(x) x.ProductID = 1)
Dim products = Product.Find(Function(x) x.ProductID <= 10)

// If you have to write InlineQueries (subsonic's backdoor) you can do this
// with c#. For vb.net you would need a) internal backing fields for the
// poco class ProductResult and you would again need the concat 
// the query string with underscore, Environment.NewLine and &
public class ProductResult
{
    public int ProductId {get;set;}
    public string ProductName {get;set;}
}

public List<ProductResult> GetProducts()
{
    string query = @"SELECT p.productid, p.name as productname
     FROM product p
     INNER JOIN productcategories pc
     WHERE pc.categoryname = 'Food'
     ORDER BY p.productid ASC";

    var result = new CodingHorror(query).ExecuteTypedList<ProductResult>();
}
SchlaWiener
Thnaks for the response schlawiener
Dan