/// <summary> /// View Model(VM) Product のプロパティの値を、Model(M) Product のプロパティにコピーします。 /// </summary> /// <param name="from">View Model(VM) Product のインスタンスを指定します。</param> /// <param name="to">Model(M) Product のインスタンスを指定します。</param> public static void SetProperties(ViewProduct from, MvvmSample.Data.Product to) { Func <string, string> getTrim = text => string.IsNullOrWhiteSpace(text) ? null : text.Trim(); to.Id = from.Id; to.Title = getTrim(from.Title); to.ProductUrl = getTrim(from.ProductUrl); to.DownloadUrl = getTrim(from.DownloadUrl); to.Description = getTrim(from.Description); to.Publisher = getTrim(from.Publisher); to.PublisherUrl = getTrim(from.PublisherUrl); to.ImageUrl = getTrim(from.ImageUrl); Func <string, decimal?> toDecimal = text => { if (string.IsNullOrWhiteSpace(text)) { return(null); } if (text == "FREE") { return(0); } return(Convert.ToDecimal(text.Replace(",", ""))); }; to.Price = toDecimal(getTrim(from.Price)); } // end sub
} // end sub /// <summary> /// Model(M) Product のプロパティの値を元に、View Model(VM) Product の新しいインスタンスを初期化して返します。 /// </summary> /// <param name="from">Model(M) Product のインスタンスを指定します。</param> /// <returns>新しい View Model(VM) Product のインスタンスを返します。</returns> public static ViewProduct FromProduct(MvvmSample.Data.Product from) { var to = new ViewProduct(); ViewProduct.SetProperties(from, to); return(to); } // end function
} // end sub /// <summary> /// Model(M) Product のプロパティの値を、View Model(VM) Product のプロパティにコピーします。 /// </summary> /// <param name="from">Model(M) Product のインスタンスを指定します。</param> /// <param name="to">View Model(VM) Product のインスタンスを指定します。</param> public static void SetProperties(MvvmSample.Data.Product from, ViewProduct to) { to.Id = from.Id; to.Title = from.Title; to.ProductUrl = from.ProductUrl; to.DownloadUrl = from.DownloadUrl; to.Description = from.Description; to.Publisher = from.Publisher; to.PublisherUrl = from.PublisherUrl; to.ImageUrl = from.ImageUrl; if (from.Price.HasValue && from.Price.Value <= 0) { to.Price = "FREE"; } else { to.Price = from.Price?.ToString("C") ?? "FREE"; } // end if } // end sub
} // end sub /// <summary> /// Model(M) Product のプロパティの値を、View Model(VM) Product のプロパティにコピーします。 /// </summary> /// <param name="from">Model(M) Product のインスタンスを指定します。</param> /// <param name="to">View Model(VM) Product のインスタンスを指定します。</param> public static void SetProperties(MvvmSample.Data.Product from, ViewProduct to) { Func <decimal?, string, string> getToString = (value, format) => value.HasValue == false ? null : value.Value.ToString(format); to.Id = from.Id; to.Title = from.Title; to.ProductUrl = from.ProductUrl; to.DownloadUrl = from.DownloadUrl; to.Description = from.Description; to.Publisher = from.Publisher; to.PublisherUrl = from.PublisherUrl; to.ImageUrl = from.ImageUrl; if (from.Price.HasValue && from.Price.Value <= 0) { to.Price = "FREE"; } else { to.Price = getToString(from.Price, "C") ?? "FREE"; } // end if } // end sub