As there is obviously still some uncertainty if Delphi Prism is really a fully fledged .NET development environment, I’m posting an example below to prove that Prism does allow you to LINQ to SQL:

This is a “Customer” class definition with a manual mapping to a “Customers” table on a SQL Server 2005:


type
[Table(Name := 'Customers')]
Customer = class(Object)
private
protected
public
[Column(IsPrimaryKey := true)]
property CustNo:Double;
[Column]
property Name: String;
[Column]
property Country: String;
end;

This is the actual LINQ to SQL code that relies on the class definition above:


method MainForm.button2_Click(sender: System.Object; e: System.EventArgs);
var
LCustomers: Table<Customer>;
LDBMain: DataContext;
LConnectionString:String;
begin
LConnectionString := 'Data Source=.\SQLEXPRESS;Initial Catalog=DBDEMOS;Integrated Security=True';
LDBMain := new DataContext(LConnectionString);
LCustomers := LDBMain.GetTable<Customer>();
var USCustomers := from Customer in LCustomers where Customer.Country = 'US' select Customer;

for each Customer in USCustomers do begin
listBox1.Items.Add(Customer.Name);
end;
end;

Interesting to note is that Prism has a very nice name scope mechanism. Even though “Customer” is a Class name, it is also used as local variable in the LINQ and FOR constructs. I am not saying that this is a good practice though ;-)

Technically, both “Customer” variables are different variables – which is quite important to understand.

The full demo source can be downloaded here: [download#2].

  • Vegar Vikan

    '…class definition with a manual mapping…', so the visual tools and code-generation that you get in c# is not there?

    Sorry to bother you, but I have tried once and failed (using oxygen a couple of months back) :-(

    -Vegar

  • brad

    Cool!
    But, I wouldnt recommend using it…
    google for “linq to sql is dead”
    http://codebetter.com/blogs/david.hayden/archiv…

  • http://www.monien.net/blog Olaf Monien

    Well, google for “steve jobs dead” or maybe even “Delphi is dead”. It appears both searches return even more hits…

  • Xepol

    In fairness, I would not call this “delphi”, but “Prism” or “Delphi Prism” if you prefer. Delphi strongly implies win32, and that just isn't the case. Prism is actually a totally seperate product from a totally seperate vendor that is currently being licensed and sold by CodeGear.

    I am not trying to take away from the shine of what RemObjects has created, it truely is impressive that RO danced circles around CodeGear to the point where CodeGear has admitted they can't do it better (or even come close) – however, it is misleading to call it “Delphi” with no qualifiers.

    After all the product that is actually called Delphi can't do Linq at all – it doesn't even come close yet (which isn't to say I don't hope Embarcadero buys RemObjects, combines the teams, puts Marc in charge of the entire Delphi line and brings some of that vision and greatness back to Delphi – because trust me, I do. I hope deeply for it.)

    I also which CodeGear would stop the marketing stupidity whereby they dilute the Delphi name just because something has a visual designer. After all, by CG's marketing standards, Microsoft's C# and VB.Net could also be labelled “Delphi” – not something that is going to help those of us who use “Delphi” Delphi in the win32 world.

  • http://www.monien.net/blog Olaf Monien

    The class designer is there, but there is no Pascal code gen. According to marc hoffman that is currently not on their list.
    For now you have to live with manual mapping. I guess, if you had Visual Studio (not just the VS shell), that you could add a C# library project to your solution, reference that from your Prism project. Then create the Table-Class mapping in the C# project using the visual designer.

    Maybe somewhat ugly, but possibly the key to get the Designer + CodeGen integrated into Prism. Who cares what language is used for the mapping …

  • http://hadihariri.com Hadi Hariri

    Actually no, this is slightly different.

  • Alisha D Herron

    nice article! nice site. you're in my rss feed now ;-)
    keep it up

  • http://www.monien.net/blog Olaf Monien

    A new article about LINQ to SQL is in the works – about something new
    and cool (I believe) :-)

    Regards,
    Olaf Monien
    —————————
    CodeGear Technology Partner
    http://www.monien.net/blog

  • Vagnik85

    After some problems that I’ve had with some exceptions concerning invalid castings while implementing the above code, I finally made the following changes in order to be ok:

    - In order to read the results of the LINQ to SQL query I used:

    for each Customer in IEnumerable(USCustomers) do begin
    …..
    end;

    Also you should add System.Collections.Generic in the uses of the .pas file. In this way IEnumerable identifier will be known.

blog comments powered by Disqus
CodeGear Technology Partner