程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 領域驅動(DDD)實戰---月份類YearMonth

領域驅動(DDD)實戰---月份類YearMonth

編輯:C#入門知識


 

用Extension的方式,來增強代碼流暢性和可讀性

 public static class YearMonthExtension
    {
        public static YearMonth year(this int year, int month)
        {
            return new YearMonth(year, month);
        }

        public static bool is_later_than(this YearMonth left, YearMonth right) {
            return left.CompareTo(right) > 0;
        }

        public static bool is_ealier_than(this YearMonth left, YearMonth right) {
            return left.CompareTo(right) < 0;
        }

        public static YearMonth get_ealier(this YearMonth left, YearMonth right)
        {
            if (left.is_ealier_than(right))
                return left;
            return right;
        }

        public static YearMonth get_later(this YearMonth left, YearMonth right)
        {
            if (left.is_later_than(right))
                return left;
            return right;
        }
}

從測試看功能:公用的測試基類,很簡單,就是聲明一個YearMonth對象做測試

public class YearMonthSpecs
    {
        protected static YearMonth subject;

    }

通過構造器,創建YearMonth對象

public class When_init_by_year_month
        :YearMonthSpecs
    {
        private Because of =
            () => subject = new YearMonth(2011,3);

        private It year_should_set_properly =
            () => subject.Year.ShouldEqual(2011);

         private It month_should_set_properly =
             () => subject.Month.ShouldEqual(3);

    }

通過流暢接口創建YearMonth: 2012.year(3)。你還可以自己定制為: 2012.年(3)

public class When_create_year_month_through_fluent_interface
    {
        private It should_create_year_month_properly =
            () => 2012.year(3).ShouldEqual(new YearMonth(2012, 3));
    }

通過字符串創建

 

public class When_init_by_string : YearMonthSpecs
    {
        private Because of =
            () => subject = new YearMonth("2011年01月");

        private It year_should_set_properly =
             () =>
                 {
                     subject.Year.ShouldEqual(2011);
                     subject.Month.ShouldEqual(1);
                 };
    }

Special Case模式,特別處理:世界末日的下一個月還是世界末日,創世紀的上一個月還是創世紀

private It far_past_last_month_is_still_far_past =
            () => YearMonth.FarPast.get_last().ShouldEqual(YearMonth.FarPast);
        private It far_past_next_month_is_still_far_past =
         () => YearMonth.FarPast.get_next().ShouldEqual(YearMonth.FarPast);

        private It far_future_last_month_is_stil_far_future =
          () => YearMonth.FarFuture.get_last().ShouldEqual(YearMonth.FarFuture);

        private It far_future_next_month_is_stil_far_future =
            () => YearMonth.FarFuture.get_next().ShouldEqual(YearMonth.FarFuture);

 

YearMonth結構類型的完整代碼

 

using System;
using Skight.Arch.Domain.Interfaces;

namespace Skight.Arch.Domain.Entities
{
    public struct YearMonth : IEquatable<YearMonth>, IComparable<YearMonth>
    {
        private readonly int ticks;
        private readonly int year;
        private readonly int month;
        private const int MONTHS_PER_YEAR=12;

        public static YearMonth FarPast = new YearMonth(0,1);
        public static YearMonth FarFuture = new YearMonth(9999,12);
        #region Constructors by ticks, year/month and datetime
        internal YearMonth(int ticks)
        {
            this.ticks = ticks;
            int remain;
            year = Math.DivRem(ticks-1, MONTHS_PER_YEAR, out remain);
            month = remain + 1;
        }
        public YearMonth(int year, int month)
            :this(year*MONTHS_PER_YEAR + month){}
        public YearMonth(DateTime date_time)
            :this(date_time.Year,date_time.Month){}

        public YearMonth(string yearMonth):this(int.Parse(yearMonth.Substring(0, 4))
            ,int.Parse(yearMonth.Substring(5, 2)))
        {}

        #endregion 

        public int Year { get { return year; } }
        public int Month { get { return month; } }
        public DateTime as_date_Time()
        {
            return new DateTime(Year,Month,1);
        }

        public override string ToString()
        {
            return string.Format("{0}年{1}月", year.ToString("0000"), month.ToString("00"));
        }

        #region Euqals and Compare
        public bool Equals(YearMonth other)
        {
            return other.ticks == ticks;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (obj.GetType() != typeof (YearMonth)) return false;
            return Equals((YearMonth) obj);
        }

        public override int GetHashCode()
        {
            return ticks;
        }

        public int CompareTo(YearMonth other)
        {
            return ticks.CompareTo(other.ticks);
        }

        #endregion

        #region Discrete interface
        public YearMonth get_last()
        {
            if (Equals(FarPast))
                return FarPast;

            if (Equals(FarFuture))
                return FarFuture;
            return new YearMonth(ticks - 1);
        }
        public YearMonth get_last(int Dvalue)
        {
            if (Equals(FarPast))
                return FarPast;

            if (Equals(FarFuture))
                return FarFuture;
            return new YearMonth(ticks - Dvalue);
        }

        public YearMonth get_next()
        {
            if (Equals(FarPast))
                return FarPast;

            if (Equals(FarFuture))
                return FarFuture;
            return new YearMonth(ticks + 1);
        }

        public YearMonth get_next(int DValue)
        {
            if (Equals(FarPast))
                return FarPast;

            if (Equals(FarFuture))
                return FarFuture;
            return new YearMonth(ticks + DValue);
        }
        #endregion 

         public static implicit operator DateTime(YearMonth year_month)
         {
             return year_month.as_date_Time();
         }

        public static implicit operator YearMonth(DateTime date_time)
        {
            return new YearMonth(date_time);
        }
    }
}


(本文版權屬於© 2012 - 2013 予沁安 | 轉載請注明作者和出處)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved