Created
October 12, 2012 19:14
-
-
Save yreynhout/3880929 to your computer and use it in GitHub Desktop.
VideoTape - Video
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 Video : VoodooMagic { | |
public static Video New(VideoId id, Title title, Date releaseDate) { | |
return new Video( | |
Events.NewVideo(id, title, releaseDate)); | |
} | |
Video(object initialEvent) { | |
ApplyEvent(initialEvent); | |
} | |
public void CorrectReleaseDate(Date corrected) { | |
ApplyEvent( | |
Events.CorrectedReleaseDate(_id, corrected)); | |
} | |
// KISS, therefore, no tell don't ask ;-) | |
internal string Id { get { return _id; } } | |
internal Title Title { get { return _title; } } | |
internal Percentage GetCheckoutDiscount() { | |
if(_releaseDate.MoreThan(5.YearsAgo()) { | |
return 25.Percent(); | |
} | |
return 0.Percent(); | |
} | |
//More behaviors here ... | |
//Insert your preferred flavor of state change handlers | |
//(e.g. https://github.com/gregoryyoung/m-r) | |
//(e.g. https://github.com/Lokad/lokad-iddd-sample) | |
} | |
public class VideoTape : VoodooMagic { | |
public class VideoTape(Video video) { | |
_video = video; | |
} | |
public void Checkout() { | |
var discount = _video.GetCheckoutDiscount(); | |
//... continue here | |
ApplyEvent( | |
Events.CheckedOutVideoTape(_id, _video.Id, _video.Title, discount)); | |
} | |
} | |
public class VideoTapeRepository { | |
public VideoTapeRepository(IEventStreamReader reader, IVideoRepository videoRepository) { | |
_reader = reader; | |
_videoRepository = videoRepository; | |
} | |
public VideoTape GetById(VideoTapeId id) { | |
var stream = _reader.Get(new Uri(String.Format("urn:stream:VideoTape/{0}", id.Value))); | |
var video = _videoRepository.GetById(ProbeStreamForVideoId(stream)); | |
var tape = new VideoTape(video); | |
tape.LoadFromHistory(stream.Events); | |
return tape; | |
} | |
VideoId ProbeStreamForVideoId(EventStream stream) { | |
//Scan the stream for the answer to this question. | |
//Could be the first event, the latest event relating to a title, etc ... | |
//Safe to say it's highly contextual. | |
//And yes, if you have millions of events in one stream, | |
//you might wanna deal with this another way ;-) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment