From Rockztar, 6 Months ago, written in C#.
Embed
  1. public class PropertyRepository: IPropertyRepository {
  2.     protected SqlConnection _connection;
  3.  
  4.     public PropertyRepository(SqlConnection connection) {
  5.         _connection = connection;
  6.     }
  7.  
  8.     public Property FindById(string propertyId) {
  9.         using(var command = _connection.CreateCommand()) {
  10.             ...
  11.         }
  12.     }
  13. }
  14.  
  15. public class PropertyService: IPropertyService {
  16.     private readonly IPropertyRepository _propertyRepository;
  17.     private readonly IOwnerRepository _ownerRepository;
  18.  
  19.     public PropertyService(IPropertyRepository propertyRepository, IOwnerRepository ownerRepository) {
  20.         _propertyRepository = propertyRepository;
  21.         _ownerRepository = ownerRepository;
  22.     }
  23.  
  24.   // I imagine you should put the logic that retrieves the owners inside the property repository as well, but just for the sake of having two repositories, I'm gonna do it this way.
  25.         public Property FindProperty(string propertyId) {
  26.       var property = _propertyRepository.FindById(propertyId);
  27.       var owners = _ownerRepository.FindByPropertyId(propertyId);
  28.      
  29.       property.Owners = owners;
  30.       return property;
  31.     }
  32.  
  33.  
  34. }
  35.  
  36. public static void main(string args[]) {
  37.     // Factory method that simply creates and opens a new connection
  38.     var connection = ConnectionFactory.CreateConnection();
  39.  
  40.     // Initialize repository
  41.     var propertyRepository = new PropertyRepository(connection);
  42.     var ownerRepository = new OwnerRepository(connection);
  43.  
  44.     // Initialize service
  45.     var service = new PropertyService(propertyRepository, ownerRepository);
  46.  
  47.     ...do stuff with service
  48.  
  49.     _connection.Dispose();
  50. }