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.
I would start off with the following. Define the Database and Mapping functionality in separate interfaces and inject them into the repository, this way the repository will be easier to test. Using this method you should be able to extend the repository to include other CRUD operations.

One problem with this approach is the separation between the mapper and the creation of the SqlCommand, it may not be very obvious which columns are returned via the select statement. 

```
// The concrete implementation of this interface will handle connections to the
// database
public interface IDatabase
{
    SqlDataReader ExecuteQuery(SqlCommand command);
}

public interface IEntityMapper<T>
{
    T MapAll(SqlDataReader reader);
}

public abstract class EntityRepository<T>
{
    private readonly IDatabase _database;
    private readonly IEntityMapper<T> _mapper;

    protected EntityRepository(IEntityMapper<T> mapper, IDatabase database)
    {
        _mapper = mapper;
        _database = database;
    }

    public T Get(int id)
    {
        return this.Get(_mapper, _database, id);
    }

    protected virtual T Get(IEntityMapper<T> mapper, IDatabase database, int id)
    {
        // Create a command can be used to fetch the entity, remember to dispose when complete
        using (var cmd = this.CreateGetCommand(id))
        {
            using (var reader = database.ExecuteQuery(cmd))
            {
                // No need to read all the rows, just the first...
                return reader.Read() ? mapper.MapAll(reader) : default(T);
            }
        }
    }

    protected abstract SqlCommand CreateGetCommand(int id);
}

```

And implement the following

```
public class OrganisationEntityRepository : EntityRepository<OrganisationEntity>
{
    public OrganisationEntityRepository(IEntityMapper<OrganisationEntity> mapper, IDatabase database) : base(mapper, database)
    {
    }

    protected override SqlCommand CreateGetCommand(int id)
    {
        var command = new SqlCommand(@"SELECT t.Id, t.Description FROM Organisation t Where t.Id = @Id");
        command.Parameters.Add("@id", SqlDbType.Int).Value = id;

        return command;
    }
}

public class OrganisationEntityMapper : IEntityMapper<OrganisationEntity>
{
    public OrganisationEntity MapAll(SqlDataReader reader)
    {
        return new OrganisationEntity();  // Populate using the reader...
    }
}

```