Monday, November 21, 2011

Factory Method Design Pattern

This is some sample code from C# 3.0 Design Patterns by Judith Bishop outlining the Factory Method Design Pattern:

using System;
  using System.Collections;

  class FactoryPattern {
  
  // Factory Method Pattern       Judith Bishop 2006
  //  Example of exporting from different suppliers
    
  interface IProduct {
    string ShipFrom();
  }

  class ProductA : IProduct {
    public String ShipFrom () {
      return " from South Africa";
    }
  }
  
  class ProductB : IProduct {
    public String ShipFrom () {
            return "from Spain";
    }
  }

  class DefaultProduct : IProduct {
    public String ShipFrom () {
            return "not available";
    }
  }

  class Creator {
    public  IProduct FactoryMethod(int month) {
      if (month >= 4 && month <=11)
        return new ProductA();
      else 
      if (month == 1 || month == 2 || month == 12)
        return new ProductB();
      else 
        return new DefaultProduct();
    }
  }
  
    static void Main() {
      Creator c = new Creator();
      IProduct product;
        
      for (int i=1; i<=12; i++) {
        product = c.FactoryMethod(i);
        Console.WriteLine("Avocados "+product.ShipFrom());
      }
    }
  }

So by looking at the code, you can probably get a good feel for what the code is trying to accomplish.

There are 3 products which all need to implement common shipping functionality but need to ship from 3 different locations. Therefore, this is a perfect candidate for implementing an Interface across all of the class implementations. By implementing a common Interface, the classes can easily be swapped for one another (called "polymorphism"). Since all of the classes are interchangeable, there only needs to be a set of criteria which dictates which class is actually instantiated and utilized within the Main method.

In this case, the Factory method is responsible for accepting that parameter (in this case, the month) and then returning the appropriate instance of the class based on the internally developed logic which has been conveniently encapsulated into a single method. The Main method can now simply be oblivious to the actual instance of the class that is required. The class is returned as an instance of an Interface and the only parameter that is required to be known is the month. The Factory Method in this manner also assists in the principles of encapsulation and decoupling.

Thursday, November 17, 2011

Converting from one version of Visual Studio version to another

If you work with a large number of developers that may have different versions of Visual Studio on their local workstations, you may have to constantly deal with version conflicts in projects and solutions.

Fortunately, a tool exists for allowing you to convert from versions of Visual Studio 2010 back to versions of Visual Studio 2008 or Visual Studio 2005 (with the disclaimer that all functionality will not be guaranteed to work by switching between the various versions).  You can download the tool here: http://www.emmet-gray.com/Articles/ProjectConverter.htm

Unfortunately, the tool is written entirely in VB.Net which makes it difficult to work with if your preferred development language is C# (like me).

I used a tool called Instant C# from Tangible Software Solutions (http://tangiblesoftwaresolutions.com/Product_Details/Instant_CSharp.html) to convert the VB.Net source over to C#.





In addition, I heavily refactored the code to make it more flexible for future Visual Studio support and additional future features and capabilities:
http://vsprojectconverter.codeplex.com

Working with 3rd party assemblies in Sandboxed solutions

If you are attempting to utilize 3rd party assemblies in SharePoint Sandboxed Solutions, you may encounter error messages when attempting to deploy your SharePoint project.

The reason this does not work is because Sandboxed Solutions run in partial trust, therefore, any assemblies that you utilize need to support a partial trust model.

If you are fortunate enough to have the source code for the assembly, then you can add the following attribute to the AssemblyInfo.cs file (in the case of C# source code): [assembly:AllowPartiallyTrustedCallers].  You can read more about this attribute here:  http://support.microsoft.com/kb/839300
You can then re-compile the source code and use the resultant assembly within your Sandboxed Solution. 

NOTE: When you add the Reference to your Visual Studio project, make sure that the property for "Copy Local" is set to True.  This will force the assembly to be copied to your bin directory and thereby include it in the WSP (SharePoint Solution Package) for deployment to your Site Collection.

As you can probably guess, if you do not have the source code to the assembly and it is not already marked to support partial trust, you are out of luck and will have to deploy your solution as a Farm Level Solution instead.  This is extremely unfortunate since online hosted solutions of SharePoint such as SharePoint Online/Office 365 only support Sandboxed Solutions--thereby making more complex application development solutions impossible. 



Friday, October 7, 2011

ADFS v. 2.0 and Windows Server 2008 R2

If you need to configure ADFS v. 2.0 for use in Claims-based authentication scenarios, interestingly enough, ADFS v. 2.0 DOES NOT come pre-installed with Windows Server 2008 R2--even after the release of SP1.

Therefore, you will not be able to install ADFS as part of the Server Roles that come with Server Manager.  Instead, you will have to separately download the release of ADFS v. 2.0 and install and configure it separately.

You can download the release of ADFS v. 2.0 from here: http://www.microsoft.com/download/en/details.aspx?id=10909

There is also an update rollup for ADFS v. 2.0 available which can be downloaded from here: http://support.microsoft.com/kb/2607496

In addition, this is an excellent article on configuring ADFS v. 2.0: http://www.sysadminsblog.com/microsoft/installing-and-configuring-adfs-2-0/

Thursday, October 6, 2011

SQL Server 2008 R2 Express with SP1

If you are like me, you probably avoid installing service packs like you try to avoid the plague.  Therefore, whenever possible, I try to install "slipstreamed" or integrated products that already include the service pack or any necessary updates/patches etc. that are required for an installation.

Unfortunately, certain platforms such as SQL Server 2008 R2 Express with SP1 are not so easy to find through a Google search or even a Microsoft MSDN search.  If you look on the main Microsoft SQL Server Express page, there is no easy way to tell if you are downloading the RTM release of SQL Server 2008 R2 Express or the release that includes SP1: http://www.microsoft.com/sqlserver/en/us/editions/express.aspx

Therefore, for your convenience, I have included a link to the download for the SQL Server 2008 R2 Express with SP1 installation here: http://www.microsoft.com/download/en/details.aspx?id=26729

Installing and configuring ADFS v. 1.0

If you need to install and configure ADFS (Active Directory Federation Services) to use in scenarios such as Claims Based Authentication, you will need to know several things before installing and configuring ADFS.

  1. You will need to install and configure a Domain Controller/Active Directory prior to installing ADFS.  If you are simply setting up a test environment, you can install the domain controller with Active Directory on the same server as your ADFS installation.
  2. Active Directory requires SSL certificates to be installed in IIS in order to function properly.  When stepping through the ADFS installation wizard, you will have the option to create self-signed certificates. 
  3. After re-booting from an installation of ADFS, you will get an error message/error icon next to the ADFS role in Server Manager.  This is caused by the usage of the self-signed SSL certificates.  In order to resolve this issue, you will have to do the following:
    • Open up the IIS Manager console
    • Open up Server Certificates
    • For the individual Server Certificates that were created through the ADFS installation wizard, right click on the certificates and select View.
    • Once you are able to view the certificate, click on the Details tab.
    • On the Details tab, you should see a button on the bottom of the screen which states "Copy to File"
    • Click on the Copy to File button to export the server certificate.
    • Follow the wizard dialogs to export and save the server certificate.
    • Once both certificates have been successfully exported, you can right click on the certificates and select "Install Certificate"
    • When proceeding through the certificate installation dialogs, select the radio button for "Place all certificates in the following store"
    • You will then want to browse to the location of the "Trusted Root Certification Authorities"
    • Complete the Install Certificate wizard to store the server certificate in this location. 
    • Repeat this step for the other server certificate as well.
    • Once again re-boot the server.
    • When you open up Server Manager after the re-boot, the error message/error icon next to the ADFS role should have disappeared.
  4. Verify that you can now successfully access the following Url in a browser without getting a server certificate error message: https://<fully qualified server Url>/adfs/fs/federationserverservice.asmx
  5. If you are able to navigate to this Url and view the available methods on the Web Service, you have successfully configured ADFS for use!

Wednesday, October 5, 2011

Determining the underlying technology of a website

If you have ever looked at a website and wondered how it was built and which technology or set of technologies were used to build the website, a great site which decomposes the site and allows you to review many of these details is Built With.

You can visit the site here: http://builtwith.com

The nice thing about this tool is that you can enter any Url and it will not only decompose server side technologies such as ASP.Net, but it will even decompose such things as use of Content Management Systems (CMS) and even JavaScript libraries such as jQuery!!  Very cool stuff!