title: Azure table storage using repository pattern and unit of work pattern
body: <p>I'm trying to build a reusable code for accessing Azure Table Storage using repository pattern and unit of work pattern.</p>
  
  <p>What I want to achieve is to instantiate an unit of work and then use that for different repositories.</p>
  
  <p>Something like this:</p>
  
  <pre><code>MyUnitOfWork uow = new MyUnitOfWork();
  uow.Users.Add(User user);
  uow.Users.GetAllUsers();
  uow.Users.FindUser(User user);
  uow.Users.Delete(User user);
  .......
  uow.Products.Add(Product product);
  uow.Products.Delete(Product product);
  .......
  uow.Invoices.Add(Invoice invoice);
  .......
  uow.Save();
  </code></pre>
  
  <p>I have some issues understanding how to implement this using TableServiceContext</p>
  
  <p>Here is what I did so far ...</p>
  
  <p>The model:</p>
  
  <pre><code>   public class User : TableServiceEntity
     {
      public User(string partitionKey, string rowKey)
          : base(partitionKey, rowKey)
      {
      }
  
      public User()
          : this(Guid.NewGuid().ToString(), String.Empty)
      {
      }
  
      public string UserName { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      }
  </code></pre>
  
  <p>IRepository Interface:      </p>
  
  <pre><code>  public interface IRepository&lt;T&gt; where T : TableServiceEntity
    {
      IQueryable&lt;T&gt; Query { get; }
  
      //bool CreateIfNotExist();
  
      //bool DeleteIfExist();
  
      //void AddEntity(T obj);
  
      //void AddEntity(IEnumerable&lt;T&gt; objs);
  
      //void AddOrUpdateEntity(T obj);
  
      //void AddOrUpdateEntity(IEnumerable&lt;T&gt; objs);
  
      //void DeleteEntity(T obj);
  
      //void DeleteEntity(IEnumerable&lt;T&gt; objs);
      }
  </code></pre>
  
  <p>Interface Unit Of Work:</p>
  
  <pre><code>    public interface ITableUow
      {
      void Save();
  
      IUserRepository Users { get; }
      }
  </code></pre>
  
  <p>Interface User Repository:</p>
  
  <pre><code>    public interface IUserRepository: IRepository&lt;User&gt;
      {
      IQueryable&lt;User&gt; GetUsers();
      }
  </code></pre>
  
  <p>Azure Table Repository:</p>
  
  <pre><code>     public class AzureTableRepository&lt;T&gt; : IRepository&lt;T&gt; where T : TableServiceEntity
      {
      public AzureTableRepository(TableServiceContext context)
      {
          if (context != null)
          {
              TableContext = context;
          }
      }
  
      protected TableServiceContext  TableContext { get; set;    }
  
      public IQueryable&lt;T&gt; Query
      {
          get { throw new NotImplementedException(); }
      }
  
      }
  </code></pre>
  
  <p>User Repository:</p>
  
  <pre><code>    public class UserRepository : AzureTableRepository&lt;User&gt;, IUserRepository
      {
      public UserRepository(TableServiceContext context)
          : base(context)
      {
  
      }
  
  
      public IQueryable&lt;User&gt; GetUsers()
      {
          throw new NotImplementedException();
      }
      }
  </code></pre>
  
  <p>Azure Table Unit Of Work:</p>
  
  <pre><code>     public class AzureTableUow: ITableUow
      {
  
      public void Save()
      {
  
      }
  
      public IUserRepository Users
      {
          get { throw new NotImplementedException(); }
      }
      }
  </code></pre>