Extending Interfaces with Implementation

Today I was faced with an issue.  I had an interface based implementation of a specific functionality.  All classes that implemented this interface had different implementation of course.  What I had to add is ability to run certain checks on each implementation that would need to analyze the data defined in the interface and return a Boolean result of this check.

The problem was that the checks were all the same and I hated to add this check to the interface and write identical code in all implementations.  As everyone knows interfaces cannot contain implementation code though.  So, what should I do?

So, a particular .NET 3.5 feature came to my rescue.  I am referring to extension methods.  As you may or may not know extension methods are tied to a type.  Well, interface is a type too.  Here is some code to look at.

Here is the interface definition:

    public interface ISupportCustomAction

    {

        Action CustomAction { get; set; }

        void Execute();

    }

Here is my class definition that implements this interface

    public class CustomActionOne : ISupportCustomAction

    {

        #region ISupportCustomAction Members

        private Action action;

        public Action CustomAction

        {

            get

            {

                return action;

            }

            set

            {

                action = value;

            }

        }

 

        public void Execute()

        {

            // fill in the method

        }

 

 

        #endregion

    }

Finally, here is my extension method

    public static class ISupportCustomActionExtensions

    {

        public static bool Check(this ISupportCustomAction action)

        {

            if (action.CustomAction == null)

            {

                return false;

            }

            else

            {

                return true;

            }

        }

    }

Here is my test code to confirm that everything works

    class Program

    {

        static void Main(string[] args)

        {

            CustomActionOne action = new CustomActionOne();

            action.CustomAction = () => { Debug.WriteLine("Exectuted Action."); };

            Analize(action);

 

            action = new CustomActionOne();

            Analize(action);

            Console.ReadKey();

        }

 

        private static void Analize(ISupportCustomAction action)

        {

            if (action.Check())

            {

                Console.WriteLine("Can execute action");

            }

            else

            {

                Console.WriteLine("Cannot execute action");

            }

        }

    }

How useful this is?  Pretty useful in my case.  Saved me some typing and resulted in consistent coding.  My point is that is good to know what features .NET has and use them as a need arises.  As you can see I have a hybrid here of a base (abstract) class and an interface.  As a result, I am able to use features that belong to both concepts.

 

Thanks.

Community Events

I often forget about different conferences I would like to keep an eye on, so I decided to make a list of them on my blog.

Silverlight 4 + COM + SQL Server = Cool!

Today is the day to talk about COM possibilities in Silverlight 4.  One would question the titles of my post.  COM and Cool in the same sentence?  Let me proof this title to you.

First, let me take a closer look at COM.  In order to access COM, you must install Silverlight application on a local machine.  The access to COM is not enabled when a Silverlight application is run in the browser.  So, first of all you have to enable this by checking “Enable running application out of browser” checkbox in project properties.  Second of all, you have to check “Require elevated trust…” checkbox in “Out-of-browser settings” area in project properties.  Now, you are ready to install you application and test COM support.  How about Word automation:

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
var doc = word.Documents.Add();
var paragraph = doc.Paragraphs.Add();
paragraph.Range.Text = "Some text";

Now, let’s talk about database access.  Parts of System.Data.SqlClient namespace are not exposed to COM by default.  So, to enable database access we must write an assembly that is exposed to COM that wraps database access.  To do so, check the property “Register for COM Interop” in project properties for the .NET (NOT Silverlight) based project that will fire off database queries for us.  Once the assembly is built, I can just use RegAsm to register my DLL with COM on a machine.  Of course, this would be a prerequisite to use Silverlight application for local data access.  The other prerequisite is to have .NET Runtime installed on that machine as well.  I could write an install project of course to make this process easier.

The next step is to write a Silverlight assembly that would use COM Interop similar to the one above for Word to talk to my database access .NET based assembly.

dynamic sqlDB = ComAutomationFactory.CreateObject("COMSQLClient.COMSqlDatabase");

Now the most exciting part.  I created and published the project on CodePlex that does exactly what I just talked about.  Check out the project and let me know what you think.  You can download the source code and look into the implementation details.  Here is the link to it:

http://silverlight4sqllib.codeplex.com/

Thanks.

The MSDN Southern Fried Roadshow

Glen Gordon, our local evangelist is doing another road show.  Here are the event details.

Thursday, February 25, 2010 1:00 PM – Thursday, February 25, 2010 5:00 PM Eastern Time (US & Canada)
Welcome Time: 12:30 PM

Atlanta Marriot Alpharetta

5750 Windward Pkwy
Alpharetta Georgia 30005
United States

Language(s):
English.

Product(s):
Azure Services Platform, Microsoft technologies, Windows and Windows 7.

Audience(s):
Pro Dev/Programmer.

Presenter(s):
Glen Gordon, Joe Healy

Event Overview

MSDN Events presents: Take Your Applications Sky High with Cloud Computing and the Windows Azure Platform

Join your local MSDN Events team as we take a deep dive into cloud computing and the Windows Azure Platform. We’ll start with a developer-focused overview of this new platform and the cloud computing services that can be used either together or independently to build highly scalable applications. As the day unfolds, we’ll explore data storage, SQL Azure, and the basics of deployment with Windows Azure. Register today for these free, live sessions in your local area.

If you register and attend this event, you will be placed in a raffle to win a chance to bring home one (1) free copy of Windows 7 – you could be the lucky winner! Register today!

SESSION 1: Overview of Cloud Computing and Windows Azure

The Windows Azure platform is a set of high-performance cloud computing services that can be used together or independently and enable developers to leverage existing skills and familiar tools to develop cloud applications. In this session, we’ll provide a developer-focused overview of this new online service computing platform. We’ll explore the components, key features and real day-to-day benefits of Windows Azure.

Highlights include:

· What is cloud computing?

· Running web and web service applications in the cloud

· Using the Windows Azure and local developer cloud fabric

· Getting started – tools, SDKs and accounts

· Writing applications for Windows Azure

SESSION 2: Survey of Windows Azure Platform Storage Options

Durable data storage is a key component of any cloud computing offering. The Windows Azure Platform offers many options, which can be used alone or in combination. Windows Azure itself offers ready-to-use and lightweight storage in the form of tables, blobs, and queues. Another choice for storage is SQL Azure, a true relational database in the cloud. In this session, we’ll explore the highlights of these implementations and how to both create and use storage in each form. We’ll give you guidance on choosing the right forms of storage for your application scenarios.

Highlights include:

· Understanding table & blob storage

· Programming against table & blob storage

· Working with queue storage

· Managing credentials and connection strings

· Scaling and configuration

· Understanding SQL Azure databases versus local SQL Server databases

· SQL Azure firewall, logins and passwords

· Database creation, deployments and migrations

· Database management using SQL Management Studio

· Programming against SQL Azure databases

SESSION 3: Going Live with your Azure Solution

Windows Azure features a powerful, yet simple deployment model. By focusing on your application and abstracting away the infrastructure details, you can deploy almost any app with minimal fuss. In this session, we’ll walk you through the basics of Windows Azure deployment, including site monitoring, diagnostics and performance issues.

Highlights include:

· Start-to-Finish Visual Studio demonstration of a realistic XML data driven business web site from the desktop to the cloud.

· Windows Azure Deployments

· Start-to-Finish Visual Studio demonstration of a realistic SQL Server data driven business web site from the desktop to the cloud.

· Configuration of your application in the cloud

· Guidance and Suggestions to ensure your success

*Be sure to learn more about the TechNet event happening on the same day at the same location from 8:30am–12:00pm. If you are interested in attending, please register separately for that event here: http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032439495&Culture=en-US

Registration Options

Event ID:
1032439974

Here is the link to this event:

http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032439974&Culture=en-US

Entity Framework 4.0 and Multiple Data Contexts

In the EF version 1.0, there was a problem when one would try to update multiple contexts as part of a single transaction.  For example, I can use Transaction Scope, the update one context, then second context.  Here is sample code to illustrate:

 

using (TransactionScope scope = new TransactionScope())
{
    try
    {
        using (RolodexEntities context = new RolodexEntities())
        {
            context.Connection.Open();

            Company company = new Company();
            company.CompanyName = "Test 1";
            company.DateAdded = DateTime.Today;
            context.AddToCompanies(company);
            context.SaveChanges();

            using (RolodexEntities context2 = new RolodexEntities(context.Connection as EntityConnection))
            {
                Company company2 = new Company();
                company2.CompanyName = "Test 1";
                company2.DateAdded = DateTime.Today;
                context2.AddToCompanies(company2);
                context2.SaveChanges();
            }
        }

        scope.Complete();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

This code in version 1.0 would throw an exception when second context’s constructor was called.  The exception was quite obscure and was referring to the fact that the connection was already open.  This was actually correct, but that was the whole reason I was passing in the connection.  I was trying to make sure that my transaction would not get promoted to Distributed Transactions Coordinator.  The transaction would always get promoted as soon as there are multiple SQL Server connections used within using scope/.end using scope code block.  So, in version 1.0 MSDTC would be required in the code above.

This issue still exists in EF 4.0.  You can vote to fix the issue here : https://connect.microsoft.com/data/feedback/details/533240/cannot-share-a-store-connection-between-multiple-contexts-in-order-to-avoid-dtc-promotion

Here is the code that confirms the problem:

using (TransactionScope scope = new TransactionScope())
{
    try
    {
        using (RolodexEntities context = new RolodexEntities())
        {
            context.Connection.Open();

            Company company = new Company();
            company.CompanyName = "Test 11";
            company.DateAdded = DateTime.Today;
            context.AddToCompanies(company);
            context.SaveChanges();

            RolodexEntities1 contextTemp = new RolodexEntities1();
            MetadataWorkspace space = (contextTemp.Connection as EntityConnection).GetMetadataWorkspace();
            EntityConnection connection = new EntityConnection(space, (context.Connection as EntityConnection).StoreConnection);

            using (RolodexEntities1 context2 = new RolodexEntities1(connection))
            {
                User user = new User();
                user.UserName = "test";
                user.Role = "User";
                context2.AddToUsers(user);
                context2.SaveChanges();
            }
        }

        scope.Complete();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Please feel free to ask any questions on this subject.