Tuesday, September 18, 2007

The EntitySpaces Dynamic Query API will soon be sporting Join’s and Arithmetic Expressions. The syntax is so elegant even LINQ enthusiasts will take pause. Let’s take a look at how you would do a join using the enhanced EntitySpaces DynamicQuery. Note that we are using all of your existing objects to build the query.

Performing Joins …

CustomersQuery cust = new CustomersQuery ("c");
OrdersQuery orders = new OrdersQuery ("o");
OrderDetailsQuery details = new OrderDetailsQuery ("d");

cust.Select(cust.ContactName, details.Quantity.Sum().As("TotalQuantity"));
cust.InnerJoin(orders).On(cust.CustomerID == orders.CustomerID);
cust.InnerJoin(details).On(orders.OrderID == details.OrderID);
cust.Where(cust.ContactName.Like("%Mike%"));
cust.GroupBy(cust.ContactName);

CustomersCollection coll = new CustomersCollection ();
coll.Load(cust);    // Load it …

Now that is a pretty sweet syntax …

Of course, RightJoin, LeftJoin, and FullJoin are also supported. The nice thing about this approach is that you are spoonfed the syntax via intellisense, no need to stop and create a view (although you can if you want to and generate your business entities off of the view). The "o", "c" and "c" shown above are merely the aliases used when building the SQL.

Arithmetic Expressions

You can now use arithmetic expressions in your query's. Notice how you can use the natural language syntax with * / + - and %. Take a look at this sample …

CustomersQuery cust = new CustomersQuery ("c");
OrdersQuery orders = new OrdersQuery ("o");
OrderDetailsQuery details = new OrderDetailsQuery ("d");

cust.Select(cust.ContactName, (details.Quantity * details.Price).Sum().As("TotalPrice"));
cust.InnerJoin(orders).On(cust.CustomerID == orders.CustomerID);
cust.InnerJoin(details).On(orders.OrderID == details.OrderID);
cust.Where(cust.ContactName.Like("%Mike%"));

CustomersCollection coll = new CustomersCollection ();
coll.Load(cust);    // Load it …

How Does this Effect Binding?

Your EntitySpaces collections will now provide a new method named LowLevelBind(). Normally, you bind directly to the properties in your EntitySpaces entities. However, when joins are in play you are bringing back columns that aren’t in your entities. Thus, the LowLevelBind will bind directly to the underlying DataTable. The are other ways of course using EntitySpaces but we wont cover those in this post.

grid.DataSource = coll.LowLevelBind();

Can I still Save an Entity that was Built off a Join?

The answer is "yes". Of course, it will only save to the main table, in the above case the Customer table.
We already have this implemented and are hoping to have a beta out with joins and arithmetic expressions around October 1st, 2007.

These new features will be available for all of the many databases supported by EntitySpaces.

EntitySpaces

From mobile devices to large scale enterprise solutions in need of serious transaction support, EntitySpaces can meet your needs. Whether you’re writing an ASP.NET application with medium trust requirements, or a Windows.Forms application, the EntitySpaces architecture is there for you. EntitySpaces is provider independent, which means that you can run the same binary code against any of the supported databases. EntitySpaces is available in both C# and VB.NET. EntitySpaces uses no reflection, no XML files, and sports a tiny foot print of less than 200k. Pound for pound, EntitySpaces is one tough, dependable .NET architecture.

The EntitySpaces Team
--

EntitySpaces LLC
Persistence Layer and Business Objects for Microsoft .NET
http://www.entityspaces.net

Tuesday, September 18, 2007 2:24:47 PM (Eastern Standard Time, UTC-05:00)
Sweet!

Will you support the LINQ join syntax too?
Sean Nolan
Thursday, September 20, 2007 8:15:26 AM (Eastern Standard Time, UTC-05:00)
That's a pretty involved questions really, the answer is probably "yes", but not for a while. There is actually a way we can allow folks to populate their EntityCollections now using LINQ, see http://www.west-wind.com/weblog/posts/141435.aspx. You can already use LINQ against our collections once loaded, of course. It could very well be that ES2008 will fully support LINQ using the trick above, but we haven't explored it yet. LINQ did have a way to get a DataTable from a LINQ query, it was in there beta's. I'm sure they removed it so we couldn't use it as EntitySpaces would have had full LINQ support out of the box on day 1, they couldn't allow that.

If you want to see the nightmare involved in supporting LINQ take a look at Fran Bouma's blog, and follow his journey. Frankly, right now, spending months trying to support LINQ isn't in our roadmap, we believe our DynamicQuery syntax is the best in the industry as far as intuitiveness and understandablity go. We need to add SubSelects next and that will be very clean as well.

So, I guess the short answer is "yes", we will support LINQ, but the longer answer is "not for a while". Eventually I will make a detailed blog post on this subject. For LLBLGen, I can understand why Frans would want to try to move to LINQ quickly, just compare the LLBLGen query syntax on his "LINQ" posts versus our EntitySpaces queries. We, however, are in a much better position.
Comments are closed.