title: Repository and Data Mapper pattern
body: <p>After a lots of read about Repository and Data Mapper I decided to implement those patterns in a test project. Since I'm new to these I'd like to get your views about how did I implement those in a simple project. </p>
  
  <p>Jeremy Miller says :</p>
  
  <blockquote>
    <p>Do some sort of nontrivial, personal coding project where you can freely experiment with design patterns.</p>
  </blockquote>
  
  <p>But I don't know I did all this things right or not.</p>
  
  <p>Here is my project structure :</p>
  
  <p><img src="https://i.stack.imgur.com/9x0rV.png" alt="enter image description here"></p>
  
  <p>As you can see there are many folders which I'm going to describe them in detail in below.</p>
  
  <ul>
  <li><p>Domain : Project Domain Entities go here I've a simple Personnel class which is inherited from EntityBase class, EntityBase class has a single property named Id.</p>
  
  <pre><code>public int Id { get; set; }
  </code></pre></li>
  <li><p>Infrustructure : Here is a simple Data Access Layer with two classes. SqlDataLayer is a simple class which is inherited from an abstract class named DataLayer. Here I provide some functionality like following code :</p>
  
  <pre><code>public SQLDataLayer() {
      const string connString = "ConnectionString goes here";
      _connection = new SqlConnection(connString);
      _command = _connection.CreateCommand();
  }
  </code></pre></li>
  </ul>
  
  <p>adding parameter to commands parameter collection :</p>
  
  <pre><code>    public override void AddParameter(string key, string value) {
          var parameter = _command.CreateParameter();
          parameter.Value = value;
          parameter.ParameterName = key;
  
          _command.Parameters.Add(parameter);
      }
  </code></pre>
  
  <p>executing DataReader :</p>
  
  <pre><code>    public override IDataReader ExecuteReader() {
          if (_connection.State == ConnectionState.Closed)
              _connection.Open();
  
          return _command.ExecuteReader();
      }
  </code></pre>
  
  <p>and so on.</p>
  
  <ul>
  <li>Repository : Here I tried to implement repository pattern. IRepository is a generic interface </li>
  </ul>
  
  <p>IRepository.cs :</p>
  
  <pre><code>public interface IRepository&lt;TEntity&gt; where TEntity : EntityBase
  {
      DataLayer Context { get; }
  
      TEntity FindOne(int id);
      ICollection&lt;TEntity&gt; FindAll();
  
      void Delete(TEntity entity);
      void Insert(TEntity entity);
      void Update(TEntity entity);
  }
  </code></pre>
  
  <p>Repository.cs :</p>
  
  <pre><code>public class Repository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : EntityBase, new() {
      private readonly DataLayer _domainContext;
      private readonly DataMapper&lt;TEntity&gt; _dataMapper;
      public Repository(DataLayer domainContext, DataMapper&lt;TEntity&gt; dataMapper) {
          _domainContext = domainContext;
          _dataMapper = dataMapper;
      }
      public DataLayer Context {
          get { return _domainContext; }
      }
      public TEntity FindOne(int id)
      {
          var commandText = AutoCommand.CommandTextBuilder&lt;TEntity&gt;(CommandType.StoredProcedure, MethodType.FindOne);
  
          // Initialize parameter and their types
          Context.AddParameter("Id", id.ToString(CultureInfo.InvariantCulture));
          Context.SetCommandType(CommandType.StoredProcedure);
          Context.SetCommandText(commandText);
  
          var dbReader = Context.ExecuteReader();
          return dbReader.Read() ? _dataMapper.Map(dbReader) : null;
      }
  </code></pre>
  
  <p>I didn't expose not implemented methods from IRepository.</p>
  
  <p>Here in Generic Repository class I expect two parameters in constructor first is a reference to my SqlDataLayer class and second is a reference to Entity DataMapper.
  Those parameters sent by each Entities Repository class which inherited from Repository class. for example :</p>
  
  <pre><code>public class PersonnelRepository : Repository&lt;Personnel&gt;, IPersonnelRepository {
      public PersonnelRepository(DataLayer domainContext, PersonnelDataMapper dataMapper)
          : base(domainContext, dataMapper) {
  
      }
  }
  </code></pre>
  
  <p>As you can see here in the FindOne method I tried to automate some operation such as creating CommandText, then I took the advantage of my DataLayer class to configure command and finally execute command to get IDataReader. I pass IDataReader to my DataMapper class to map to the Entity. </p>
  
  <ul>
  <li><p>DomainMapper : Finally here I map result of IDataReader to Entities, bellow is a sample of how I map Personnel entity :</p>
  
  <pre><code>public class PersonnelDataMapper : DataMapper&lt;Personnel&gt; {
  public override Personnel Map(IDataRecord record) {
      return new Personnel {
          FirstName = record["FirstName"].ToString(),
          LastName = record["LastName"].ToString(),
          Address = record["Address"].ToString(),
          Id = Convert.ToInt32(record["Id"])
      };
  }}
  </code></pre></li>
  </ul>
  
  <p><strong>Usage :</strong></p>
  
  <pre><code>    using (var context = new SQLDataLayer()) {
          _personnelRepository = new PersonnelRepository(context, new PersonnelDataMapper());
              var personnel  = _personnelRepository.FindOne(1);
      }
  </code></pre>
  
  <p>I know I did many mistake here, that's why I'm here. I need your advice to know what I did wrong or what are the good points in this simple test project.</p>
  
  <p>Thanks in advance.</p>