BLToolkit : Mapping a code table
来源:互联网 发布:剑网三dbm数据请求失败 编辑:程序博客网 时间:2024/06/05 17:21
In my database, there is one large "code" table with system code look-ups for values used all over the system. Like so:
[TableName("code_entries")] public class Code { [MapField("code_nbr")][PrimaryKey, Identity] public int Id; [MapField("code")] public string Value;}
I am new to BLToolkit, and am hoping that there is a concept similar to the static Mappings I have seen, but that will allow me to easily map occurrences of these codes in other tables to their respective values. For instance:
[TableName("person")] public class Person { [MapField("person_nbr")][PrimaryKey, Identity] public int Id; [MapField("dob")][Nullable] public int BirthDate; [MapField("eye_color")][Nullable] public int EyeColorCode; [MapField("hair_color")][Nullable] public int HairColorCode; }
If EyeColorCode and HairColorCode above map to values in the Codes table, can I create an easy way to map that data within the OR classes and obtain the whole object in a single query?
I'd like to end up with something like:
// person.Id = 1// person.DOB = some date// person.EyeColor = "Blue"// person.HairColor = "Brown"
Answers
It's not really what you wanted but you could use Associations
so you could add this to your Person class
[Association(ThisKey="eye_color", OtherKey="code_nbr", CanBeNull=true)]public Code EyeColor;[Association(ThisKey="hair_color", OtherKey="code_nbr", CanBeNull=true)]public Code HairColor;
And then do something like
from p in db.Personselect new{ Id = p.Id, DOB = p.BirthDate, EyeColor = p.EyeColor.Value, HairColor = p.HairColor.Value};
Anyway these seem like the type of codes that almost never change I usually put these on the client at startup and then fill in the description when I display the data, makes everything a lot easier, and if I can't find an Id then I just refresh the collection
Thanks David. I went with your approach, but modified it slightly to make it a little less painful form me. I added associations to my classes as in your example:
[Association(ThisKey = "EyeColorCode", OtherKey = "Id")] public Code EyeColor { get; set; }[Association(ThisKey = "HairColorCode", OtherKey = "Id")] public Code HairColor { get; set; }
Then I wrote an extension method. It takes a new object, and merges all the writable properties into the source object. Using this, I don't need to specify every single property in my query, i.e.:
from p in db.Personselect p.PropertyUnion(new Person() {{ EyeColor = p.EyeColor, HairColor = p.HairColor};
This saves me quite a bit of code for some of my more complex objects, and I think it is more readable. Here is the code for the extension method:
/// <summary>/// Union by retrieving all non-null properties from source parameter and setting those properties on the instance object/// </summary>public static T PropertyUnion<T>(this T destination, T source) { // Don't copy from a null object if (Object.ReferenceEquals(source, null) || Object.ReferenceEquals(destination, null)) { return destination; } // copy properties foreach (var property in source.GetType().GetProperties()) { if (!property.CanWrite || !property.CanRead) continue; var match = destination.GetType().GetProperty(property.Name); if (!match.CanWrite || !match.CanRead) throw new MethodAccessException("Matching property '" + match.Name + "' must allow both read and write operations."); var value = property.GetValue(source, null); if (value != null && !value.Equals(Activator.CreateInstance(property.PropertyType))) match.SetValue(destination, value, null); } return destination;}
Thanks again for your help!
- BLToolkit : Mapping a code table
- BLToolkit : Table-value functions
- BLToolkit: Sogen - Code generator for BLToolkit
- BLToolkit. Mapping ObjectToObject, without copy creation.
- MAPPING TABLE
- MAPPING TABLE 的作用
- BLToolKit: how to insert a list of objects?
- Code Forces 582 A. GCD Table(数论)
- org.hibernate.mapping.Table.java
- Table Mapping in ADO.NET
- Trafodion TMUDF(Table-Mapping UDF)
- BLToolkit 简介
- BLToolkit AzureSql
- BLtoolkit : association
- Texture mapping a pyramid
- A. Table
- A - Table
- table a
- Mac下MySQL卸载方法
- ACdream 1420 High Speed Trains(容斥原理+高精度)
- Socket选项的那些事(一)
- 使用cocoaPods import导入时没有提示的解决办法
- HEVC 高级运动向量预测技术(AMVP)
- BLToolkit : Mapping a code table
- POJ 1094 Sorting It All Out(拓扑排序变形)
- HDU-1069 Monkey and Banana
- 72.UITableView:改变 TableHeaderView 的高度
- js内容不间断滚动
- java线程同步的小例子
- 在JavaScript遇到的坑(一)
- Office 365 IT Professional——如何用PowerShell创建Site Collection?
- 做一个iPhone应用需要花多少钱?