Closed jibai-kia closed 1 year ago
// Reward.cs
using System.ComponentModel.DataAnnotations;
namespace CleanBrightCompany.Domain
{
public class Reward : IReward
{
private int _id;
private string _name = string.Empty;
private string _description = string.Empty;
[Key]
public int Id
{
get => GetId();
set => SetId(value);
}
public string Name
{
get => GetName();
set => SetName(value);
}
public string Description
{
get => GetDescription();
set => SetDescription(value);
}
public Reward() { }
public Reward(int id, string name, string description, int pointValue)
{
SetId(id);
SetName(name);
SetDescription(description);
}
public int GetId()
{
return _id;
}
public void SetId(int id)
{
if (id < 0)
{
throw new ArgumentException("Reward id cannot be negative.");
}
_id = id;
}
public string GetName()
{
return _name;
}
public void SetName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Reward name cannot be null or empty.");
}
_name = name.Trim();
}
public string GetDescription()
{
return _description;
}
public void SetDescription(string description)
{
if (string.IsNullOrWhiteSpace(description))
{
throw new ArgumentException("Reward description cannot be null or empty.");
}
_description = description.Trim();
}
}
}
// IReward.cs
namespace CleanBrightCompany.Domain
{
public interface IReward
{
public int GetId();
public void SetId(int id);
public string GetName();
public void SetName(string name);
public string GetDescription();
public void SetDescription(string description);
}
}
The Reward
class represents a reward object and has the following properties and methods:
Properties:
Id
: an integer representing the unique identifier of the reward. It has a Key attribute, which means that it is the primary key of the reward entity.Name
: a string representing the name of the reward.Description
: a string representing the description of the reward.Methods:
GetId()
: returns the reward's id.SetId(int id)
: sets the reward's id. It throws an exception if the given id is negative.GetName()
: returns the reward's name.SetName(string name)
: sets the reward's name. It throws an exception if the given name is null, empty or whitespace.GetDescription()
: returns the reward's description.SetDescription(string description)
: sets the reward's description. It throws an exception if the given description is null, empty or whitespace.Overall, the Reward
class provides basic validation for its properties and can be used to create, read, update, and delete rewards in the system.
The IReward.cs
file defines an interface IReward
that contains the basic properties and methods for a Reward
object.
Interfaces are used to define a contract that a class must implement. In this case, any class that wants to be considered a Reward
must implement the IReward
interface, which requires it to have Id
, Name
, and Description
properties, and GetId()
, SetId()
, GetName()
, SetName()
, GetDescription()
, and SetDescription()
methods.
This allows other parts of the code, such as the RewardService
or the RewardRepository
, to interact with Reward
objects through the IReward
interface, without having to know the specific implementation of the Reward
class.
This thread provides updates related to Reward and IReward