Skip to content

Instantly share code, notes, and snippets.

@zharro
Last active December 19, 2016 15:09
Show Gist options
  • Save zharro/0050c802832694d02da9e75cb9296a4e to your computer and use it in GitHub Desktop.
Save zharro/0050c802832694d02da9e75cb9296a4e to your computer and use it in GitHub Desktop.
Entity simple example and base class. Base class comes from @vkhorikov (https://github.com/vkhorikov/DddInAction/tree/master/DddInPractice.Logic/Common).
using System;
class Appointment : Entity
{
public string Title { get; set; }
public int PatientId { get; set; }
public int RoomId { get; private set; }
public int? DoctorId { get; set; }
public bool IsConfirmed { get; private set; }
// Value object
public DateTimeRange TimeRange { get; private set; }
public Appointment()
{
IsConfirmed = false;
}
public void UpdateRoom(int newRoomId)
{
RoomId = newRoomId;
// Additional logic here
}
public void UpdateTime(DateTimeRange newStartEnd)
{
TimeRange = newStartEnd;
// Additional logic here
}
}
public abstract class Entity
{
public virtual long Id { get; protected set; }
public override bool Equals(object obj)
{
var other = obj as Entity;
if (ReferenceEquals(other, null))
return false;
if (ReferenceEquals(this, other))
return true;
if (GetType() != other.GetType())
return false;
if (Id == 0 || other.Id == 0)
return false;
return Id == other.Id;
}
public static bool operator ==(Entity a, Entity b)
{
if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
return true;
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
return false;
return a.Equals(b);
}
public static bool operator !=(Entity a, Entity b)
{
return !(a == b);
}
public override int GetHashCode()
{
return (GetType().ToString() + Id).GetHashCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment