Imam zadatak koji glasi:Turista može da ostavi recenziju za određenu turu. Recenzija obuhvata: ocenu (1-5), komentar, informacije o samom turisti koji je ostavio recenziju, datum kada je posetio turu, datum kada je ostavio komentar i slike.
U timu imamo neka dogovorena pravila koja mormao postovati kao sto su:
Kod pišemo na engleskom jeziku.
Klase se pišu po UpperCamelCase konvenciji. (npr. DoctorAppointment)
Promenljive se pišu lowerCamelCase konvenciji gde je prva reč napisana malim
slovima (npr. int parsedLines).
Pisanje privatnih polja u klasama se piše u lowerCamelCase konvenciji gde je prefix _
(ovo je privatno polje: _totalPrice).
U klasi se prvo navode sva polja, properitiji, konstruktori, geteri i seteri (ako ih ima),
metode interfejsa koje su implementirane, i onda na kraju sve ostale metode.
Za ovaj zadatak je potrebno da:
Odabereš dobar modul u koji smeštamo novu klasu, tako da semantiči entitet pripada tom modulu,
Kreiraš entitet u odgovarajućem Core projektu, po uzoru na postojeće entitete.
U startu počinjemo sa tri modula:
Stakeholders - Modul koji se bavi funkcionalnostima vezanim za registraciju korisničkih profila.
Tours - Modul koji se bavi funkcionalnostima vezanim za definisanje i izvršavanje tura.
Blog - Modul koji se bavi funkcionalnostima vezanim za blog.
Da bismo kreirali novi entitet, potrebno je da:
U okviru Core projekta odgovarajućeg modula otvorimo Domain direktorijum i u okviru njega napravimo klasu sa značajnim nazivom.
Nova klasa nasleđuje Entity klasu iz BuildingBlocks.Core projekta.
Nova klasa definiše polja koja odgovaraju zahtevu, gde stavljamo sve set metode na privatne.
Nova klasa ima konstruktor sa parametrima koji odgovaraju svim poljima. Konstruktor treba da validira prosleđene parametre.
Moduli su Stakeholders, Blog i Tours. Pretpostavljam da cemo smestati u Tours modul.
Ovako izgleda Entity klasa koju nasledjujemo:
namespace Explorer.BuildingBlocks.Core.Domain;
public abstract class Entity
{
public long Id { get; protected set; }
code
public override bool Equals(object? obj)
{
return obj is Entity entity && Id.Equals(entity.Id);
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
A ovako tura:
using Explorer.BuildingBlocks.Core.Domain;
namespace Explorer.Tours.Core.Domain;
public enum TourLevel { Beginner, Intermediate, Advanced}
public enum TourStatus { Draft, Active, Finished, Canceled = -1}
public class Tour : Entity {
public string Name { get; private set; }
public string Description { get; private set; }
public TourLevel Level { get; private set; }
public TourStatus Status { get; private set; }
public double Price { get; private set; }
code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
public Tour(string name, string? description, TourLevel level) {
if(string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException("Invalid Name.");
Name = name;
Description = description;
Level = level;
Status = TourStatus.Draft;
Price = 0.0;
}
}
Kako bi postepeno pristupao rjesavanju ovog problema, konkretno za sada samo kreiranje entiteta u odgovarajućem Core projektu, po uzoru na postojeće entitete.
To address the task of creating a Review entity within your project, we'll follow a structured approach aligned with your team's conventions and existing architecture. Below are the detailed steps along with the corresponding code implementation.
Step 1: Choosing the Appropriate Module
Given that the Review entity is closely related to tours (since it's a review for a specific tour), it semantically fits within the Tours module. This ensures that all tour-related functionalities, including reviews, are encapsulated within a single module, promoting better organization and maintainability.
Step 2: Creating the Review Class in the Core.Domain Namespace
Navigate to the Explorer.Tours.Core.Domain directory within your project structure. Here, we'll create a new class named Review that inherits from the abstract Entity class.
Step 3: Defining the Fields and Properties
Based on the requirements, the Review entity should include the following attributes:
Rating (int): An integer value between 1 and 5.
Comment (string): The textual feedback provided by the tourist.
Tourist Information (long): A reference to the tourist who left the review. Assuming that the tourist's profile is managed in the Stakeholders module, we'll reference the tourist by their unique identifier (Id).
Tour Date (DateTime): The date when the tourist participated in the tour.
Review Date (DateTime): The date when the review was submitted.
Images (List<string>): A collection of image URLs associated with the review.
Naming Conventions
Private Fields: Prefixed with an underscore and written in lowerCamelCase (e.g., _rating).
Properties: Written in UpperCamelCase (e.g., Rating).
Step 4: Implementing the Review Class
Here's the complete implementation of the Review class adhering to your team's coding standards and project architecture:
csharp
using System;
using System.Collections.Generic;
using Explorer.BuildingBlocks.Core.Domain;
namespace Explorer.Tours.Core.Domain
{
public class Review : Entity
{
// Private Fields
private int _rating;
private string _comment;
private long _touristId;
private DateTime _tourDate;
private DateTime _reviewDate;
private List<string> _images;
// Properties
public int Rating
{
get => _rating;
private set
{
if (value < 1 || value > 5)
throw new ArgumentOutOfRangeException(nameof(Rating), "Rating must be between 1 and 5.");
_rating = value;
}
}
public string Comment
{
get => _comment;
private set
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentNullException(nameof(Comment), "Comment cannot be null or empty.");
_comment = value;
}
}
public long TouristId
{
get => _touristId;
private set
{
if (value <= 0)
throw new ArgumentException("Invalid TouristId.", nameof(TouristId));
_touristId = value;
}
}
public DateTime TourDate
{
get => _tourDate;
private set
{
if (value == default)
throw new ArgumentException("Invalid TourDate.", nameof(TourDate));
_tourDate = value;
}
}
public DateTime ReviewDate
{
get => _reviewDate;
private set
{
if (value == default)
throw new ArgumentException("Invalid ReviewDate.", nameof(ReviewDate));
_reviewDate = value;
}
}
public IReadOnlyList<string> Images => _images.AsReadOnly();
// Constructor
public Review(int rating, string comment, long touristId, DateTime tourDate, DateTime reviewDate, List<string> images)
{
Rating = rating;
Comment = comment;
TouristId = touristId;
TourDate = tourDate;
ReviewDate = reviewDate;
_images = images ?? new List<string>();
}
// Methods (if any) can be added here following the specified order
}
}
Explanation of the Implementation
Private Fields: All private fields are prefixed with an underscore (_) and use lowerCamelCase as per the conventions.
Properties:
Rating: Ensures the value is between 1 and 5; otherwise, throws an ArgumentOutOfRangeException.
Comment: Ensures the comment is neither null nor empty; otherwise, throws an ArgumentNullException.
TouristId: Validates that the tourist's ID is a positive number; otherwise, throws an ArgumentException.
TourDate and ReviewDate: Ensure that the dates are valid and not default DateTime values.
Images: Exposes the list of images as a read-only collection to prevent external modification.
Constructor:
Accepts parameters corresponding to all fields.
Initializes the properties using the setters, which include validation logic.
For the images parameter, it ensures that if null is passed, it initializes an empty list to avoid NullReferenceException.
Inheritance: The Review class inherits from the Entity class, thereby gaining the Id property and equality overrides.
Step 5: Integrating with Other Modules
Since the Review entity references a tourist via TouristId, ensure that:
There's a relationship or a foreign key constraint established between the Review entity in the Tours module and the User entity in the Stakeholders module.
Consider using repositories or services to manage interactions between modules if needed.
Step 6: Additional Considerations
Image Handling: Depending on your requirements, storing images as URLs is a simple approach. Alternatively, you might want to create a separate Image value object or entity to handle more complex scenarios.
Validation Enhancements: You can further enhance validation by implementing domain-specific rules or using validation frameworks.
Extensibility: If in the future you need to add more attributes to the Review entity, the current structure allows for easy extension without violating the existing conventions or architecture.
Conclusion
By following the outlined steps and adhering to your team's coding standards, the Review entity is effectively integrated into the Tours module. This setup promotes consistency, maintainability, and scalability within your project's architecture.
If you have any further questions or need assistance with subsequent steps (like creating repositories, services, or integrating with other modules), feel free to ask!