Is this Repository Design Pattern Valid and efficient? I am using Dapper ORM in my Data access Layer, but I think analysis of the code below doesn't depend upon any specific ORM.  What should be the single common place throughout the entire Data Access Layer to initialise the Connection String?

Base Abstract Class

abstract class Repository
    {
        protected static IDbConnection _db = new SqlConnection(ConfigurationManager.ConnectionStrings["GuideDB"].ConnectionString);

      //should I use Constructor here
    }


A Generic Repository Interface

public interface IRepository&lt;T&gt;
    {
        IEnumerable&lt;T&gt; GetAll();
        T GetItem(int id);
        T Create(T item);
        bool Update(T item);
        bool Delete(int id);
        void Reset();
    }


Concrete class

public class StateRepository :Repository, IRepository&lt;State&gt; 
    {

        private static ICollection&lt;State&gt; _states = PopulateStates();// doing this valid ??
        private static object _lock = new object();

        #region Public Methods

        public IEnumerable&lt;State&gt; GetAll()
        {
            lock (_lock)
            {
                return _states.ToArray();
            }
        }

        public IEnumerable&lt;State&gt; GetAllStatesByCountryId(short id)
        {
            lock (_lock)
            {
                return _states.Where(s =&gt; s.CountryId == id).ToArray();
            }
        }

        public State GetItem(int id)
        {
            lock (_lock)
            {
                return _states.FirstOrDefault(s =&gt; s.Id == id);
            }
        }

        public State Create(State state)
        {
            lock (_lock)
            {
                if (state == null)
                {
                    throw new ArgumentNullException("state");
                }

                state.Id = (short)(_states.Any() ? _states.Max(s =&gt; s.Id) + 1 : 1);
                _states.Add(state);
                return state;
            }
        }

        public bool Update(State item)
        {
            lock (_lock)
            {
                var state = _states.FirstOrDefault(s =&gt; s.Id == item.Id);

                if (state != null)
                {
                    state.Id = item.Id;
                    state.Name = item.Name;
                    state.ShortInfo = item.ShortInfo;
                    state.CountryId = item.CountryId;

                    return true;
                }

                return false;
            }
        }

        public bool Delete(int id)
        {
            lock (_lock)
            {
                var state = _states.FirstOrDefault(s =&gt; s.Id == id);
                return _states.Remove(state);
            }
        }

        public void Reset()
        {
            lock (_lock)
            {
                _states = PopulateStates(); // doing this valid ??
            }
        }
        #endregion

        private static ICollection&lt;State&gt; PopulateStates()
        {
            return  _db.Query&lt;State&gt;("SELECT * FROM States").ToList(); //Dapper ORM Code
        }

       //How to Add a efficient Dispose method to this repository , is it really needed
    }
I think your abstract class should "implement" the IRepository interface, because otherwise you will always have to put both the interface and the base class in your inheritance list. Also, I think you should name your Repository -> SqlRepository, since all the code in the base class is coupled to Sql. As @Heslacher pointed out, you should inject your connection, because keeping the static connection might cause problems. What would happen if one of my Repository implementation would dispose the connection? All your repositories would be down.

abstract class SqlRepository&lt;T&gt; : IRepository&lt;T&gt;
{
    protected IDbConnection Connection { get; set; }

    protected SqlRepository&lt;T&gt;(string connectionString)
    {
        Connection = new SqlConnection(connectionString);
    }

    abstract IEnumerable&lt;T&gt; GetAll();
    abstract T GetItem(int id);
    abstract T Create(T item);
    abstract bool Update(T item);
    abstract bool Delete(int id);
    abstract void Reset();
}


If your application runs on one instance, your repository would be okay. But since it puts all database entities in cache (your collection) and never returns to the database unless you specify it, you have a problem. Example : Client A (On an X machine) loads the database in cache, Client B (on a Y machine) does the same then adds a State to the repository. Client A calls GetAll, what should be the expected output? Client A wouldn't have the new State. And if Client B calls Reset the new State was never persisted, so it is lost.

I don't think you should use the ICollection&lt;State&gt;, each of your call should go to the database, so you are sure you have the latest version of your data. If you remove it, I don't think you need to handle thread safety anymore.