Archive for April, 2008

Entity Framework Stored Procedure Generation

Saturday, April 26th, 2008

At work my team and I are utilizing the ADO.NET Entity Framework as a means to mitigate the object-relational impedance mismatch. Simply put, we’re utilizing it as an object-relational mapper for our latest project. So far we have been really impressed with the flexibility it provides us, especially surrounding the area of mapping the same table to multiple entity types and creating inheritance relationships among those entity types. It has proven itself as a much more viable candidate than .netTiers or LINQ to SQL for us.

While I have a lot of praise for the Entity Framework, there are some quirks that take getting used to. For example, you can’t update properties that are part of a primary key, even though you can update (non-identity) columns that are part of the primary key in SQL Server 2005. Another is the requirement that if an entity will be inserted into the database using a stored procedure, it must also be updated and deleted through a stored procedure. Yet another is that each stored procedure mapped to a particular entity type must have parameters mapped to every association key, regardless of the type of stored procedure (INSERT, UPDATE, or DELETE). For example, consider the following model:

image 

If I created stored procedures for the Account entity to use in my EF model, they’d look like this:

CREATE PROC dbo.Account_Insert

(

    @customerId INT

)

AS

BEGIN

    INSERT INTO Account (customerId)

    VALUES (@customerId)

 

    SELECT SCOPE_IDENTITY() as accountId

END

GO

 

CREATE PROC dbo.Account_Update

(

    @accountId INT,

    @customerId INT

)

AS

BEGIN

    UPDATE Account

    SET customerId = @customerId

    WHERE accountId = @accountId

END

 

CREATE PROC dbo.Account_Delete

(

    @accountId INT,

    @customerId INT

)

AS

BEGIN

    DELETE FROM Account

    WHERE accountId = @accountId

END

Notice the delete stored procedure—it must take in both the ID of the Account and the ID of the Customer it is associated with. Based on Colin Meek’s post here, I can understand why this is necessitated by the Entity Framework, but I can’t dispute the fact that it is different from I’m used to writing stored procedures.

As we moved towards implementing sprocs for our persistence layer to utilize, we ran into a few problems. At first, we thought we could utilize old templates from CodeSmith and .netTiers to generate sprocs, and our EF model would consume them gracefully. Unfortunately, if we used these templates, we would end up with sprocs like this for the Account table:

CREATE PROC dbo.Account_Insert

(

    @accountId INT OUTPUT,

    @customerId INT

)

AS

BEGIN

    INSERT INTO Account (customerId)

    VALUES (@customerId)

 

    SET @accountId = SCOPE_IDENTITY()

END

GO

 

CREATE PROC dbo.Account_Update

(

    @accountId INT,

    @customerId INT

)

AS

BEGIN

    UPDATE Account

    SET customerId = @customerId

    WHERE accountId = @accountId

END

 

CREATE PROC dbo.Account_Delete

(

    @accountId INT

)

AS

BEGIN

    DELETE FROM Account

    WHERE accountId = @accountId

END

Notice the slight differences:

  1. The INSERT sproc takes in an output parameter for the identity column and then sets the value of that parameter to the ID that was just inserted. The EF version of the sproc does not take in an output parameter; instead it just selects SCOPE_IDENTITY() for the EF to merge back into the Account entity that was inserted.
  2. The DELETE sproc does not take the parameter for the Customer ID, because it is not part of the primary key.

This causes some vexation for us because our new business domain houses about forty tables for which we need to write CUD sprocs that fit the Entity Framework’s rules. We could write them by hand (which would be painful and subject to error), try to modify CodeSmith’s templates to generate them correctly, or try to generate them using some other way.

From the title of this blog entry, you can probably infer that we didn’t write them by hand. Furthermore, Ryan Hauert and I were toying around with the idea of using T4 Templates to generate the SQL we needed. In all honesty, we didn’t even consider modifying the CodeSmith templates; I don’t think this was a bad choice, as T4 templates can benefit people who don’t want to purchase CodeSmith, but it may’ve been easier for our particular situation. But I digress…

The T4 template we created is designed to be a general purpose way to generate CUD procedures for an Entity Framework model. In case you didn’t read the links I provided on the templates, they are code-generation files which are intrinsically recognized by Visual Studio 2008. (They required the VS SDK in VS2005.) They have a .tt extension, and Visual Studio will nested generated code files underneath them in the Solution Explorer, similar to how designer files are nested under Entity Framework and LINQ to SQL models.

image

