title: Domain modelling with Repository in a 3-tier pattern
body: <p>Please note that this code is a code example of how I usually write my code but I have just removed code that would remove focus from my question. I look forward to listen.</p>
  
  <p>I can understand that I need atleast 10 reps. before I can post images and my images illustrates my question... So please follow this link to see my original question at codereview.stackexchange.com - <a href="https://codereview.stackexchange.com/questions/44237/domain-modelling-with-repository">https://codereview.stackexchange.com/questions/44237/domain-modelling-with-repository</a></p>
  
  <p>I've been struggling with some architectural problems which I'm having a hard time figuring out myself.</p>
  
  <p>Im trying to construct a basic structure of a project with the domain model and repository pattern.</p>
  
  <p>It's easy enough to construct the POCO classes and repository when I want to implement some Business logic and different kinds of UI (i.ex. WinForms and MVC) I feel that I miss something because I feel that my code is tightly coupled and I always have to reffer to the POCO classes when ever I need to get an object and show it.</p>
  
  <p>I start out by constructing the following projects in C#(vs2012):</p>
  
  <p>Model</p>
  
  <p>DAL</p>
  
  <p>BL</p>
  
  <p>TestConsole</p>
  
  <p>Here is an example of my Model a model Class:</p>
  
  <pre><code>namespace Panda.Model
  {
      public class Person : IEntity
      {
          public int Id { get; set; }
          public string Name { get; set; }
  
          public Person()
          {
  
          }
          public Person(string name)
          {
              this.Name = name;
          }
      }
  }
  </code></pre>
  
  <p>Here is an example code from my Persons class in my BL project:</p>
  
  <pre><code>using Panda.DAL.Repositories;
  using Panda.DAL.Contexts;
  using Panda.Model;
  namespace Panda.BL
  {
      public class Logic
      {
          private readonly IRepository&lt;Person&gt; _personRep;
          public Logic()
          {
              _personRep = new Repository&lt;Person&gt;(new GenericContext());
          }
  
          public LinkedList&lt;Person&gt; ListOfPersons()
          {
              LinkedList&lt;Person&gt; persons = new LinkedList&lt;Person&gt;();
              persons.AddFirst(new Person("Nicklas"));
              persons.AddFirst(new Person("Martin"));
              persons.AddFirst( new Person("Kresten"));
              return persons;
          }
      }
  </code></pre>
  
  <p>My DAL project consists of generic Repositories that takes a Class of type IEntity:</p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class, IEntity
      {
          /// &lt;summary&gt;
          /// The Generic Repository class that can use all Model classes when istantiating it.
          /// It holds all the generic methods for insert, select, delete and update.
          /// &lt;/summary&gt;
          internal DbSet&lt;T&gt; DbSet;
          internal GenericContext Context;
          public Repository(GenericContext context)
          {
              this.Context = context;
              DbSet = context.Set&lt;T&gt;();
          }
  </code></pre>
  
  <p>My code for the program.cs file in the Console app looks like this:</p>
  
  <pre><code>using Panda.BL;
  
  namespace Panda.TestConsole
  {
      public class Program
      {
          static void Main(string[] args)
          {
              Logic lol = new Logic();
  
              foreach (var item in lol.ListOfPersons())
              {
                  Console.WriteLine(item.Name);
              }
          }
      }
  }
  </code></pre>
  
  <p>Problem is that I don't know how to decouple my Model and DAL further from my UI project (Console etc..) Everytime I want to get i.ex. a Person object I have to of course to reference to my Model project from my console project when ever I want to use a method from the BL project.</p>
  
  <p>My understanding of the whole DDD and 3 tier pattern is that you should be able only talk (reference) to the BL when ever you want to add a new UI project (i.ex. Console, WebForms or MVC) But now I always have to reference to both Models and BL when ever I want to use a method in the BL project.</p>
  
  <p>Right now I feel like have alot of depencies which coupling things to tightly..</p>
  
  <p>I really look forward to hear your thoughts on this which has been puzzling me for a while.</p>
  
  <p>Thanks in advance</p>