Tulsa DNUG - Agile Code

by October 24, 2011 01:00 PM

My Tulsa DNUG presentation description:

From the Agile Manifesto: Responding to change over following a plan. From the manifesto's 12 principles: Welcome changing requirements, even late in development and Continuous attention to technical excellence and good design enhances agility.

Code that works to enable those three statements is code that I refer to as agile code. We'll talk about writing that kind of code by working through a simple example pulled from the book Agile Principles, Patterns, and Practices in C# by Robert Martin.

Like my last presentation, I burned through this lightning fast. And once again, people started opening up and turned the night into a great meeting. I'm not too surprised since it seems any topic that has anything at all to do with Agile brings out passion in people. It was a fun group and I'm glad they invited me over.

Oh, and what was my favorite quote of the night? "You can't cure stupid". Pure awesome!

See the code I used in the presentation here.

Tags: , ,

Fort Smith DNUG - SOA For The Developer

by October 10, 2011 01:00 PM

My Fort Smith DNUG presentation description:

Service Oriented Architecture has been a buzz word for a while now and gets talked about all the time. However, I see very few presentations targeting the developer and what he/she should be doing in code as an SOA advocate. In this presentation we surface an old and well known design pattern called the Gateway. When developing applications that talk to other applications (or pull data straight out of their databases!), this pattern can save your bacon at worst and at best will give you a clear and easy to see delineation between your app and external systems.

I actually burned through the presentation rather quickly and for a moment thought the whole thing was going to be a dud. And then people started opening up with comments and questions and I'd say the night turned into a huge success. Awesome group to present in front of. Thanks for the invite!

See the code I used in the presentation here.

Tags: , ,

In Memory Repository

by July 13, 2011 04:28 AM

I mentioned to a buddy the in memory repository I've used on a few projects. He asked me to share so here it is...

Two things to note. The Entity base class has a single property on it called Id. And remember that the InMemoryRepository isn't transactional so everything done is immediate and submitting changes or rolling back on the unit of work has no effect. You could certainly add this behavior of course, but as-is doesn't bother with it.

public interface IRepository
{
  T Get<T>(int id) where T : Entity;
  T Get<T>(Expression<Func<T, bool>> predicate) where T : Entity;

  IQueryable<T> Find<T>() where T : Entity;
  IQueryable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : Entity;

  T Add<T>(T entity) where T : Entity;
  T Remove<T>(T entity) where T : Entity;
}

public class InMemoryRepository : IRepository
{
  public InMemoryRepository()
  {
  }

  public InMemoryRepository(bool clear)
  {
    if (clear)
      _lists.Clear();
  }

  public T Get<T>(int id) where T : Entity
  {
    return Get<T>(x => x.Id == id);
  }

  public T Get<T>(Expression<Func<T, bool>> predicate) where T : Entity
  {
    return Find<T>().SingleOrDefault(predicate);
  }

  public IQueryable<T> Find<T>() where T : Entity
  {
    return ListFor<T>().AsQueryable();
  }

  public IQueryable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : Entity
  {
    return Find<T>().Where(predicate);
  }

  public T Add<T>(T entity) where T : Entity
  {
    ListFor<T>().Add(entity);
    return entity;
  }

  public T Remove<T>(T entity) where T : Entity
  {
    ListFor<T>().Remove(entity);
    return entity;
  }

  /**********************************************************************/
  /**********************************************************************/

  private static readonly IDictionary<Type, object> _lists = new Dictionary<Type, object>();
  private static readonly object _lockObject = new object();

  private static IList<T> ListFor<T>()
  {
    var type = typeof(T);

    if (!_lists.ContainsKey(type))
    {
      lock (_lockObject)
      {
        if (!_lists.ContainsKey(type))
        {
          Type listType = typeof(List<>).MakeGenericType(type);
          _lists[type] = Activator.CreateInstance(listType);
        }
      }
    }

    return (IList<T>)_lists[type];
  }
}

Tags:

Equality Members

by June 16, 2011 06:48 AM

I was watching a presentation today and noticed an implementation of GetHashCode() that was... less than perfect :)

For those without the luxury of Resharper, I thought I'd create a few examples of implementing the equality members. For a refresher on defining equality, see this MSDN article. Note that, while technically optional, it is recommended to overload the == and != operators too.

public class IntPrimaryKey : IEquatable<IntPrimaryKey>
{
    public int PrimaryKey { get; set; }

    public bool Equals(IntPrimaryKey other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return PrimaryKey == other.PrimaryKey;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as IntPrimaryKey);
    }

    public override int GetHashCode()
    {
        return PrimaryKey;
    }
}

public class StringPrimaryKey : IEquatable<StringPrimaryKey>
{
    public string PrimaryKey { get; set; }

