ImageCollection
ImageCollection is a specialized collection class for managing Image objects with domain-specific logic. It provides filtering, sorting, validation, and convenience methods for working with images across all entities (Estate, Department, Employee, Project, etc.).
Why Use ImageCollection?
Instead of using a generic List<Image>, ImageCollection provides:
- Domain-specific methods like
GetPrimaryImage(),GetFloorPlans(),GetExteriorImages() - Built-in validation when adding images
- Filtering and sorting capabilities
- Type safety and self-documenting code
- Consistent behavior across all models
Basic Usage
// Creating a new collection
var images = new ImageCollection();
// Adding images (with automatic validation)
images.Add(new Image
{
Id = "IMG-001",
Filename = "living-room.jpg",
Category = "Interior",
Order = 1
});
// Getting the primary image
var primary = images.GetPrimaryImage();
// Filtering by category
var exteriorImages = images.GetByCategory("Exterior");
// Getting first 5 images
var galleryImages = images.GetFirst(5);
Key Methods
Primary Image
GetPrimaryImage() - Gets the primary/featured image
Returns the image with Order = 1, or the first image with the lowest Order, or the first image in the collection.
var primary = estate.Images.GetPrimaryImage();
if (primary != null)
{
DisplayFeaturedImage(primary.OriginalUrl);
}
Filtering by Category
GetByCategory(string category, bool ordered = true) - Gets images in a specific category
// Get all floor plans
var floorPlans = estate.Images.GetByCategory("Floor Plan");
// Get kitchen images (without sorting)
var kitchenImages = estate.Images.GetByCategory("Kitchen", ordered: false);
GetByCategories(string[] categories, bool ordered = true) - Gets images matching any category
var interiorImages = estate.Images.GetByCategories(
new[] { "Interior", "Kitchen", "Bathroom", "Bedroom" }
);
Ordering & Limiting
GetOrdered() - Returns images sorted by their Order property
var sortedImages = estate.Images.GetOrdered();
foreach (var image in sortedImages)
{
Console.WriteLine($"{image.Order}: {image.Caption}");
}
GetFirst(int count) - Gets the first N images
Common Use Cases
These convenience methods cover typical real estate scenarios:
GetFloorPlans() - Gets floor plan images
Searches for categories: "Floor Plan", "Floorplan", "Plan"
var floorPlans = estate.Images.GetFloorPlans();
if (floorPlans.HasImages)
{
DisplayFloorPlans(floorPlans);
}
GetExteriorImages() - Gets exterior/facade images
Searches for categories: "Facade", "Exterior", "Outside"
GetInteriorImages() - Gets interior images
Searches for categories: "Interior", "Kitchen", "Bathroom", "Bedroom", "Living Room", "Dining Room"
Validation
Images are automatically validated when added to the collection:
var images = new ImageCollection();
// This will throw InvalidOperationException
images.Add(new Image { Filename = "test.jpg" }); // Missing Id
// This will succeed
images.Add(new Image
{
Id = "IMG-001",
Filename = "test.jpg"
});
Validation rules:
- Image cannot be null
- Must have an Id
- Must have a Filename
Utility Properties
HasImages - Check if collection has any images
ImageCount - Get total image count
HasCategory(string category) - Check if a category exists
Complete Example
// Estate with multiple images
var estate = new Estate
{
Id = "EST-001",
Heading = "Modern Apartment in City Center"
};
// Images are automatically initialized as ImageCollection
estate.Images.Add(new Image
{
Id = "IMG-001",
Filename = "exterior.jpg",
Category = "Exterior",
Order = 1,
Caption = "Building facade"
});
estate.Images.Add(new Image
{
Id = "IMG-002",
Filename = "living-room.jpg",
Category = "Interior",
Order = 2,
Caption = "Spacious living room"
});
estate.Images.Add(new Image
{
Id = "IMG-003",
Filename = "floor-plan.jpg",
Category = "Floor Plan",
Order = 3
});
// Get primary image for listing card
var primaryImage = estate.Images.GetPrimaryImage();
Console.WriteLine($"Primary: {primaryImage.Caption}");
// Get floor plans for detail page
var floorPlans = estate.Images.GetFloorPlans();
Console.WriteLine($"Floor plans: {floorPlans.ImageCount}");
// Get interior images for gallery
var interiorImages = estate.Images.GetInteriorImages();
Console.WriteLine($"Interior images: {interiorImages.ImageCount}");
// Check if we have exterior photos
if (estate.Images.HasCategory("Exterior"))
{
Console.WriteLine("Has exterior photos");
}
Usage Across Models
ImageCollection is used consistently across all models:
Estate
Department
Employee
Project, Phase, Building
Download
- ImageCollection.cs - Complete C# source code
Related Documentation
- Image Class - The individual image class
- DocumentCollection - Similar collection for documents
- Shared Classes Overview - All shared classes