title: Using Simple Injector with Unit Of Work & Repository Pattern in Windows Form
body: <p>I'm trying to implement IoC in my windows form application. My choice fell on Simple Injector, because it's fast and lightweight. I also implement unit of work and repository pattern in my apps. Here is the structure:</p>
  
  <p><strong>DbContext</strong>:</p>
  
  <pre><code>public class MemberContext : DbContext
  {
      public MemberContext()
          : base("Name=MemberContext")
      { }
  
      public DbSet&lt;Member&gt; Members { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();\
      }
  }
  </code></pre>
  
  <p><strong>Model</strong>:</p>
  
  <pre><code>public class Member
  {
      public int MemberID { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p><strong>GenericRepository</strong>:</p>
  
  <pre><code>public abstract class GenericRepository&lt;TEntity&gt; : IGenericRepository&lt;TEntity&gt; 
      where TEntity : class
  {
      internal DbContext context;
      internal DbSet&lt;TEntity&gt; dbSet;
  
      public GenericRepository(DbContext context)
      {
          this.context = context;
          this.dbSet = context.Set&lt;TEntity&gt;();
      }
  
      public virtual void Insert(TEntity entity)
      {
          dbSet.Add(entity);
      }
  }
  </code></pre>
  
  <p><strong>MemberRepository</strong>:</p>
  
  <pre><code>public class MemberRepository : GenericRepository&lt;Member&gt;, IMemberRepository
  {
      public MemberRepository(DbContext context)
          : base(context)
      { }
  }
  </code></pre>
  
  <p><strong>UnitOfWork</strong>:</p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork
  {
      public DbContext context;
  
      public UnitOfWork(DbContext context)
      {
          this.context = context;
      }
  
      public void SaveChanges()
      {
          context.SaveChanges();
      }
  
      private bool disposed = false;
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
          {
              if (disposing)
              {
                  context.Dispose();
              }
          }
  
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  }
  </code></pre>
  
  <p><strong>MemberService</strong>:</p>
  
  <pre><code>public class MemberService : IMemberService
  {
      private readonly IUnitOfWork unitOfWork;
      private readonly IMemberRepository memberRepository;
  
      public MemberService(IUnitOfWork unitOfWork, IMemberRepository memberRepository)
      {
          this.unitOfWork = unitOfWork;
          this.memberRepository = memberRepository;
      }
  
      public void Save(Member member)
      {
          Save(new List&lt;Member&gt; { member });
      }
  
      public void Save(List&lt;Member&gt; members)
      {
          members.ForEach(m =&gt;
              {
                  if (m.MemberID == default(int))
                  {
                      memberRepository.Insert(m);
                  }
              });
          unitOfWork.SaveChanges();
      }
  }
  </code></pre>
  
  <p>In Member Form I only add a textbox to input member name and a button to save to database. This is the code in member form:</p>
  
  <p><strong>frmMember</strong>:</p>
  
  <pre><code>public partial class frmMember : Form
  {
      private readonly IMemberService memberService;
  
      public frmMember(IMemberService memberService)
      {
          InitializeComponent();
  
          this.memberService = memberService;
      }
  
      private void btnSave_Click(object sender, EventArgs e)
      {
          Member member = new Member();
          member.Name = txtName.Text;
          memberService.Save(member);
      }
  }
  </code></pre>
  
  <p>I implement the SimpleInjector (refer to <a href="http://simpleinjector.readthedocs.org/en/latest/windowsformsintegration.html" rel="noreferrer">http://simpleinjector.readthedocs.org/en/latest/windowsformsintegration.html</a>) in <strong>Program.cs</strong> as seen in the code below:</p>
  
  <pre><code>static class Program
  {
      private static Container container;
  
      [STAThread]
      static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Bootstrap();
          Application.Run(new frmMember((MemberService)container.GetInstance(typeof(IMemberService))));
      }
  
      private static void Bootstrap()
      {
          container = new Container();
  
          container.RegisterSingle&lt;IMemberRepository, MemberRepository&gt;();
          container.Register&lt;IMemberService, MemberService&gt;();
          container.Register&lt;DbContext, MemberContext&gt;();
          container.Register&lt;IUnitOfWork, UnitOfWork&gt;();
  
          container.Verify();
      }
  }
  </code></pre>
  
  <p>When I run the program and add a member, it doesn't save to database. If I changed <code>container.Register</code> to <code>container.RegisterSingle</code>, it will save to database. From the documentation, <code>RegisterSingle</code> will make my class to be a Singleton. I can't using RegisterLifeTimeScope because it will generate an error</p>
  
  <p>"The registered delegate for type IMemberService threw an exception. The IUnitOfWork is registered as 'Lifetime Scope' lifestyle, but the instance is requested outside the context of a Lifetime Scope"</p>
  
  <p>1) How to use SimpleInjector in Windows Form with UnitOfWork &amp; Repository pattern?<br/>
  2) Do I implement the patterns correctly?</p>