It’s a very simple process to use the T4 template:

  1. Add the file into your project. (The file is linked at the end of the blog entry.)
  2. Simply save the template file and CUD sprocs will be generated for every EDMX file in the current directory.

If you want to change what directory the template looks in or what files it generates sprocs for, look at the configuration parameters on lines 24 and 25 in the picture above. You can specify the directory explicitly by initializing the directoryName variable to anything other than the empty string or null. Furthermore, if you want to match a specific EDMX file only, you can use a regular expression to filter out unwanted files. I could use it like this:

Regex edmxFileFilter = new Regex(@"Entities\.edmx");

Then only the Entities.edmx file would be processed.

The template not only generates EF-compliant sprocs, but it also considers only the tables that are in the SSDL of the model file. Unfortunately, the template must query the database (SQL Server 2005 only!) in order to make all the decisions necessary to create the output file. However, this is usually quite fast on generations other than the first one.

As you can see from the picture above, the template requires a few different assemblies to be installed. Most of them shouldn’t be a problem when generating EF sprocs, because it strongly suggests that you have the .NET Framework 3.5 installed. Something you may not have are the SQL Server Server Management Objects (SMO), which offer an abstraction layer over the metadata of a SQL Server instance (e.g. databases, tables, columns, keys, indexes, data types), and keep the template cleaner than if we had to make ad hoc queries directly. The SMO installation is available on this page, where its installer is called "SQLServer2005_XMO.msi." There are other installers for 64-bit and Itanium 64-bit.

Once that is installed the template is very simple to run and gives us output like this:

image

Now we can take this script and run it against SQL Server and then import the sprocs into our Entity Framework model. Finally we can map the stored procedures to the entity types they encapsulate and voilà! We’re done.

There are a few limitations with the template the moment, but it will account for most cases:

  1. No stored procedures are generated for tables that do not have primary keys (in the database, not in the EF model). There is no way we can infer what you meant for the key to be for the update and delete sprocs; while we could generate insert procedures, it wouldn’t help the end user because the EF requires that an entity type be mapped to all three, or none at all.
  2. The template is available only in C# at the moment.
  3. It has not been tested in all scenarios, so any feedback you have would be great. :)
  4. It does not account for entity inheritance.

T4 Template

Special thanks to Bob Pace for giving Ryan and me the idea and providing helpful resources on T4.

Also, if you want to get started writing your own T4 templates, check out Hilton Giesenow’s excellent video here.

May 22, 2008 - Updates:

Ryan and I ran into an issue recently where we were accidentally comparing all of the parameters against values in database rows for the delete stored procedures. This was a mistake, because we really only need to check for equality on the primary key columns. The new template has been uploaded.

Interact 2008 - Day 3

Monday, April 14th, 2008

The final day at Interact 2008 was a fitting end to the conference; for me it was definitely more mellow because there wasn’t a keynote, and I didn’t attend as many breakout sessions. On the other hand, I would be lying if I said I didn’t learn anything.

To replace the keynote the conference organizers had planned a "Birds of a Feather" sessions, small gatherings of about five to fifteen people each that focused on certain topics of discussion, such as "Mobilizing Unified Communications," "Voice Infrastructure," and "Architecting for a UC World." This morning Mark and I attended the "Blogging and Other Online Activities" group (you can probably guess why). There were a lot of great ideas floated not just about blogging, but also about other online collaboration tools like forums, wikis, and newsgroups.

The most interesting discussion was around forums, because there was a lot of debate concerning their effectiveness. I use forums for both technical and interpersonal reasons, so it was interesting to see that the issues of contention apply to both types of forums:

  • They are not effective tools for finding answers to questions.

The forums at MSDN are currently the only forums that I know of that allow users to mark posts as answers. Even so, what if threads have tens or even hundreds of pages? It is not easy to sift through that much content just to find an answer to your question. The forum and thread format, however, does facilitate general discussion threads, but with large threads it’s still not easy to keep track of all that’s been said.

  • Put up an FAQ list? Nobody reads it.

One thing I’ve noticed in both sets of forums is that nobody reads the FAQ. Most people just rush in and post because they want their question to receive special attention (maybe that means their question will be answered more quickly?). One brilliant idea I heard that combats this approach and steers more focus toward the FAQ is for moderators to delete threads that ask questions that are already in the FAQ.

