Question: (sorry for my bad english) I'm writing an implementation of Dijkstra algoritm and I need to use a priority queue.
I use PriorityQueue as definited in Java Platform SE 6.
There is a way o a method like Q.update() in Java Platform SE 5 that rebuild priority queue in case the priorities of its elements have changed since they were inserted? (I have problem with relax and Q.poll())
I need that the update takes O(log n)

Answer: 
Your "next step" would not be the same as mine.

My next step would be to find another example of this "common code" you're trying to refactor. Perhaps a "`CustomerEntity.Get(int id)'" method.

Now, let's pretend the only difference between the CustomerEntity and OrganisationEntity versions are the query string and the replacement of the term "Organisation" with "Customer". My next step would be to try to make the two methods more and more identical. Assuming this method is part of an OrganisationEntityRepository class, I'd refactor that towards an EntityRepository1 class, and the CustomerEntityRepository towards EntityRepository2.

Steps 1 would be to introduce a generic parameter for the type of the entity. You'll have to do the same for the OrganisationEntityMapper and CustomerEntityMapper classes.

Next, go back and look at what's still different. I see they use different mapper classes, so let's make the mapper type generic. In order to do that and still reference the MapAll method, I'll introduce an IMapper interface with the MapAll method, and have my two concrete mapper classes implement that.

Now, the next big difference is the query. I'll put that into a virtual "CommandText" property.

Now I think I'm ready for a base class, perhaps `EntityRepositoryBase<TEntity,TMapper>`. With suitable assumptions, I wind up with the following:

```
public abstract class EntityRepositoryBase<TEntity, TMapper>
    where TMapper : IMapper<TEntity>
{
    public virtual TEntity Get(int id)
    {
        List<TEntity> entities;
        using (var command = new SqlCommand {CommandText = CommandText})
        {
            command.Parameters.Add("@id", SqlDbType.Int).Value = id;

            entities = new List<TEntity>();

            using (var reader = Database.ExecuteQuery(command, ConnectionName.Dev))
            {
                while (reader.Read())
                {
                    var mapper = Mapper;
                    entities = mapper.MapAll(reader);
                }
            }
        }

        return entities.First();
    }

    protected abstract string CommandText { get; }
    protected abstract TMapper Mapper { get; }
}

public class OrganisationEntityRepository :
    EntityRepositoryBase<OrganisationEntity, OrganisationEntityMapper<OrganisationEntity>>
{
    protected override string CommandText
    {
        get { return @"SELECT t.Id, t.Description FROM Organisation t Where t.Id = @Id"; }
    }

    protected override OrganisationEntityMapper<OrganisationEntity> Mapper
    {
        get { throw new NotImplementedException(); }
    }
}

public class CustomerEntityRepository : EntityRepositoryBase<CustomerEntity, CustomerEntityMapper<CustomerEntity>>
{
    protected override string CommandText
    {
        get { return @"SELECT t.Id, t.Description FROM Customer t Where t.Id = @Id"; }
    }

    protected override CustomerEntityMapper<CustomerEntity> Mapper
    {
        get { throw new NotImplementedException(); }
    }
}

```

---

And, needless to say, though I'll say it anyway: props to JetBrains ReSharper 5.1 for doing all the moving around of things, so I didn't have to.
Here is how I would refactor it:

```
public class Repository<T> : IRepository<T> where T : class, new()
{
    private IEntityMapper<T> _mapper;

    public Repository(IEntityMapper<T> mapper)
    {
        _mapper = mapper;
    }

    public virtual T Find(string value)
    {
        SqlCommand command = new SqlCommand();
        command.CommandText = @"SELECT t.Id, t.Description FROM Organisation t Where t.Description LIKE @value";
        command.Parameters.Add("@value").Value = value + "%";

        SqlDataReader reader = Database.ExecuteQuery(command, ConnectionName.Dev);
        return FillCollection(reader);
    }

    public void T Get(int id)
    {
        SqlCommand command = new SqlCommand();
        command.CommandText = @"SELECT t.Id, t.Description FROM Organisation t Where t.id = @value";
        command.Parameters.Add("@value").Value = id;

        SqlDataReader reader = Database.ExecuteQuery(command, ConnectionName.Dev);
        if (!reader.Read())
            return null;

        T entity = new T();
        _mapper.Map(entity, reader);
        return entity;
    }

    protected IList<T> FillCollection(IDataReader reader)
    {
        List<T> items = new List<T>();
        while (reader.Read())
        {
            T entity = new T();
            _mapper.Map(entity, reader);
            _items.Add(entity);
        }
        return items;
    }
}

public interface IEntityMapper<T>
{
    //row is the most generic part of a DataReader
    void Map(T entity, IDataRow row);
}

```

Keypoints:

1. Created a base class with FillCollection as a protected method.
2. Forced entity to be a class with a default constructor to speed up object creation.
3. Using a interface for the mapper and taking it in the constructor.
4. I would not fill an entire collection to just get the first item. waste of cpu.
5. Try to use as generic interfaces as possible, as `IDataRecord` instead of `DbDataReader`