一年以来我最好的创意
生活随笔
收集整理的這篇文章主要介紹了
一年以来我最好的创意
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從Kanas.Net 1.0開始,我對元數據的處理都是這樣的:
TypeBroker:收集與實體類型相關的元數據信息,包括反射信息和映射信息;
PropertyBroker:收集與實體類型的屬性相關的元數據信息,包括反射信息和映射信息。
這應該是必須經歷的一步,是整個環境上下文信息中最重要的一部分。
在業務層很難避免對實體元數據信息的操作,例如定位實體類或者定位實體屬性。
實體類型的問題還比較好辦,畢竟在C#下可以使用typeof運算符。實體屬性就比較麻煩了。
{
????intern?Entity(Type?entityType)
????{
????????if?(entityType?!=?null)
????????{
????????????if?(typeof(BizObject).IsAssignableFrom(entityType))
????????????{
????????????????_Type?=?TypeBroker.Types[entityType];
????????????}
????????????else
????????????{
????????????????throw?new?ArgumentException("指定的類型不是實體類型",?"entityType");
????????????}
????????}
????????else
????????{
????????????throw?new?ArgumentException("尚未指定實體類型",?"entityType");
????????}
????}
????protected?TypeBroker?_Type;
????protected?PropertyBroker?PropertyOf(int?propertyindex)
????{
????????PropertyBrokerCollection?properties?=?_Type.Properties;
????????if?(properties.Count?>?propertyIndex?&&?0?<=?propertyIndex)
????????{
????????????return?properties[propertyIndex];
????????}
????????else
????????{
????????????throw?new?ArgumentException("指定的屬性序號無效",?"propertyIndex");
????????}
????}
????private?static?Entity_Pet?_Pet?=?new?Entity_Pet();
????
????public?static?Entity_Pet?Pet
????{
????????get
????????{
????????????return?_Pet;
????????}
????}
????private?static?Entity_Cat?_Cat?=?new?Entity_Cat();
????
????public?static?Entity_Cat?Cat
????{
????????get
????????{
????????????return?_Cat;
????????}
????}
????public?static?implicit?operator?TypeBroker(Entity?entity)
????{
????????return?entity._Type;
????}
}
public?class?Entity_Pet?:?Entity
{
????intern?Entity_Pet()?:?base(typeof(Pet))
????{
????}
????intern?Entity_Cat(Type?entityType)?:?base(entityType)
????{
????}
????public?PropertyBroker?ID
????{
????????get
????????{
????????????return?PropertyOf(typeof(Cat),?0);
????????}
????}
????public?PropertyBroker?Name
????{
????????get
????????{
????????????return?PropertyOf(typeof(Cat),?1);
????????}
????}
}
public?class?Entity_Cat?:?Entity_Pet
{
????intern?Entity_Cat()?:?base(typeof(Cat))
????{
????}
????public?PropertyBroker?Color
????{
????????get
????????{
????????????return?PropertyOf(typeof(Cat),?2);
????????}
????}
}以上示例代碼包含以下語義:
Entity.Pet表示寵物實體;Entity.Cat表示貓實體;
Entity.Pet.ID表示寵物的標識屬性,Entity.Pet.Name表示寵物的名稱屬性;
Entity.Cat.ID表示貓的標識屬性,Entity.Cat.Name表示貓的名稱屬性,Entity.Cat.Color表示貓的顏色屬性。
于是,以上Hibernate實現檢索的代碼在我這里變成這樣了:
StringConstraint.By(Entity.Cat.Name).Like("Frantz@")
在IDE環境下,敲入“Entity.”后會彈出所有實體類型名稱;敲入“Entity.Cat.”后會彈出Cat這個實體類型所有的屬性。如果你愿意在Entity的Cat靜態屬性上加上summary注釋,還會提示實體類型的概念名呢!
TypeBroker:收集與實體類型相關的元數據信息,包括反射信息和映射信息;
PropertyBroker:收集與實體類型的屬性相關的元數據信息,包括反射信息和映射信息。
這應該是必須經歷的一步,是整個環境上下文信息中最重要的一部分。
在業務層很難避免對實體元數據信息的操作,例如定位實體類或者定位實體屬性。
實體類型的問題還比較好辦,畢竟在C#下可以使用typeof運算符。實體屬性就比較麻煩了。
?
以往采用三種方式:
- 實體類型+屬性序號,如:(typeof(Cat), 2)
- 實體類型+屬性名稱,如:(typeof(Cat), "Name")
- 屬性全名稱,如:("Cat.Name")
?
顯然,以上三種方式都不夠優雅。
看看Hibernate如何處理:
List cats = sess.createCriteria(Cat.class)
.add( Expression.like("name", "Fritz%") )
從Cat實體中檢索名稱含Fritz的實例集合,和我的方法二一樣。
注意到那個"name",不僅需要開發者記住對Cat的name屬性的命名,而且還逃過了設計期的類型檢查,所有的錯誤必須到運行期才知道。
現在我在實體信息之外加了一個元數據詞典,解決了這個問題。因為所有的實體類型的源代碼都是工具生成的,所以再順便生成一個元數據詞典代價低廉。
具體實現是這樣的:
?
定義實現一個抽象類:Entity
為每個實體類都建立一個詞典類,繼承自Entity或者Entity的派生類,每個類都指向相應的實體類型。例如名稱為Entity_User的類型指向User實體。
在每個嵌套類型中為對應的實體類的每個屬性定義一個對應的屬性,該屬性的名稱為對應實體屬性的名稱,其值為對應屬性的元數據。
在Entity類中為每個嵌套類定義一個私有的靜態實例,并通過只讀的靜態屬性暴露該靜態實例。
這是示例代碼:
{
????intern?Entity(Type?entityType)
????{
????????if?(entityType?!=?null)
????????{
????????????if?(typeof(BizObject).IsAssignableFrom(entityType))
????????????{
????????????????_Type?=?TypeBroker.Types[entityType];
????????????}
????????????else
????????????{
????????????????throw?new?ArgumentException("指定的類型不是實體類型",?"entityType");
????????????}
????????}
????????else
????????{
????????????throw?new?ArgumentException("尚未指定實體類型",?"entityType");
????????}
????}
????protected?TypeBroker?_Type;
????protected?PropertyBroker?PropertyOf(int?propertyindex)
????{
????????PropertyBrokerCollection?properties?=?_Type.Properties;
????????if?(properties.Count?>?propertyIndex?&&?0?<=?propertyIndex)
????????{
????????????return?properties[propertyIndex];
????????}
????????else
????????{
????????????throw?new?ArgumentException("指定的屬性序號無效",?"propertyIndex");
????????}
????}
????private?static?Entity_Pet?_Pet?=?new?Entity_Pet();
????
????public?static?Entity_Pet?Pet
????{
????????get
????????{
????????????return?_Pet;
????????}
????}
????private?static?Entity_Cat?_Cat?=?new?Entity_Cat();
????
????public?static?Entity_Cat?Cat
????{
????????get
????????{
????????????return?_Cat;
????????}
????}
????public?static?implicit?operator?TypeBroker(Entity?entity)
????{
????????return?entity._Type;
????}
}
public?class?Entity_Pet?:?Entity
{
????intern?Entity_Pet()?:?base(typeof(Pet))
????{
????}
????intern?Entity_Cat(Type?entityType)?:?base(entityType)
????{
????}
????public?PropertyBroker?ID
????{
????????get
????????{
????????????return?PropertyOf(typeof(Cat),?0);
????????}
????}
????public?PropertyBroker?Name
????{
????????get
????????{
????????????return?PropertyOf(typeof(Cat),?1);
????????}
????}
}
public?class?Entity_Cat?:?Entity_Pet
{
????intern?Entity_Cat()?:?base(typeof(Cat))
????{
????}
????public?PropertyBroker?Color
????{
????????get
????????{
????????????return?PropertyOf(typeof(Cat),?2);
????????}
????}
}以上示例代碼包含以下語義:
Entity.Pet表示寵物實體;Entity.Cat表示貓實體;
Entity.Pet.ID表示寵物的標識屬性,Entity.Pet.Name表示寵物的名稱屬性;
Entity.Cat.ID表示貓的標識屬性,Entity.Cat.Name表示貓的名稱屬性,Entity.Cat.Color表示貓的顏色屬性。
于是,以上Hibernate實現檢索的代碼在我這里變成這樣了:
StringConstraint.By(Entity.Cat.Name).Like("Frantz@")
在IDE環境下,敲入“Entity.”后會彈出所有實體類型名稱;敲入“Entity.Cat.”后會彈出Cat這個實體類型所有的屬性。如果你愿意在Entity的Cat靜態屬性上加上summary注釋,還會提示實體類型的概念名呢!
轉載于:https://www.cnblogs.com/Barton131420/articles/200476.html
總結
以上是生活随笔為你收集整理的一年以来我最好的创意的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 今天写了个Win32服务设置软件,呵呵,
- 下一篇: DataGrid中添加DropdownL