Rants Tagged with “ADO.NET”
1 2 3 4 5 6 7 8 > >> (Total Pages: 8/Total Results: 78)

As many of you may have heard, I recently launched http://GiveAQuiz.com as a new web site for creating and taking quizzes. The Data Team at Microsoft were great in helping me build this site. I've written a whitepaper detailing how we used the Microsoft data stack to accomplish it. This whitepaper shows how we used these data technologies to build the site:
If you are using Microsoft's Data stack for your own web-based and/or Silverlight site, give a look...hopefully some lessons learned you can use:
http://msdn.microsoft.com/en-us/library/ff847451.aspx
If you've been following my blog, you should know that I am keeping a pretty close watch on ADO.NET Data Services. The team recently released a second CTP of the new version with some interesting features. This CTP has some pretty compelling additions, but I am going ot focus on one in particular.
I've been teaching and using ADO.NET Data Services for a long time and I like showing off exposing a LINQ-based provider (Entity Framework, NHibernate or others) to a Silverlight application. While ADO.NET Data Services does expose its API through a REST API, the magic for me is in its use in Silverlight. In case you haven't been following along, using the Silverlight client you can issue a LINQ query through the Silverlight client (though in fairness, the full power of LINQ is not supported in the client):
var qry = (from g in ctx.Games
where g.Genre.Name == "Shooter"
orderby g.ReleaseDate
select g) as DataServiceQuery<Game>;
qry.BeginExecute(new AsyncCallback(r =>
{
try
{
theList.ItemsSource = qry.EndExecute(r).ToList();
}
catch (Exception ex)
{
// NOOP
}
}), null);
This is a powerful feature so that (unlike web services) developers can use a looser service definition to define their data stack. Let the developer who needs data be able to sort, filter and shape that data as necessary.
The last part of that phrase is "shape" on purpose. When I first teach this technology, I am often asked "what sort of shaping do you mean?" In ADO.NET Data Services 1.0, you could shape it by returning a hierarchy of data using the Expand method in the LINQ query. For example, we can return the game data with the Genre entity attached to every game like so:
var qry = (from g in ctx.Games
.Expand("Genre")
where g.Genre.Name == "Shooter"
orderby g.ReleaseDate
select g) as DataServiceQuery<Game>;
qry.BeginExecute(new AsyncCallback(r =>
{
try
{
theList.ItemsSource = qry.EndExecute(r).ToList();
}
catch (Exception ex)
{
// NOOP
}
}), null);
But this level of shaping is powerful but limited in scope. This is where ADO.NET Data Services 1.5 comes up trumps. In this new CTP we can now do projections to retrieve specific data. For example:
var qry = (from g in ctx.Games
where g.Genre.Name == "Shooter"
orderby g.ReleaseDate
select new
{
Name = g.Name,
ReleaseDate = g.ReleaseDate.GetValueOrDefault(),
}) as DataServiceQuery;
This query creates a new anonymous type that has the two values we want instead of returning the entire entity. In any other .NET project this would be fine, but for Silverlight we can't use the anonymous type for data binding (for security reasons). So like other anonymous type uses in Silverlight, you must project them into a known type. A type like that is pretty easy to cruft up if you need:
var qry = (from g in ctx.Games
where g.Genre.Name == "Shooter"
orderby g.ReleaseDate
select new GameInfo()
{
Name = g.Name,
ReleaseDate = g.ReleaseDate.GetValueOrDefault()
}) as DataServiceQuery<GameInfo>;
This is useful to no only limit the fields you return but also to shape the result. For example we can use the projection to retrieve the name, release date and the genre's name (normally a related object) like so:
var qry = (from g in ctx.Games
where g.Genre.Name == "Shooter"
orderby g.ReleaseDate
select new GameInfo()
{
Name = g.Name,
ReleaseDate = g.ReleaseDate.GetValueOrDefault(),
GenreName = g.Genre.Name
}) as DataServiceQuery<GameInfo>;
This is all well and good, but what about saving these projections? For the most part you should treat complex projections (like the one above) as read-only. But if you retrieve entities that are purely subsets of existing entities, then ADO.NET Data Services will handle it. Though currently this is crashing in Silverlight (it is a CTP after all). I will update this if I find a way around the saving bug in the near future and post the demo at that time.
But in general this is an awesome new addition to the library that I hope will close some of the use-cases that forced me to write Service Operations in the past.
What do you think?

It all depends...

One of my favorite patterns in the Entity Framework is a smarter way to use discriminators for data in a database. Using Tables per Hierarchy (TPH) or Tables per Type (TPT) all for more complex modeling of data in a relational data store. In a simple example, you might have a database that looks like this:

Notice that the ProductType is actually helping define what type of product it is (and therefore which decorator table holds additional information about that type). In the Entity Framework I like to model this as inheritance in the model:

Notice the connection between the Product and the Game/Accessory/Console tables is inheritance not a foreign key. A Game contains the properties defined by the Game type plus the properties defined by the Product type. Its real inheritance. This allows us to make LINQ queries like so:
var qry = from g in ctx.Products
where g is Game
select g;
The second line is the magic part of the query. This example shows searching through the Products in the model who are actually of the Type "Game". So the model has knowledge in it that makes it easier to query the real intent of the data without having to trouble with the details. The user of this model doesn't need to worry about what properties make it a Game, but can just treat it as such. Very powerful and just using tooling to describe data we've always modeled like this.
Up to this point we've had to build these models ourselves (not always the easiest to do with the current designer). Today the ADO.NET EF team released a new version of the command-line compiler (EDMGEN) that can look for these patterns and produce the right model for you. The team used some resources over at MS Research to help provide heuristics to detect these patterns and do the right thing. Check it out:
http://blogs.msdn.com/adonet/archive/2009/04/04/edmgen2-now-with-reverse-engineering-options.aspx
If you missed me in Bulgaria's DevReach conference, the video of my Silverlight Data Access talk is now available. The original talk's description was:
In this session, we will explore the different methods for dealing with data in your Silverlight 2 applications including LINQ, ASMX, WCF, REST, Astoria and WebClient calls. The session covers both how to consume data as well as how to expose data to Silverlight 2.
Hope you enjoy it!
Today at 3pm CST (4pm EST, 1pm PST) I will be doing a LiveMeeting talk on ADO.NET Data Services. If you are not at the PDC this week, drop by NotAtPDC.com and check out my session!
I've spent a number of hours since I downloaded the Oslo SDK to look around. In this post I want to tell you what I've found out.
Oslo is a funny beast because its multi-layered. With only a couple of hours on its hard to even know if my assumptions are all correct. I am sure we'll see in comments if I am far off or not.
As I read stories about Oslo before I was able to look at actual bits, I felt confused. I heard two stories: "Oslo is about Modeling and Model Driven Development"; and "Oslo is about building Domain Specific Languages". So which is it? Both.
I used to be "The ADO Guy" so lets start with the modeling:
Modeling Features
Oslo's Modeling Platform is a layered technology that is made up of several pieces:
- Quadrant: A Visual Designer for flowchart-like models.
- The "M" Language: A Textual DSL that supports creating your own system models or extending the built-in models.
- "Oslo Repository": A Database for storing models to be used at design-time and runtime.
In the SDK that I have played with, Quadrant isn't included (it is in the Oslo Virtual PC that is on the PDC attendee hard disk) so I haven't had any opportunity to look at that. What I did spent time with what the "M" language and a simple editor for it that is included called "Intellipad". The "M" language allows you to define quite a lot about an application, but the only part I played with so far is the modeling of data. Using Intellipad I attempted to model some Video Games data that i've been playing with in demos and the Silverlight 2 workshop. Intellipad simply lets me define the types including the data extents:

What is particularly interesting here is how the "M" language can be used to generate SQL scripts to create the storage. In Intellipad we can show a panel that shows the generated SQL:

(Click for a larger view of the SQL)
This is enough to keep my interest. I like where this is going but until I can really see what Quadrant does its hard to see how this fits into the bigger picture. So now lets look at the Domain Specific Language underpinnings:
Domain Specific Language Features
Of even more interest to me is the infrastructure that they used to build the modeling features. The "M" language is actually a Domain Specific Language called "MSchema" built with the Oslo Domain Specific Language ecosystem. MSchema defines a "little language" that is used to define models. But how is MSchema defined? It uses a lower level Domain Specific Language called MGrammar. MGrammar defines a "little language" for defining Domain Specific Languages. Beneath both of these is another language called MGraph that I haven't totally grok'ed yet.
I haven't had time to try and build one but in the SDK is a cool small example called "Song". This small Domain Specific Language defines a "little language" that supports songs as a list of notes and rests, for example:
Music
- - - D
C C# F G
E E - D
A E - E
G F - E
D C D E
A E D D
D E A C
More importantly, we can use the MGrammar to define what this language is composed of:

In most other instances I've been pushed to think of Domain Specific Languages being build either in a language (usually something like Ruby with lots of Macro'ing) or in data structures (like JSON or XML) since both of these ideas allow for avoiding building my own parser. The Oslo stack changes this idea for me an really allows me to define small languages without the need for these scenarios.
You may be asking how Domain Specific Languages impact you as a developer though? I recently gave a talk at the Atlanta .NET Users Group to try and cement some of these ideas. My overarching idea is that by writing "little languages" that use the same semantics as the domain, we can 'code' the intent of users in a domain (e.g. a business) and then the domain's stakeholders can validate the 'code' since its written in the language of the domain. But that's a story for another post. Understand that I see Domain Specific Languages as a powerful idea for everyday programmers, not just academic scenarios.
This is just a tiny taste, but so far I am incredibly intrigued by the possibilities of Oslo. In the coming months you can expect to hear quite a bit from me about this topic. But I am no expert. I started down this Domain Specific Language path by trying to read and study information on the topic. By far the best thing I ran across was this great video by Martin Fowler that everyone should watch...its well worth your time:
Since Martin Fowler is important in this space, I was thrilled to see he was shown a preview and had time to give his first impressions as well:
What do you think?
There is a known problem with ADO.NET Data Services today that is important if you (or your server) lives in specific timezones. The problem is associated with the way that the Silverlight Data Services Library constructs their URI for searches.
The problem surfaces if you do a query that has a DateTime comparison in it. For example:
var qry = from o in ctx.Orders
where o.OrderDate <= dt
select o;
This query generates the following URI in the EST timezone in the US:
http://.../ProductService.svc/Orders()?$filter=OrderDate le datetime'2008-10-13T00:00:00-04:00'
This works great. The problem is that in other timezones (e.g. Bulgaria) where its forward of Greenwich Mean Time, so the UTC date is +03:00 like so:
http://.../ProductService.svc/Orders()?$filter=OrderDate le datetime'2008-10-13T00:00:00+03:00'
Because the "+" isn't URL Encoded, it becomes a space which makes the date incorrect. For now you can convert the date to universal time but that's a hack at best:
var qry = from o in ctx.Orders
where o.OrderDate <= DateTime.Today.ToUniversalTime()
select o;
It works but its a hack.
In building my Silverlight RC example using ADO.NET Data Services for Entity Framework and NHibernate I ran into what I think is a common pattern. I am writing an editor for XBox game data. The model for this data uses decorator tables in the database which are modeled as a common "Product" class and derived "Game", "Console" and "Accessory" classes. In the application I am using paging to only look at fifty results at once. This works fine on both sides.
But one of the pieces of information I wanted was a list of all the Game Genre. This became problematic as ADO.NET Data Services wanted me to retrieve all 880 games in order to get a list of these Genres (of which there are only 20 some odd). The whole idea of using paging is go avoid the huge overhead of bringing down the whole entity. Interestingly when I executed a LINQ query that used projection into non-entities, the query wasn't supported as projection isn't allowed in the ADO.NET Data Services URI model (which the client uses).
What ADO.NET Data Services does allow is to create Service Operations (e.g. WebGet or WebInvoke) on the Data Service to extend the model for specific cases. There are some limitations (must return IEnumerable<T>, IQueryable<T> or void) but this works pretty well. The difference between returning IEnumerable and IQueryable is whether system queries can be applied to them. Returning a fixed list (my need) meant to return a IEnumerable<T> list. Intersting that ADO.NET Data Services support returning an IEnumerable<T> of primitive types. For example my operation was spec'd as:
[WebGet]
public IEnumerable<string> Genres()
{
}
This works and returns a simple XML file with the primitive values. But alas the Silverlight Data Service Client doesn't support non-entities. I tried using the DataServiceContext.BeginExecute() to call this service and it threw an exception that it couldn't materialize non-entity classes. Hrmph!
This was a case where adding a quick web service call to get this data on the server and return it would have been the easy road, but that's not how I roll, is it?
After confirming this behavior with the Data team, I decided to write an extension to the DataServiceContext class to support this. In this little piece of code, the same pattern of calling DataServiceContext.BeginXXX is used. To make this work I simply use the HttpWebRequest class to do a simple GET to the server's URI syntax and use LINQ to XML to convert the data into the simple types (String in my case).
I've started to help out with some new ideas on the CodePlex SilverlightContrib project so I thought this was the perfect place for this code. Its not packaged up yet in a build (and probably won't be until sometime after RTW ships) but if you want to grab it you can grab the latest checkin I made:
SilverlightContrib ChangeSet 41005
I'll be shipping this new demo as soon as RTW ships (its not done yet). Look for the announcement here.
It seems that because of some internal NHibernate changes that are required to make NHibernate LINQ work really well, the current version of NHibernate LINQ will not be supported. Evidently there are a number of complex queries that do not work correctly under the current codebase. Its been announced that these changes will be made in the NHibernate 2.1 (which is in development). Follow the link to read the full details!