Afterwards, I headed to the Developer Code Camp, where Paul Robicheaux presented the basics of the UCMA and Albert Kooiman did a short demo of Speech Server 2007’s capabilities. I was already familiar with Speech Server 2007, but it was very valuable to understand the purpose of the UCMA and how developers could utilize it (which I discussed yesterday). The code camp at this conference was a little rushed to cover all of the APIs we wanted to, but there was still time before the close of the conference to get hands-on experience with the APIs in the hands-on lab area.

(more…)

Interact 2008 - Day 2

Saturday, April 12th, 2008

The second day at Interact opened with a keynote by Terry Myerson, the Corporate Vice President of Exchange, who approached the idea of Unified Communications from the email (specifically, Exchange) perspective. The previous day’s keynote covered a lot of Terry’s points, but it was interesting to hear how the Exchange team viewed their role in Microsoft’s UC platform. Even though Exchange has Unified Messaging, the team wants to move the focus to UC, not just UM, because the business problem that Microsoft is addressing is achieving people-centric communication. Thus it follows that in any UC solution, it’s people first and tools second.

Terry then touched on the topic of interoperability, discussing how Exchange ActiveSync would be leveraged by the iPhone Enterprise edition. But perhaps the most interesting part of the presentation was the demonstration of Exchange Labs, a new program geared to test the next version of Exchange. In this inaugural rollout, its target audience is select schools and universities as part of Windows Live @ edu, allowing them to connect students, alumni, faculty, and staff through email. There were two individuals who had overseen the deployment of Exchange Labs to their respective school districts with amazing results.

Terry concluded with a quotation from Thomas Jefferson: "Every generation needs a new revolution." I personally think we are seeing great advancements in the area of unified communications—this is our revolution.

Day 2 featured more sessions that appealed to developers than the previous day, so it was right up my alley. Here are the sessions I attended:

  • Microsoft Unified Communications for Developers: Building Communications into Your Applications (Albert Kooiman, Paul Robichaux)

The Microsoft Unified Communications platform enables developers to easily build secure and productivity enhancing applications atop a extensible foundation. This session will explore the concept of communications as a first class feature in Windows and Web applications through sample applications that demonstrate how to build voice, video and messaging communications into your applications along with speech and messaging based UIs. It will give an overview of the types of applications that can be built using messaging as well as software-based Voice over IP (VoIP) and will explain which APIs and SDKs are available to build those applications.

  • Developing with Exchange Web Services (Paul Robichaux, Albert Kooiman)

Exchange Server 2007 introduces new Web Services APIs to integrate information stored in Exchange into line-of-business applications. Examples include calendaring applications, contact management applications, or applications that access other content from the Exchange store, like creating editing and sending messages, handling tasks or managing contacts, etc. This session gives an overview of the Exchange Web Services APIs, and focuses on specific illustrations of how to use Exchange Web Services in your line-of-business applications. This session includes walk-throughs of code examples of how to integrate data from the store into LOB applications and third party solutions.

(more…)

Interact 2008 - Day 1

Thursday, April 10th, 2008

The first day of Interact 2008 was a superb opening to the conference. The day started with a keynote from Gurdeep Singh Pall, who is the "corporate vice president in the Unified Communications Group at Microsoft Corp. He is responsible for vision, product strategy and R&D for Microsoft’s Unified Communications including voice over Internet protocol (VoIP)."1 From there we had the opportunity to attend "breakout sessions," seminars led by Microsoft employees or MVPs on abstruse topics for OCS, Office Communicator (OC), and Exchange, or participate in hands-on labs—exercises also related to OCS, OC, and Exchange, aimed at developers and IT professionals.

Gurdeep’s keynote stressed three points. The first was the future of software communications. Telephony is an industry that has been dominated by hardware, and the evolution of communication systems has traditionally coincided with the evolution of phones. However, what about other means of communication such as email and IM? It is becoming apparent that software has a place in the communication industry; some would even argue that it plays a major role. By building a platform for software developers to build off in the unified communications area, Microsoft’s UC solution brings the focus away from the presence of a machine (such as a phone or a laptop) to the presence of a human being.

Gurdeep went on to explain his own version of Maslow’s Hierarchy of Needs (called Gurdeep’s Hierarchy of Needs) which addresses the needs of telecom managers. The four lower levels of the pyramid were needs analogous to Maslow’s deficiency needs. If you have them, you don’t think about them; for example, you don’t go around the office exclaiming that you have email—similarly, you don’t think about physiological needs if you have met them. The point was that once the deficiency needs are met, you can self-actualize, which encourages spontaneity and creativity for innovating solutions to business problems.

Finally, Gurdeep discussed the perspective that Microsoft brings to the UC table—moving IT into the "business zone" using three core concepts:

  • Interoperate (1 + 1 = 2)