    public bool Equals(StringPrimaryKey other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(PrimaryKey, other.PrimaryKey);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as StringPrimaryKey);
    }

    public override int GetHashCode()
    {
        return PrimaryKey == null ? 0 : PrimaryKey.GetHashCode();
    }
}

public class MultiNumberPrimaryKey : IEquatable<MultiNumberPrimaryKey>
{
    public int PrimaryKey1 { get; set; }
    public int PrimaryKey2 { get; set; }

    public bool Equals(MultiNumberPrimaryKey other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return PrimaryKey1 == other.PrimaryKey1 && PrimaryKey2 == other.PrimaryKey2;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as MultiNumberPrimaryKey);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return (PrimaryKey1 * 397) ^ PrimaryKey2;
        }
    }
}

public class MultiStringPrimaryKey : IEquatable<MultiStringPrimaryKey>
{
    public string PrimaryKey1 { get; set; }
    public string PrimaryKey2 { get; set; }

    public bool Equals(MultiStringPrimaryKey other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(PrimaryKey1, other.PrimaryKey1) && Equals(PrimaryKey2, other.PrimaryKey2);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as MultiStringPrimaryKey);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return ((PrimaryKey1 == null ? 0 : PrimaryKey1.GetHashCode()) * 397) ^ (PrimaryKey2 == null ? 0 : PrimaryKey2.GetHashCode());
        }
    }
}

public class MultiMixedPrimaryKey : IEquatable<MultiMixedPrimaryKey>
{
    public int PrimaryKey1 { get; set; }
    public string PrimaryKey2 { get; set; }

    public bool Equals(MultiMixedPrimaryKey other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return PrimaryKey1 == other.PrimaryKey1 && Equals(PrimaryKey2, other.PrimaryKey2);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as MultiMixedPrimaryKey);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return (PrimaryKey1 * 397) ^ (PrimaryKey2 == null ? 0 : PrimaryKey2.GetHashCode());
        }
    }
}

public class MultiPrimaryKey : IEquatable<MultiPrimaryKey>
{
    public int PrimaryKey1 { get; set; }
    public int PrimaryKey2 { get; set; }
    public int PrimaryKey3 { get; set; }
    public int PrimaryKey4 { get; set; }

    public bool Equals(MultiPrimaryKey other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return PrimaryKey1 == other.PrimaryKey1 && PrimaryKey2 == other.PrimaryKey2 && PrimaryKey3 == other.PrimaryKey3 && PrimaryKey4 == other.PrimaryKey4;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as MultiPrimaryKey);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = PrimaryKey1;
            result = (result * 397) ^ PrimaryKey2;
            result = (result * 397) ^ PrimaryKey3;
            result = (result * 397) ^ PrimaryKey4;
            return result;
        }
    }
}

public class MultiPrimaryKey2 : IEquatable<MultiPrimaryKey2>
{
    public string PrimaryKey1 { get; set; }
    public string PrimaryKey2 { get; set; }
    public string PrimaryKey3 { get; set; }
    public string PrimaryKey4 { get; set; }

    public bool Equals(MultiPrimaryKey2 other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(PrimaryKey1, other.PrimaryKey1) && Equals(PrimaryKey2, other.PrimaryKey2) && Equals(PrimaryKey3, other.PrimaryKey3) && Equals(PrimaryKey4, other.PrimaryKey4);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as MultiPrimaryKey2);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = PrimaryKey1 == null ? 0 : PrimaryKey1.GetHashCode();
            result = (result * 397) ^ (PrimaryKey2 == null ? 0 : PrimaryKey2.GetHashCode());
            result = (result * 397) ^ (PrimaryKey3 == null ? 0 : PrimaryKey3.GetHashCode());
            result = (result * 397) ^ (PrimaryKey4 == null ? 0 : PrimaryKey4.GetHashCode());
            return result;
        }
    }
}

Tags: ,

NWA TechFest 2011 - LINQ

by March 28, 2011 11:04 AM

This was my session along with its extract from NWA TechFest 2011:

LINQ and Extension Methods

LINQ has been around for a while now. I heard just the other day, "I think I'm going to start really using LINQ, it seems kind of cool." I almost passed out! So for all of those just now getting acquainted with LINQ, this is the session for you. I'm going to move quickly in the beginning to get out those few things that will make using it all the easier from now on. Then we'll fill whatever remaining time we have with a few advanced queries and some must-have extension methods you'll want to include in all of your projects.

I would venture to say it went great. I got some great feedback and the audience was extremely interactive which is always awesome. Especially loved when we got to the point where I talked about the common extension methods I use in all of my projects. I got a few raised eyebrows and a few looks that made it apparent the person thought I was nuts. Good times!

As promised, here is the source from the session: 2011-03-25_Techfest.zip. And yes, all of the extension methods are in there :)

Happy coding

Tags: ,