xxx: while you download one library, while another, while you glue the xml config for half a meter, while you configure the mapping for hibernate, while you draw the base, while you lift the web services
xxx: like and hello world you write, and two weeks have passed and it seems to everyone that this is an accounting system for small business
ibash.org.ru
public abstract class TaskBase : Entity { protected TaskBase(string summary, string desc) { ChangeSummary(summary); ChangeDescription(desc); } protected TaskBase() { } public string Summary { get; private set; } public string Description { get; private set; } public bool IsComplete { get; protected set; } public virtual void Complete() { if (IsComplete) throw new InvalidOperationException("Task is already completed"); IsComplete = true; } public void ChangeSummary(string summary) { Summary = summary; } public void ChangeDescription(string description) { Description = description; } }
public class IndeterminateTask : TaskBase { public IndeterminateTask(string summary, string desc, Category category) : base(summary, desc) { Category = category; } protected IndeterminateTask() { } public Category Category { get; private set; } public void ChangeCategory(Category category) { Category = category; } }
public abstract class DailyTask : TaskBase { protected DailyTask(string summary, string desc, DateTime setupDay) : base(summary, desc) { DueToDate = setupDay; } protected DailyTask() { } public DateTime DueToDate { get; private set; } public void ChangeDueDate(DateTime dueToDate) { DueToDate = dueToDate; } }
public class SingleDailyTask : DailyTask { public SingleDailyTask(string summary, string desc, DateTime setupDay, bool isWholeDay) : base(summary, desc, setupDay) { IsWholeDay = isWholeDay; } protected SingleDailyTask() { } public bool IsWholeDay { get; private set; } public void ChangeIsWholeDay(bool isWholeDay) { IsWholeDay = isWholeDay; if (IsWholeDay) { ChangeDueDate(DueToDate.Date); } } }
public class MultipleDailyTask : DailyTask { public MultipleDailyTask(string summary, string desc, DateTime setupDay, IEnumerable<DateTime> dueToDates) : base(summary, desc, setupDay) { ChangeSubtasks(dueToDates.ToList()); } protected MultipleDailyTask() { } public virtual ICollection<Subtask> Subtasks { get; set; } public override void Complete() { throw new NotSupportedException(); } public void CompleteSubtask(DateTime subtaskDueDate) { if (Subtasks == null) throw new InvalidOperationException(); var subtask = Subtasks.FirstOrDefault(i => i.DueTime == subtaskDueDate); if (subtask == null) throw new InvalidOperationException(); subtask.Complete(DateTime.Now); var hasUncompleted = Subtasks.Any(i => i.CompletedAt == null); if (!hasUncompleted) { base.Complete(); } } public bool HasUncompletedSubtasks { get { return Subtasks != null && Subtasks.Any(i => i.CompletedAt == null); } } public int CompletionPercentage { get { var totalSubtasks = Subtasks.Count; var completedSubtasks = Subtasks.Count(i => i.CompletedAt.HasValue); if (totalSubtasks == 0 || totalSubtasks == completedSubtasks) return 100; return (int) Math.Round(completedSubtasks * 100.0 / totalSubtasks, 0); } } public void ChangeSubtasks(ICollection<DateTime> subtasksDueToDates) { var times = subtasksDueToDates.Select(i => i.ToTime()); if (Subtasks == null) { Subtasks = times.Select(i => new Subtask(i)).ToList(); return; } var oldSubtasks = Subtasks.ToList(); var newSubtasks = times.ToList(); //removing no longer exist items foreach (var oldSubtask in oldSubtasks) { if (!newSubtasks.Contains(oldSubtask.DueTime)) { Subtasks.Remove(oldSubtask); } } //adding new foreach (var newSubtask in newSubtasks) { if (Subtasks.All(i => i.DueTime != newSubtask)) { Subtasks.Add(new Subtask(newSubtask)); } } } }
public class Subtask : Entity { public DateTime DueTime { get; private set; } public DateTime? CompletedAt { get; private set; } public Subtask(DateTime dueTime) { DueTime = dueTime; } public void Complete(DateTime completedAt) { CompletedAt = completedAt; } protected Subtask() { } }
public class Category : Entity { public Category(string name, Category parentCategory) { Name = name; ParentCategory = parentCategory; } protected Category() { } public string Name { get; private set; } public virtual ICollection<IndeterminateTask> Tasks { get; set; } public virtual ICollection<Category> ChildrenCategories { get; set; } public virtual Category ParentCategory { get; private set; } public void ChangeName(string name) { Name = name; } public void ChangeParentCategory(Category category) { ParentCategory = category; } }
public interface IRepository { } public interface ITaskRepository : IRepository { IEnumerable<TaskBase> AllMatching(Specification<TaskBase> specification); void Add(TaskBase taskBase); void Remove(TaskBase taskBase); TaskBase Get(Guid taskId); } public interface ICategoryRepository : IRepository { IEnumerable<Category> All(); void Add(Category category); void Remove(Category category); Category Get(Guid id); }
public static class CategorySpecifications { public static Specification<Category> Name(string name) { return new DirectSpecification<Category>(category => category.Name == name); } } public static class TaskSpecifications { public static Specification<TaskBase> CompletedTask() { return new DirectSpecification<TaskBase>(task => task.IsComplete); } public static Specification<TaskBase> DueToDateRange(DateTime startDateIncl, DateTime endDateIncl) { var spec = IsDailyTask(); spec &= new DirectSpecification<TaskBase>(task => ((DailyTask)task).DueToDate >= startDateIncl && ((DailyTask)task).DueToDate <= endDateIncl); return spec; } public static Specification<TaskBase> IsIndeterminatedTask() { return new DirectSpecification<TaskBase>(task => task is IndeterminateTask); } public static Specification<TaskBase> IsDailyTask() { return new DirectSpecification<TaskBase>(task => task is DailyTask); } }
public void RemoveTask(Guid taskId) { using (var unitOfWork = UnitOfWorkFactory.Create()) { var task = _tasksRepository.Get(taskId); if (task == null) throw new Exception(); _tasksRepository.Remove(task); unitOfWork.Commit(); } } public IEnumerable<DailyTaskDTO> GetMonthTasks() { var nowDate = DateTime.Now; var monthStartDate = new DateTime(nowDate.Year, nowDate.Month, 1); var monthEndDate = new DateTime(nowDate.Year, nowDate.Month, DateTime.DaysInMonth(nowDate.Year, nowDate.Month)); var specification = TaskSpecifications.DueToInDateRange(monthStartDate, monthEndDate); var tasks = _tasksRepository.AllMatching(specification).ToList(); return tasks.OfType<DailyTask>().ProjectedAsCollection<DailyTaskDTO>().ToArray(); }
public void RemoveTask(Guid taskId) { using (ILifetimeScope container = BeginLifetimeScope()) { container.Resolve<TaskService>().RemoveTask(taskId); } } public List<DailyTaskDTO> GetMonthTasks() { using (ILifetimeScope container = BeginLifetimeScope()) { return container.Resolve<TaskService>().GetMonthTasks().ToList(); } }
Source: https://habr.com/ru/post/173893/
All Articles