title: Business logic in repositories or in separate business classes?
body: <p>I’m developing an application where I’m using repositories (repository pattern) that handle all the CRUD operations (I’m using entity framework) and Unit of work for handling the context.
  I have a generic repository like this:</p>
  
  <pre><code>Public Interface IRepository(Of TEntity As Class)
  
   Function GetAll() As IEnumerable(Of TEntity)
   Function GetByID(id As Object) As TEntity
  
  End Interface
  
  
  Public Class Repository(Of TEntity As Class)
    Implements IRepository(Of TEntity)
  
    Friend _dbContext As Entities = Nothing
    Friend _dbSet As IDbSet(Of TEntity)
  
    Public Sub New(ByVal context As Entities)
      If context Is Nothing Then
          Throw New ArgumentNullException("context wasn't supplied")
      End If
      Me._dbContext = context
      Me._dbSet = context.Set(Of TEntity)()
    End Sub
  
    Public Function GetAll() As IEnumerable(Of TEntity) Implements IRepository(Of TEntity).GetAll
      Return _dbSet.ToList().AsQueryable()
   End Function
  
  
  
   Public Overridable Function GetByID(id As Object) As TEntity Implements IRepository(Of     
      TEntity).GetByID
      Return _dbSet.Find(id)
   End Function
  
  
  End Class
  
  
  Public Interface IUnitOfWork
  
    ReadOnly Property PersonRepository() As IRepository(Of Person)
    ReadOnly Property OrgRepository() As IRepository(Of Organization)
  
    Sub Save()
  
  End Interface
  
  
  Public Class UnitOfWork
  Implements IUnitOfWork
  Implements IDisposable
  
  Private _context As Entities
  Private _personRepository As IRepository(Of Person)
  Private _orgRepository As IRepository(Of Organization)
  Private disposed As Boolean = False
  
  Public Sub New()
      Me._context = New Entities
  End Sub
  
  Public ReadOnly Property PersonRepository() As IRepository(Of Person) Implements  IUnitOfWork.PersonRepository
      Get
          If Me._personRepository Is Nothing Then
              Me._personRepository = New Repository(Of Person)(_context)
          End If
          Return _personRepository
      End Get
  End Property
  
  .
  .
  .
  .
  
  Public Sub Save() Implements IUnitOfWork.Save
      _context.SaveChanges()
  End Sub
  
  Public Sub Dispose() Implements IDisposable.Dispose
      Dispose(True)
      GC.SuppressFinalize(Me)
  End Sub
  
  Protected Overridable Sub Dispose(disposing As Boolean)
      If Not Me.disposed Then
          If disposing Then
              _context.Dispose()
          End If
      End If
      Me.disposed = True
  End Sub
  
  End Class
  </code></pre>
  
  <p>My problem is the business logic, where should I put it? I need to count up all my persons returned and I have to calculate the total weight of all these persons. Should this be done in the person repository or do I need a separate person class for this?
  This is not a MVC application, my classes should be used by another application (I have no front end classes). </p>
  
  <p>I have looked at <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow">this example</a></p>
  
  <p>Under the headline “Changing the Course Controller to use the UnitOfWork Class and Repositories” he does this</p>
  
  <pre><code>public ViewResult Details(int id) { 
    Course course = unitOfWork.CourseRepository.GetByID(id); 
    return View(course); 
   } 
  </code></pre>
  
  <p>How is it possible to say </p>
  
  <pre><code>Course course = unitOfWork.CourseRepository.GetByID(id);
  </code></pre>
  
  <p>The course object is of type Course but the object returned from unitOfWork.CourseRepository.GetByID(id) is of type course entity (from entity framework).</p>
  
  <p>My last question….can I have any properties in the repository classes. If I should have my function for counting all the people and my total weight function in the repository class I need to also have my properties for name, address and so on for storing information.</p>
  
  <p>I hope sombody can help me :)</p>
  
  <p>Thanks in advance!</p>
  
  <p>Liss</p>
  
  <p>EDIT:</p>
  
  <p>Thanks for your answer! So what you are saying is that if I don't have any heavy logic (that is the case for me) I use an extra Person class where I have my properties an also functions for weight and total number of person, but also functions that "mirrors" the functions (GetAll, GetByID) in the generic repository class. And I use the Person class from other classes not the repository class? I still don't understand the example Course course = unitOfWork.CourseRepository.GetByID(id), her the repository and unit of work is used the same way as I did, I think. But when I try this, the unitOfWork.CourseRepository.GetByID(id) returns an object of entity type but the course is an "ordinary" object type (a ordinary class with only properties). How can I say: object of type A = object of type B?</p>
  
  <p>EDIT 2:</p>
  
  <p>Thanks a lot for taking the time to answer me!! So what you say is that it is sometimes better to not auto generate entities but write POCO class instead? But if I want to use my auto generated entities, and I have an entity named PersonEntity I can use this like this in my controller class like this:</p>
  
  <pre><code> Dim personEntity As PersonEntity
  
   personEntity = unitOfWork.PersonEntityRepository.GetByID(id)
  </code></pre>
  
  <p>But if I do it like this I have a reference to an entity in my controller class, but do I want that? Isn’t it best to separate the entities from the controller if I later need to change from Entity Framework? 
  But what I still do not understand is, should I create a new Person class with the same properties as the autogen PersonEntity class and use this in my controller like this</p>
  
  <pre><code>Dim person As Person
  
  person = unitOfWork.PersonEntityRepository.GetByID(id)
  </code></pre>
  
  <p>But then the unitOfWork.PersonEntityRepository.GetByID(id) will return a object of type PersonEntity… so this is not working.
  And where should I put my total weight and total persons functions?</p>
  
  <p>If I understand what you are saying, an entity object can be used as a transfer object but it’s better to use a POCO/DTO object. But if I use a Person POCO can I still have the PersonEntity object? And how do I populate the POCO object when I return the PersonEntity object?</p>