简体   繁体   English

在vb.net上使用subsonic

[英]using subsonic with vb.net

I'm quite comfortable with VB and have a very large project i need to do. 我对VB非常满意,并且有一个很大的项目需要做。 I've come across SubSonic and it looks awesome. 我遇到过SubSonic,它看起来很棒。

I am not clear if i can use it in VB. 我不清楚我是否可以在VB中使用它。 I've seen aa couple of posts that suggest they did but on the site it specifically shows C#. 我看过几篇文章暗示他们这样做了,但是在网站上专门显示了C#。

Can I use SubSonic with VB.NET? 我可以在VB.NET中使用SubSonic吗?

SubSonic itself is entirely written in C#, but the code generation for your tables and views is also available for vb.net. SubSonic本身完全用C#编写,但表和视图的代码生成也可用于vb.net。

For SubSonic3 you will need to add the VB-Templates to your project 对于SubSonic3,您需要将VB模板添加到您的项目中

http://github.com/subsonic/SubSonic-3.0-Templates/tree/master//SubSonic.TemplatesVB/ 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. 对于SubSonic2,您必须在子命令程序(sonic.exe)中添加/lang vb参数。但是,我个人会坚持使用C#代码生成,因为由于用户群较大,因此它经过了更多的测试。

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. 您可以在解决方案中添加另一个c#类库项目(无论如何,您都应该为DAL创建一个单独的项目),并将其链接到您的vb.net网站项目中。 I run this kind of setup, too. 我也运行这种设置。

There are 3 drawbacks in this scenario: 在这种情况下有3个缺点:

1) You can't create solutions with mixed programming language projects with the visual studio express editions. 1)您无法使用Visual Studio Express版本的混合编程语言项目创建解决方案。

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. 2)如果您不想扩展生成的类(使用部分类,而不继承),则必须在c#中执行此操作,因为部分类必须位于同一项目中。

3) If you change something to your c# project the design time errors are only shown after the next compile (eg 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. 3)如果对c#项目进行了更改,则仅在下一次编译后才会显示设计时错误(例如,如果更改了vb项目中使用的列名并重新生成DAL,则错误窗口仅在编译时显示此错误您的解决方案/ C#项目一次。

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, ...). 归根结底,我鼓励您继续使用c#,因为使用subsonic(linq,lamdas等)进行编码会更有趣。 But if you don't want to, you should be fine with vb. 但是,如果您不想这样做,可以使用vb很好。

Some examples, that are simplier to achive in c# 一些示例,在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>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM