ASP.NET Hosting

Archives

Archives / 2006 / June
  • Tools galore

    How many tools do you think we can use for .NET development?
    Well, more than 905 tools (including 292 libraries) according to what I have referenced in SharpToolbox so far! How many do you actually know? ;-)
    I started collecting this information more than three years ago now... It's been a while since I've taken a look at the counters.

    If I remember well, the latest categories that I've added are Grid computing, Workflow, Rich-client UI. The categories with the most tools are IDE - IDE-addins, Persistence - Data-tier, Reporting, Object-relational mapping, and ASP.NET.

    JavaToolbox is in good shape too with 558 tools including 196 libraries!

    Searching for a tool? Keep digging, I have most of them in store :-)

  • Linq to Amazon implementation fore steps

    On Monday, I have announced an implementation of Linq for Amazon Web Services, that allows to query for books using the following syntax:

    var query =
      from book in new Amazon.BookSearch()
      where
        book.Title.Contains("ajax") &&
        (book.Publisher == "Manning") &&
        (book.Price <= 25) &&
        (book.Condition == BookCondition.New)
      select book;


    Before getting to the details of the implementation code, I'd like to describe what we need to do in order to be able to use such code.
    First, as you can see we work with book objects. So, let's defined a Book class:

    public class Book
    {
      public IList<String>  Authors;
      public BookCondition  Condition;
      public String         Publisher;
      public Decimal        Price;
      public String         Title;
      public UInt32         Year;
    }

    Here I use public fields for the sake of simplicity, but properties and private fields would be better.
    You can see that this class defines the members we use in our query: Title, Publisher, Price and Condition, as well as others we'll use later for display. Condition is of type BookCondition, which is just an enumeration defined like this:

    public enum BookCondition {All, New, Used, Refurbished, Collectible}

    The next and main thing we have to do is define this BookSearch class we use to perform the query. This class implements System.Query.IQueryable<T> to receive and process query expressions. IQueryable<T> is defined like this:

    interface IQueryable<T> : IEnumerable<T>, IQueryable

    which means that we have to implement the members of the following interfaces:

    interface IEnumerable<T> : IEnumerable
    {
      IEnumerator<T> GetEnumerator();
    }

    interface IEnumerable
    {
      IEnumerator GetEnumerator();
    }

    interface IQueryable : IEnumerable
    {
      Type ElementType { get; }
      Expression Expression { get; }

      IQueryable CreateQuery(Expression expression);
      object Execute(Expression expression);
    }

    and finally the IQueryable<T> interface itself of course:

    interface IQueryable<T> : IEnumerable<T>, IQueryable, IEnumerable
    {
      IQueryable<S> CreateQuery<S>(Expression expression);
      S Execute<S>(Expression expression);
    }


    It may look like a lot of work... I will try to describe it simply so that you can create your own implementation of IQueryable without too much difficulty once you get to know how the mechanics work.

    In order to be able to implement IQueryable, you need to understand what happens behind the scenes. The from..where..select query expression you write in your code is just syntactic sugar that the compiler converts quietly into something else! In fact, when you write:

    var query =
      from book in new Amazon.BookSearch()
      where
        book.Title.Contains("ajax") &&
        (book.Publisher == "Manning") &&
        (book.Price <= 25) &&
        (book.Condition == BookCondition.New)
      select book;


    the compiler translates this into:

    IQueryable<Book> query = Queryable.Where<Book>(new BookSearch(), <expression tree>);

    Queryable .Where is a static method that takes as arguments an IQueryable followed by an expression tree.
    I can hear you crying out loud: "What the hell is an expression tree?!".
    Well, an expression tree is just a way to describe what you wrote after where as data instead of code. And what's the point?

    1. To defer the execution of the query
    2. To be able to analyze the query to do whatever we want in response
    In our case, we will go to the web and ask Amazon to return some XML data about books, but we could also translate the query into SQL and execute it against a database (this is what Linq to SQL does!), or do anything else you'd consider useful.

    And why is it called a "tree"? Because it's a hierarchy of expressions. Here is the complete expression tree in our case:

    Expression.Lambda<Func<Book, Boolean>>(
      Expression.AndAlso(
        Expression.AndAlso(
          Expression.AndAlso(
            Expression.CallVirtual(
              typeof(String).GetMethod("Contains"),
              Expression.Field(book, typeof(Book).GetField("Title")),
              new Expression[] { Expression.Constant("ajax") }),
            Expression.Call(
              typeof(String).GetMethod("op_Equality"),
              null,
              new Expression[] {
                Expression.Field(book, typeof(Book).GetField("Publisher")),
                Expression.Constant("Manning") })),
          Expression.Call(
            typeof(Decimal).GetMethod("op_LessThanOrEqual"),
            null,
            new Expression[] {
              Expression.Field(book, typeof(Book).GetField("Price")),
              Expression.Constant(new Decimal(25), typeof(Decimal)) })),
        Expression.EQ(
          Expression.Convert(
            Expression.Field(book, typeof(Book).GetField("Condition")),
            typeof(BookCondition)),
          Expression.Constant(BookCondition.New))),
      new ParameterExpression[] { book }));


    If you look at this tree, you should be able to locate the criteria we have specified in our query. What we will do in the next step is see how all this combines and how we will extract the information from the expression tree to be able to construct a web query to Amazon.
    We'll keep that for another post... Stay tuned!

    Cross-posted from http://linqinaction.net

  • Introducing Linq to Amazon

    As an example that will be included in the Linq in Action book, I've created an example that shows how Linq can be extended to query anything.
    This example introduces Linq to Amazon, which allows querying Amazon for books using Linq! It uses Linq's extensibility to allow for language-integrated queries against a book catalog. The Linq query gets converted to REST URLs supported by Amazon's web services. These services return XML. The results are converted from XML to .NET objects using Linq to XML.

    For the moment, let's look at the client code:

    var query =
      from book in new Amazon.BookSearch()
      where
        book.Title.Contains("ajax") &&
        (book.Publisher == "Manning") &&
        (book.Price <= 25) &&
        (book.Condition == BookCondition.New)
      select book;


    I think this code speaks for itself! This is Linq to Amazon code. It expresses a query against Amazon, but does not execute it... The query will be executed when we start enumerating the results.

    The following piece of code converts from Linq to Amazon to Linq to Objects:

    var sequence = query.ToSequence();

    The query gets executed this time and an enumeration of the resulting books is created.
    The next steps could be to use Linq to Objects to perform grouping operations on the results. e.g.:

    var groups =
      from book in query.ToSequence()
      group book by book.Year into years
      orderby years.Key descending
      select new {
        Year = years.Key,
        Books =
          from book in years
          select new { book.Title, book.Authors }
      };


    This allows to display the results like this:

    Published in 2006
      Title=Ruby for Rails : Ruby Techniques for Rails Developers    Authors=...
      Title=Wxpython in Action    Authors=...

    Published in 2005
      Title=Ajax in Action    Authors=...
      Title=Spring in Action (In Action series)    Authors=...

    Published in 2004
      Title=Hibernate in Action (In Action series)    Authors=...
      Title=Lucene in Action (In Action series)    Authors=...

    using the following code:

    foreach (var group in groups)
    {
      Console.WriteLine("Published in "+group.Year);
      foreach (var book in group.Books)
      {
        Console.Write("  ");
        ObjectDumper.Write(book);
      }
      Console.WriteLine();
    }


    What a great way to query a catalog of books! Don't you think that this code is very legible and clearly expresses the intention?
    Linq to XML can also be used to display the results as XHTML, and Linq to SQL can be used to persist the results in a database.

    Over the coming days, I'll publish more details and the complete source code for this example.
    Update: The next part of this series is available: Linq to Amazon implementation fore steps

    Cross-posted from http://linqinaction.net

  • Linq rebranding

    Soma announced that the naming scheme for the Linq technologies has been simplified:

    LINQ to ADO.NET includes:
          LINQ to DataSet
          LINQ to Entities
          LINQ to SQL (formerly DLinq)

    LINQ support for other data types includes:
          LINQ to XML (formerly XLinq)
          LINQ to Objects
    So long DLinq and XLinq... Welcome  Linq to Whatever!
    I agree that this will make things a bit more explicit, especially the separation between what is related to ADO.NET and what is not. It will also help implementors find names for their products using Linq. We can expect to see more LINQ to Something in the future...

    Cross-posted from http://linqinaction.net

  • ADO.NET Entity Framework documents are back (ADO.NET vNext)

    The original articles about the ADO.NET Entity Framework didn't stay online very long, but this time, two official documents are available:

    These documents will give you an overview of what is coming in the next version of ADO.NET, mainly the ADO.NET Entity Framework, which is Microsoft's upcoming solution for mapping data to objects.
    In these documents, you'll encounter the following:
    • The Entity Data Model
    • Entity SQL
    • LINQ to Entities
    • LINQ to DataSets
    • LINQ to SQL (formerly known as DLinq)
    Note: there is no preview release of the Entity Framework for the moment.
    We still don't know if DLinq will continue to live for a very long time (I don't see why we'd need two object-relational mapping solutions from Microsoft except for creating confusion).

    Update:
    These documents are also available online as web pages, not just Word documents.
    A document about the EDM (Entity Data Model) is also available: web page or Word document

    Cross-posted from http://linqinaction.net

  • Bye bye DLinq, Hello Linq for Sql and the ADO.NET Entity Framework!

    Some of you may have read the documents on ADO.NET 3.0 and the Entity Framework when they were published, before they were promptly removed. The big question since that time has been: What will happen to DLinq in regard to the future of ADO.NET, which seemed to offer the same services and more with a different solution?
    Well, Andres Aguiar has the scoop, live from TechEd in Boston: apparently both frameworks will be kept.

    OK, it actually happened. We'll have two mapping technologies in .NET v.next.

    LinQ for SQL, previously known as DLinQ is the 'simple' mapping technology.
    LinQ for Entities, will be on top of the new ADO.NET Entity Framework, and will be the 'complex' (we could say 'real') mapping technology.
    What? Two object-relational mapping technologies for .NET from Microsoft? What's the point?! Why not raise ObjectSpaces from the dead while the are at it...

    I feel the same as Andres:
    I think Microsoft did this for internal politically correctness (they did not want to not to ship any of the frameworks) but I can't see why this is good for the .NET Framework as a whole.
    The team that worked on DLinq worked on ObjectSpaces before and killing DLinq after ObjectSpaces may have been to hard for Microsoft's developers after all the years they have spent on these projects.

    To me, two solutions mean an excess of one. I don't see how people will be able to choose, and I think one will have to disappear sooner or later...

    Cross-posted from http://linqinaction.net

  • Bye bye WinFX, Hello .NET Framework 3.0!

    Somasegar, Corporate Vice President of Microsof's Developer Division, has announced that the WinFX brand has been killed! You should now talk about ".NET Framework 3.0" instead when you refer to the package that contains WPF (Windows Presentation Foundation - Avalon), WCF (Windows Communication Foundation - Indigo), WF (Workflow Foundation - Windows Workflow Foundation - Winoe), WCS (Windows CardSpace - InfoCard).
    The .NET Framework 3.0 runs on the .NET CLR 2.0.
    What will the framework delivered as part of the Orcas wave be called? .NET Framework 3.5? .NET Framework 4.0? And it will contain C# 3.0 and VB.NET 9.0...
    Clear as mud, isn't it? Microsoft's marketing likes to play funny games...

  • Copying and pasting text with styles removed - PureText

    I've been doing a lot of copy&paste operations between several Word documents or HTML pages lately, and I got fed up with doing "Edit | Paste Special... | Unformatted Text" all the time to avoid having my Word documents polluted with styles I don't want.
    A great little free tool came to the rescue: Steve Miller's PureText. I highly recommend that you give it a try!
    Pasting text with formatting and styles removed is as easy as using the Apple+V shortcut, an easy replacement for CTRL+V.

    Oh, for those who don't know, the Apple key is the one that looks like a window that looks like a waving flag located between CTRL and ALT...

  • Viewing the SQL generated by DLinq

    As demonstrated by Sahil in the last issue of his Demystifying DLinq series, you can get see the SQL DLinq would execute for a given query at debug-time using the built-in Query Visualizer:



    This useful little tool can also run the SQL query and show you the results if you want.

    In addition to the Query Visualizer, you can also get the SQL queries at runtime using the following line of code to redirect the SQL to the console: db.Log = Console.Out;
    Of course, you can also redirect the logs to anything else as far as it's a System.IO.TextWriter.

    Even better, you can also call GetQueryText(myQuery) on your DataContext instance to get the SQL at any time you want for a given query.
    If you have updated in-memory objects returned by DLinq, you can get the SQL that would be executed on the database by using DataContext.GetChangeText().

    A difference between GetQueryText() and GetChangeText() on one side, and the Query Visualizer on the other side, is that with the latter you can see the value of the SQL query's parameters.

    Cross-posted from http://linqinaction.net

  • Focusing on LINQ

    I've recently published some posts about LINQ, and as I wrote before, you can expect to see more on this weblog in the coming months. The reason? I'm currently writing a book on LINQ!
    I've opened a web site dedicated to the LINQ, XLINQ, DLINQ technologies and to the book. If you visit linqinaction.net you'll be able to find useful pointers to learn more about these technologies.

    Hopefully, the book and the site will give you everything you need to understand LINQ.