Previously, the model for UC has been vertically integrated communications, where vendors offer their own stack of technologies to provide a comprehensive telephony solution. Microsoft wants to turn that model on its side by identifying various layers of telephony (hardware, software, devices, etc.) for which multiple vendors can offer a number of different solutions. Microsoft knows it can make advances by providing a software platform which can interoperate with existing vendors’ hardware and devices. This model will allow for industry- and enterprise-specific development and innovation—in essence, self-actualization.

  • Integrate (1 + 1 = 3)

Whereas interoperability is about making sure your platform is compatible with other platform, integration is about achieving synergy across disparate systems in an architecture. At this point, Gurdeep let Albert Kooiman and members of Clarity Consulting to demonstrate how they developed a pilot project which integrated their CRM application with the presence and communication features within Office Communicator and OCS. (This sort of integration is also what we are very attracted to for our own telephony solution.)

  • Impact

Sometimes it’s about more than just ROI. (And I’m sure some of you will disagree.) Gurdeep played a video about children at St. Luke’s Hospital who are getting a second chance at school and education through Microsoft RoundTable. There is an abridged article about the story here.

(more…)

Interact 2008 - Pre-Game Show

Monday, April 7th, 2008

Mark and I flew into San Diego tonight, and wow, if you’ve never been here, prepare for some really beautiful scenery! Unfortunately I didn’t get much of a chance to take pictures outside, but I did once we got inside the "Pub World" pavilion. The first night here was phenomenal—I have never been to a Microsoft event like this before, but I am really impressed at the execution.

The event tonight was a chance for people to just get together and enjoy the evening. The pavilion had XBOX360 (Halo and some other games I didn’t recognize), pool, darts (!), air hockey, foosball, ping pong, shuffleboard, and of course, a huge screen showing the NCAA finals. Here are some photos from the event.

PICT0817

PICT0824

PICT0818

PICT0819

PICT0820

Here’s to another great day tomorrow!

Interact 2008

Sunday, April 6th, 2008

Recently my blog posts have focused on Visual Studio Extensibility, but the challenges that I have come up against at work involve vastly different technologies. One of these is in the Unified Communications space, where my team and I recently investigated the feasibility of using Microsoft’s Office Communications Server (OCS) in a contact center space. During this pilot project, I helped Ryan Hauert develop a front-end client (a scaled-back CRM application) that utilized the UCC API to interact with OCS. I also created an ACD that linked that front-end client with an IVR workflow built by Bek Yakvalkhodjiev against Speech Server 2007. Instead of using a queue to manage call routing, I used a prototype version of the algorithm that Mark Stafford explains here. The entire project took about three weeks, and that includes the time it took for all of us to learn the APIs and to set up the sandbox environment where OCS was hosted. The results were very fruitful not just for our team, but for the company as a whole to see what Microsoft has to offer versus its competitors in the UC space.

The reason I bring this up is because Mark and I will be heading to Microsoft’s Interact 2008 conference tomorrow in San Diego, California. This will be a fantastic opportunity not only to learn more about what Microsoft is bringing to the table, but also to make valuable connections with other people interested in OCS who are potentially going through the same evaluation that we are right now! If you’re interested in the contact-center-building facet of OCS and are heading to Interact ‘08, feel free to drop me a line through my blog or through my email.

Dynamic Menu Commands in Visual Studio Packages - Part 3

Saturday, April 5th, 2008

This is the final post in a series detailing how to build dynamic menu commands in Visual Studio packages. The previous posts are located here:

  • Part 1 - Discusses UI Contexts and how to utilize the built-in ones for dynamic menu commands.
  • Part 2 - Discusses the use of the BeforyQueryStatus event to provide more flexibility than built-in UI contexts.

What we’ve explored to date is a way to provide dynamic menu commands (e.g. dynamic visibility, enabled state) in our own package, and the techniques that I’ve shown thus far have worked well for this scenario. However, if you want to develop multiple menu commands or even multiple packages that rely on a custom condition (as shown in Part 2), then you’re stuck implementing the same logic in an event handler for the BeforeQueryStatus event for each OleMenuCommand.

Wouldn’t it be great if we could create our own UI Context, similar to the built-in ones like UIContext_NoSolution or UIContext_FullScreenMode? That way we can create multiple menu commands that rely on that context or even multiple packages which rely on it.

And that’s exactly what this post will cover. We’ll use the solution starting from where we left off at the end of Part 2.

(more…)