-
-
Save vkhorikov/a36d895f6be41ff2c2895e0ab4a88d91 to your computer and use it in GitHub Desktop.
Code review: Fabric class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fabric : AggregateRootWithLinkedObjects | |
{ | |
public bool PreOrder { get; protected set; } | |
private string _Due { get; set; } | |
public Maybe<string> Due | |
{ | |
get => _Due; | |
protected set => _Due = value.Unwrap(); | |
} | |
protected virtual ICollection<FabricImage> _Images { get; set; } | |
public IEnumerable<FabricImage> Images => _Images.Skip(0); | |
protected virtual ICollection<FabricTag> _Tags { get; set; } | |
public IEnumerable<FabricTag> Tags => _Tags.Skip(0); | |
internal static Fabric Create( | |
int[] imageIds, int[] tagIds, bool preOrder, Maybe<string> due) | |
{ | |
Guard.ForDuplicates(imageIds, "Images"); | |
Guard.ForDuplicates(tagIds, "Tags"); | |
var fabric = new Fabric(); | |
if (preOrder) | |
{ | |
if (due.HasNoValue) | |
{ | |
throw new InvalidOperationException( | |
"A pre ordered fabric must have due set"); | |
} | |
fabric.SetPreOrder(due.Value); | |
} | |
else | |
{ | |
if (due.HasValue) | |
{ | |
throw new InvalidOperationException( | |
"A pre ordered fabric does not need a due set"); | |
} | |
} | |
fabric._Images = imageIds | |
.Select(id => new FabricImage(fabric.Id, id)) | |
.ToList(); | |
fabric.MakeFirstImageDefault(); | |
fabric._Tags = tagIds | |
.Select(id => new FabricTag(fabric.Id, id)) | |
.ToList(); | |
return fabric; | |
} | |
private void SetPreOrder(string due) | |
{ | |
PreOrder = true; | |
Due = due; | |
} | |
private void HasArrived() | |
{ | |
PreOrder = false; | |
Due = null; | |
} | |
internal void Update(Fabric update) | |
{ | |
if (update.PreOrder) | |
{ | |
SetPreOrder(update.Due.Value); | |
} | |
else | |
{ | |
HasArrived(); | |
} | |
UpdateLinkedObjects(_Tags, update.Tags, AddFabricTag); | |
UpdateLinkedObjects(_Images, update.Images, AddFabricImage); | |
UpdateDefaultImage(update); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal void Update(Fabric update) | |
{ | |
if (update.PreOrder) | |
{ | |
SetPreOrder(update.Due.Value); | |
} | |
else | |
{ | |
HasArrived(); | |
} | |
/* ... */ | |
} | |
private void SetPreOrder(string due) | |
{ | |
PreOrder = true; | |
Due = due; | |
} | |
private void HasArrived() | |
{ | |
PreOrder = false; | |
Due = null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal void Update(Fabric update) | |
{ | |
PreOrderInfo = update.PreOrderInfo; | |
/* ... */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static PreOrderInfo PreOrdered(string due) | |
{ | |
return new PreOrderInfo(due, true); | |
} | |
public static PreOrderInfo Arrived() | |
{ | |
return new PreOrderInfo(null, false); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fabric : AggregateRootWithLinkedObjects | |
{ | |
protected virtual ICollection<FabricImage> _Images { get; set; } | |
public IEnumerable<FabricImage> Images => _Images.Skip(0); | |
protected virtual ICollection<FabricTag> _Tags { get; set; } | |
public IEnumerable<FabricTag> Tags => _Tags.Skip(0); | |
private void MakeFirstImageDefault() | |
{ | |
FabricImage firstImage = _Images.FirstOrDefault(); | |
if (firstImage != null) | |
{ | |
firstImage.SetIsDefault(true); | |
} | |
} | |
internal void Update(Fabric update) | |
{ | |
/* ... */ | |
UpdateDefaultImage(update); | |
} | |
private void UpdateDefaultImage(Fabric update) | |
{ | |
int? newDefaultImageId = update.GetDefaultImageId(); | |
if (newDefaultImageId == null) | |
return; | |
Maybe<FabricImage> oldDefaultImageOrNothing = GetDefaultImage(); | |
if (oldDefaultImageOrNothing.HasValue) | |
{ | |
var oldDefaultImage = oldDefaultImageOrNothing.Value; | |
if (oldDefaultImage.ImageId == newDefaultImageId) | |
return; | |
oldDefaultImage.SetIsDefault(false); | |
} | |
var newDefaultImage = _Images.SingleOrDefault( | |
i => i.ImageId == newDefaultImageId); | |
newDefaultImage.SetIsDefault(true); | |
} | |
public Maybe<FabricImage> GetDefaultImage() | |
{ | |
return _Images.SingleOrDefault(i => i.IsDefault); | |
} | |
internal int? GetDefaultImageId() | |
{ | |
if (_Images.Count > 0) | |
{ | |
return GetDefaultImage().Value.ImageId; | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected virtual ICollection<FabricImage> _Images { get; set; } | |
public IEnumerable<FabricImage> Images => _Images.Skip(0); | |
protected virtual ICollection<FabricTag> _Tags { get; set; } | |
public IEnumerable<FabricTag> Tags => _Tags.Skip(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected virtual ICollection<FabricImage> _Images { get; set; } | |
public IReadOnlyList<FabricImage> Images => _Images.ToList(); | |
protected virtual ICollection<FabricTag> _Tags { get; set; } | |
public IReadOnlyList<FabricTag> Tags => _Tags.ToList(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private ICollection<FabricImage> _images; | |
public IReadOnlyList<FabricImage> Images => _images.ToList(); | |
private ICollection<FabricTag> _tags; | |
public IReadOnlyList<FabricTag> Tags => _tags.ToList(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fabric : AggregateRootWithLinkedObjects | |
{ | |
public virtual PreOrderInfo PreOrderInfo { get; set; } | |
protected virtual FabricImage _DefaultImage { get; set; } | |
public Maybe<FabricImage> DefaultImage => _DefaultImage; | |
protected virtual ICollection<FabricImage> _Images { get; set; } | |
public IReadOnlyList<FabricImage> Images => _Images.ToList(); | |
protected virtual ICollection<FabricTag> _Tags { get; set; } | |
public IReadOnlyList<FabricTag> Tags => _Tags.ToList(); | |
protected Fabric() | |
{ | |
_Images = new Collection<FabricImage>(); | |
_Tags = new Collection<FabricTag>(); | |
} | |
internal static Fabric Create( | |
int[] imageIds, int[] tagIds, bool preOrder, Maybe<string> due) | |
{ | |
Guard.ForDuplicates(imageIds, "Images"); | |
Guard.ForDuplicates(tagIds, "Tags"); | |
var fabric = new Fabric(); | |
fabric.PreOrderInfo = new PreOrderInfo(due, preOrder); | |
fabric._Images = imageIds | |
.Select(id => new FabricImage(fabric.Id, id)) | |
.ToList(); | |
fabric._DefaultImage = fabric._Images.FirstOrDefault(); | |
// was: fabric.MakeFirstImageDefault(); | |
fabric._Tags = tagIds | |
.Select(id => new FabricTag(fabric.Id, id)) | |
.ToList(); | |
return fabric; | |
} | |
internal void Update(Fabric update) | |
{ | |
PreOrderInfo = update.PreOrderInfo; | |
UpdateLinkedObjects(_Tags, update.Tags, AddFabricTag); | |
UpdateLinkedObjects(_Images, update.Images, AddFabricImage); | |
_DefaultImage = update._DefaultImage; | |
// was: UpdateDefaultImage(update); | |
} | |
private void AddFabricImage(int fabricId, int imageId) | |
{ | |
var fabricImage = new FabricImage(fabricId, imageId); | |
_Images.Add(fabricImage); | |
} | |
private void AddFabricTag(int fabricId, int tagId) | |
{ | |
var fabricTag = new FabricTag(fabricId, tagId); | |
_Tags.Add(fabricTag); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal void Update(Fabric update) | |
{ | |
/* ... */ | |
UpdateLinkedObjects(_Tags, update.Tags, AddFabricTag); | |
UpdateLinkedObjects(_Images, update.Images, AddFabricImage); | |
/* ... */ | |
} | |
private void AddFabricImage(int fabricId, int imageId) | |
{ | |
var fabricImage = new FabricImage(fabricId, imageId); | |
_Images.Add(fabricImage); | |
} | |
private void AddFabricTag(int fabricId, int tagId) | |
{ | |
var fabricTag = new FabricTag(fabricId, tagId); | |
_Tags.Add(fabricTag); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void MarkLinkedObjectsForDeletion<T>( | |
IEnumerable<ILinkToAggregate<T>> linkedObjectsToDelete) | |
{ | |
foreach (ILinkToAggregate<T> linkedObject in linkedObjectsToDelete) | |
{ | |
linkedObject.State = ObjectState.Deleted; | |
} | |
} | |
private void MarkLinkedObjectsForKeeping<T>( | |
IEnumerable<ILinkToAggregate<T>> linkedObjectsToKeep) | |
{ | |
foreach (ILinkToAggregate<T> linkedObject in linkedObjectsToKeep) | |
{ | |
linkedObject.State = ObjectState.Unchanged; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string _Due { get; set; } | |
public Maybe<string> Due | |
{ | |
get => _Due; | |
protected set => _Due = value.Unwrap(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static Fabric Create( | |
int[] imageIds, int[] tagIds, bool preOrder, Maybe<string> due) | |
{ | |
Guard.ForDuplicates(imageIds, "Images"); | |
Guard.ForDuplicates(tagIds, "Tags"); | |
/* ... */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void SetPreOrder(string due) | |
{ | |
PreOrder = true; | |
Due = due; | |
} | |
private void HasArrived() | |
{ | |
PreOrder = false; | |
Due = null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static Fabric Create( | |
int[] imageIds, int[] tagIds, bool preOrder, Maybe<string> due) | |
{ | |
/* ... */ | |
if (preOrder) | |
{ | |
if (due.HasNoValue) | |
{ | |
throw new InvalidOperationException( | |
"A pre ordered fabric must have due set"); | |
} | |
fabric.SetPreOrder(due.Value); | |
} | |
else | |
{ | |
if (due.HasValue) | |
{ | |
throw new InvalidOperationException( | |
"A pre ordered fabric does not need a due set"); | |
} | |
} | |
/* ... */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PreOrderInfo : ValueObject<PreOrderInfo> | |
{ | |
public bool PreOrder { get; } | |
private readonly string _due; | |
public Maybe<string> Due => _due; | |
public PreOrderInfo(Maybe<string> due, bool preOrder) | |
{ | |
if (preOrder && due.HasNoValue) | |
throw new InvalidOperationException("A pre ordered fabric must have due set"); | |
if (!preOrder && due.HasValue) | |
throw new InvalidOperationException("A pre ordered fabric does not need a due set"); | |
_due = due.Unwrap(); | |
PreOrder = preOrder; | |
} | |
protected override bool EqualsCore(PreOrderInfo other) | |
{ | |
return PreOrder == other.PreOrder && _due == other.Due; | |
} | |
protected override int GetHashCodeCore() | |
{ | |
return (_due ?? "").GetHashCode() ^ PreOrder.GetHashCode(); | |
} | |
public static PreOrderInfo PreOrdered(string due) | |
{ | |
return new PreOrderInfo(due, true); | |
} | |
public static PreOrderInfo Arrived() | |
{ | |
return new PreOrderInfo(null, false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fabric : AggregateRootWithLinkedObjects | |
{ | |
public virtual PreOrderInfo PreOrderInfo { get; set; } | |
protected virtual ICollection<FabricImage> _Images { get; set; } | |
public IEnumerable<FabricImage> Images => _Images.Skip(0); | |
protected virtual ICollection<FabricTag> _Tags { get; set; } | |
public IEnumerable<FabricTag> Tags => _Tags.Skip(0); | |
protected Fabric() | |
{ | |
_Images = new Collection<FabricImage>(); | |
_Tags = new Collection<FabricTag>(); | |
} | |
internal static Fabric Create( | |
int[] imageIds, int[] tagIds, bool preOrder, Maybe<string> due) | |
{ | |
Guard.ForDuplicates(imageIds, "Images"); | |
Guard.ForDuplicates(tagIds, "Tags"); | |
var fabric = new Fabric(); | |
fabric.PreOrderInfo = new PreOrderInfo(due, preOrder); | |
fabric._Images = imageIds | |
.Select(id => new FabricImage(fabric.Id, id)) | |
.ToList(); | |
fabric.MakeFirstImageDefault(); | |
fabric._Tags = tagIds | |
.Select(id => new FabricTag(fabric.Id, id)) | |
.ToList(); | |
return fabric; | |
} | |
internal void Update(Fabric update) | |
{ | |
PreOrderInfo = update.PreOrderInfo; | |
UpdateLinkedObjects(_Tags, update.Tags, AddFabricTag); | |
UpdateLinkedObjects(_Images, update.Images, AddFabricImage); | |
UpdateDefaultImage(update); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static Fabric Create( | |
int[] imageIds, int[] tagIds, bool preOrder, Maybe<string> due) | |
{ | |
/* ... */ | |
fabric.PreOrderInfo = new PreOrderInfo(due, preOrder); | |
/* ... */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment