Created
April 18, 2012 06:14
-
-
Save ledsun/2411416 to your computer and use it in GitHub Desktop.
C#のデータクラステンプレ
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.Serialization; | |
using System.Text; | |
using System.Web; | |
namespace Hoge | |
{ | |
/// <summary> | |
/// データクラスのテンプレートです。 | |
/// </summary> | |
[DataContract] | |
public class TemplateDataClass | |
{ | |
/// <summary> | |
/// IDです。 | |
/// DBのIDと対応している場合は | |
/// 保存していない採番前のインスタンスと保存後のIDありのインスタンスを | |
/// 比較できるようにEqualsでの比較項目に入れないほうが便利です。 | |
/// </summary> | |
[DataMember] | |
public uint Id { get; private set; } | |
/// <summary> | |
/// 必須項目です。 | |
/// 自動プロパティのセッターにだけprivateをつけることで不変にします。 | |
/// </summary> | |
[DataMember] | |
public string Must { get; private set; } | |
/// <summary> | |
/// コンストラクタ | |
/// 必須項目の入力値はここでチェックします。 | |
/// </summary> | |
/// <param name="id"></param> | |
/// <param name="must"></param> | |
public TemplateDataClass(uint id, string must) | |
{ | |
if (string.IsNullOrEmpty(must)) | |
{ | |
throw new ArgumentNullException("must"); | |
} | |
Id = id; | |
Must = must; | |
} | |
#region プロパティ | |
/// <summary> | |
/// オプショナルな要素 | |
/// 手動でプロパティを定義して入力値をチェックします。 | |
/// </summary> | |
private string _option = ""; | |
[DataMember] | |
public string Option | |
{ | |
get | |
{ | |
return _option; | |
} | |
set | |
{ | |
if (string.IsNullOrEmpty(value)) | |
{ | |
throw new ArgumentNullException("Option"); | |
} | |
_option = value; | |
} | |
} | |
#endregion | |
/// <summary> | |
/// 使用時に前提条件をチェックしたいときに呼ぶメソッドです | |
/// </summary> | |
/// <returns></returns> | |
public bool Validate() | |
{ | |
if (String.IsNullOrEmpty(Option)) | |
{ | |
throw new ArgumentException("Option is Empty"); | |
} | |
return true; | |
} | |
/// <summary> | |
/// ダミーデータを返します。 | |
/// テストでとにかくインスタンスがほしいときに呼ぶメソッドです。 | |
/// 別のクラスの要素になっている場合などあると便利です。 | |
/// </summary> | |
public static TemplateDataClass Dummy | |
{ | |
get | |
{ | |
return new TemplateDataClass(0, "Must"); | |
} | |
} | |
#region Equalsのオーバーライド | |
public override bool Equals(object obj) | |
{ | |
if (obj == null || GetType() != obj.GetType()) | |
return false; | |
var other = (TemplateDataClass)obj; | |
return Must == other.Must | |
&& Option == other.Option; | |
} | |
public override int GetHashCode() | |
{ | |
return Must.GetHashCode() | |
^ Option.GetHashCode(); | |
} | |
#endregion | |
#region 文字列化 | |
/// <summary> | |
/// リフレクションでプロパティを出力します | |
/// </summary> | |
/// <returns></returns> | |
public override string ToString() | |
{ | |
var values = new List<string>(); | |
foreach (var field in GetType().GetFields()) | |
{ | |
values.Add(string.Format("{0}:{1}", field.Name, field.GetValue(this))); | |
} | |
foreach (var property in GetType().GetProperties()) | |
{ | |
//Emptyなどの静的プロパティでインスタンスを返すと無限ループになるので、静的プロパティの中身は出力しません。 | |
var getMethod = property.GetGetMethod(); | |
if (getMethod != null && !getMethod.IsStatic) | |
{ | |
values.Add(string.Format("{0}:{1}", property.Name, property.GetValue(this, null))); | |
} | |
} | |
var sb = new StringBuilder(GetType().Name); | |
sb.Append(string.Format(": [{0}]", string.Join(",", values.ToArray()))); | |
return sb.ToString(); | |
} | |
#endregion | |
#region シリアライズ | |
/// <summary> | |
/// 引数なしコンストラクタが無いのでDataContractSerializerを使ってXML化します。 | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
public static string Serialize(TemplateDataClass value) | |
{ | |
var ret = ""; | |
using (var ms = new MemoryStream()) | |
{ | |
new DataContractSerializer(typeof(TemplateDataClass)).WriteObject(ms, value); | |
ret = Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length); | |
} | |
return ret; | |
} | |
/// <summary> | |
/// SerializeしたXMLからデータクラスを復元します。 | |
/// </summary> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
public static TemplateDataClass Deseriarilze(string value) | |
{ | |
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(value))) | |
return (TemplateDataClass)new DataContractSerializer(typeof(TemplateDataClass)).ReadObject(ms); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment