A following on Twitter

I’ve noticed that I have about double the number of followers than those that I am following. I’m not entirely sure what that means. I’m only following people that I’ve actually met. Does that mean I say interesting things, or are people just randomly following in the hope that I’ll follow them back. And if it is the latter then what do they get out of it? (Especially if I don’t know them)

If I look at most of the tweets that I make I would say that the majority would only be relevant to people that I know. Most of the rest are notifications of new blog posts and such (which can be received from an RSS feed anyway) So I wonder why others are following me.

So, a request: If you follow me on Twitter and I don’t (yet) follow you back, then would you be so kind as to let me know why? I’m always curious about these things. If you think I would benefit from following you in return then why should I reciprocate?

Don’t know what I’m talking about? http://twitter.com/ColinMackay

Technorati Tags: ,

I've been podcasted!

Oh no! I’ve been podcasted. Craig Murphy caught up with me at SQL Bits and demanded to know what I thought of the sessions that I attended. I’m not sure if I really am that repetitious of if it was just that we stopped and started recording a few times because the background noise got a bit excessive at times and I had to restart what I was saying.

Just to make sure I got something out of it too I told him about two SQL Events coming up here in Scotland. On the 24th October there is a talk about SQL Backups (and I’m doing a micro-presentation on SQL Injection Attacks). Then on the 14th November there are two talks about different aspects of SQL Server. The first is the start on a series on Reporting Services, this is the “Introduction to Reporting Services”. The second in a talk about SQL Server Managed Objects (SMOs). All of the above events are free.

You can download the podcast from Craig Murphy’s blog.

Recruitment Session at DDD6

Just some more information for any of those that might be interested in my session at DDD6.

A panel of experts will answer the audiences questions on the subject of recruitment chaired by Colin Mackay. This session is for you if you are looking for a job and want to find out what companies are looking for. You are trying to hire someone but don’t know what to look for. You have been burned in the past and don’t want to repeat the experience. You have a question about recruitment no one else seems to have an answer to.

There will be a question box on the day for you to post your questions, but if you want to email in advance, you can email your questions to colin@scottishdevelopers.com. Submissions by email close at 23:59 on the 23rd November.

The panel is:

Sarah Blow: Founder of Girl Geek Dinners and Software Engineer.  She has grown up around technology and HR with parents in both industry sectors and works closely with Women & Technology and companies to create a better understanding about how women may be recruited in different ways to men.

Barry Dorrans: Senior consultant for a major consultancy and Microsoft MVP. When interviewing he takes the red pen and becomes intellisense for whiteboards.

Frank Kerrigan: Development team lead for an insurance company in Glasgow. His team writes enterprise applications for internal customers using C# and SQL Server. Frank has been in devolvement and support of enterprise systems for 15 years.

Karl Lightfoot: Recruitment Consultant that for the last 9 years day in day out has only been recruiting developers in the Midlands region for the largest IT Recruitment consultancy in the UK. He has successfully recruited for large and small corporations through different market states of the supply / demand of talented Developers from Visual Basic 3 to .Net 2005.

What is a DAL (Part 4)

As has been mentioned previously, one of the purposes of the DAL is to shield that application from the database. That said, what happens if a DAL throws an exception? How should the application respond to it? In fact, how can it respond to an exception that it should not know about?

If something goes wrong with a query in the database an exception is thrown. If the database is SQL Server then a SqlException is thrown. If it isn’t SQL Server then some other exception is thrown. Or the DAL may be performing actions against a completely different type of data source such as an XML file, plain text file, web service or something completely different. If the application knows nothing about the back end database (data source) then how does it know which exception to respond to?

In short, it doesn’t. It can’t know which of the myriad of possible exceptions that could be thrown will be and how to respond to it. The calling code could just catch(Exception ex) but that is poor practice. It is always best to catch the most specific exception possible.

The answer is to create a specific exception that the DAL can use. A DalException that calling code can use. The original exception is still available as an InnerException on the DalException.

using System;
using System.Runtime.Serialization;

namespace Dal
{
    public class DalException : Exception
    {
        public DalException()
            : base()
        {
        }

        public DalException(string message)
            : base(message)
        {
        }

        public DalException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        public DalException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
    }
}

The DAL will catch the original exception, create a new one based on the original and throw the new exception.

public DataSet GetPolicy(int policyId)
{
    try
    {
        SqlDataAdapter da =
            (SqlDataAdapter)this.BuildBasicQuery("GetPolicy");
        da.SelectCommand.Parameters.AddWithValue("@id", policyId);
        DataSet result = new DataSet();
        da.Fill(result);
        return result;
    }
    catch (SqlException sqlEx)
    {
        DalException dalEx = BuildDalEx(sqlEx);
        throw dalEx;
    }
}

The code for wrapping the original exception in the DAL Exception can be refactored in to a separate method so it can be used repeatedly. Depending on what it needs to do it may be possible to put that as a protected method on one of the abstract base classes

private DalException BuildDalEx(SqlException sqlEx)
{
    string message = string.Format("An exception occured in the Policy DALrn" +
        "Message: {0}", sqlEx.Message);
    DalException result = new DalException(message, sqlEx);
    return result;
}

Previous articles in the series:

 

 

Technorati Tags: , ,

 

What is a DAL (Part 3)

In this continuation of my series on the DAL I’m going to show the ability to create several DALs and have a Factory class instantiate the correct DAL based on settings in a config file.

One of the purposes of the DAL is to shield the application from the detail of accessing the database. You may have a system where you may have to talk to different databases depending on the way the software is installed. Typically this is most useful by creators of components that need to plug into many different database systems.

The DAL will no longer be a singleton, but rely on a Factory to create the correct DAL class. This is most useful when the data may be coming from different sources. It can also be useful when your application is in development and the database isn’t ready because you can then create a mock DAL and return that instead of the real thing.

UML Diagram showing multiple DALs

UML Diagram showing multiple DALs

The above UML Diagram shows the base DAL that was introduced in the last article in this series. Again, it is an abstract class as it does not contain enough functionality to be instantiated on its own. However, it is now joined by two additional abstract classes. These add a bit more functionality specific to the target database, but still not enough to be useful as an object in its own right.

Finally at the bottom layer are the various concrete DAL classes that act as the proxies to the database.

So that the calling application doesn’t need to know what database it is dealing with the concrete classes implement interfaces. That way the application receives a reference to an IPolicyDal, for example, without having to know whether it is connected to an Oracle database or a SQL Server database (or any other future database you may need to support)

The factory class, not shown in the UML diagram above, picks up information in a config file which tells it which specialised DAL to actually create. It returns that specialised instance through a reference to the interface. This means that the calling code does not need to know what specialised class has actually been created. Nor should it need to care.

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Dal
{
    /// <summary>
    /// Creates and dispenses the correct DAL depending on the configuration
    /// </summary>
    public class DalFactory
    {
        private static IPolicyDal thePolicyDal = null;
        private static IClaimDal theClaimDal = null;

        /// <summary>
        /// Gets the policy DAL
        /// </summary>
        /// <returns>A dal that implements IPolicyDal</returns>
        public static IPolicyDal GetPolicyDal()
        {
            if (thePolicyDal == null)
            {
                string policyDalName = DalConfig.PolicyDal;
                Type policyDalType = Type.GetType(policyDalName);
                thePolicyDal =
                    (IPolicyDal)Activator.CreateInstance(policyDalType);
            }
            return thePolicyDal;
        }

        /// <summary>
        /// Gets the claim DAL
        /// </summary>
        /// <returns>A DAL that implements IClaimDal</returns>
        public static IClaimDal GetClaimDal()
        {
            if (theClaimDal == null)
            {
                string claimDalName = DalConfig.ClaimDal;
                Type claimDalType = Type.GetType(claimDalName);
                theClaimDal =
                    (IClaimDal)Activator.CreateInstance(claimDalType);
            }
            return theClaimDal;
        }
    }
}

The factory stores the each DAL class as it is created so that if a Get...() method is called repeatedly it always returns the same instance. The DAL classes should not be storing any state so this is a safe thing to do.

The DalConfig class is a very simple static class that consists of a number of property getters that retrieves the information out of the config file. The PolicyDal and ClaimDal properties return the fully qualified name of the specialised DAL class to instantiate.

Previous articles in the series:

Technorati Tags: ,

SQLBits

I haven’t said much about SQLBits, a community conference that I was at last weekend in Reading. It was a brilliant day out and I hardly spent anything. The snacks and lunch were free at the venue (Microsoft Campus) and the “GROUP BY” (Geek Dinner) was sponsored so it didn’t cost anything either.

A big thank you has to be said to Tony Rogerson, Simon Sabin, Martin Bell and all the other organisers that helped make it such a fantastic day.

Here are some photos of from SQL Bits that were taken by Craig Murphy:

Pre-SQLBits dinner - Simon Sabin and me
Simon Sabin and Colin Mackay

Pre-SQLBits dinner - Martin Bell and me
Colin Mackay and Martin Bell

Pre-SQLBits dinner - Martin Bell and me
Colin Mackay and Martin Bell

At the GROUP BY event after SQLBits
Tim Leung, Colin Mackay, Martin Cairney, Tony Rogerson

sql-bits group by group hug
Group hug at Group By

SQLBits - I was the room monitor
Me as a room monitor in Chicago 2

Scottish SQL Server User Group Meeting

When: 24th October at 18:30 until 21:00

Where: HBOS’ offices on Bankhead Crossway North, Sighthill Industrial Estate, EH11 4DT.

What: Two speakers – Dave Johnston of HBOS will talk about backup solutions for SQL Server. Colin Mackay will deliver a micro-presentation on SQL Injection Attacks.

Registration: For security a delegate list needs to be submitted in advance. See this event page for details.

Anything else? There will be refreshments and sandwiches.