SharePoint Entity Framework 3 – Customizing conversion logic

The preceding articles introduced how to use SharePoint Entity Framework to associate and convert SharePoint built-in fields. SharePoint Entity Framework also provides some extension mechanisms to help us customizing conversion logic, adapt custom field and custom property type.

Custom Conversion Logic

In the first article we mentioned, Entity class contains a Read method and a Write method, used to read List Item and write List Item. Actually both the two methods are virtual, we can override them accordingly to add our conversion logic.

For instance, there is a sample entity class:

public class SampleEntity: Entity
{
[Field("Field1")]
public int Property1 { get; set; }

public string Price { get; set; }

public override void Read(Microsoft.SharePoint.SPListItem item, Microsoft.SharePoint.SPRegionalSettings regionalSettings = null)
{
base.Read(item, regionalSettings);

this.Price = "$" + item["Money"].ToString();
}

public override void Write(Microsoft.SharePoint.SPListItem item, Microsoft.SharePoint.SPRegionalSettings regionalSettings = null)
{
base.Write(item, regionalSettings);

item["Money"] = double.Parse(this.Price.Substring(1));
}
}

This SampleEntity overrides Read and Write method of base class, add the logic to convert between Money field and Price property. We noted Prices property is not decorated by FieldAttribute (or any child class of it), it means this property doesn’t need to be handled by SharePoint Entity Framework.

However Property1 is associated with Field1 field by using FieldAttribute, so this property will be converted by the default conversion logic of SharePoint Entity Framework.

Note: If we overrides Read or Write method, but not invoke the corresponding method of base class inside the method implementation, we will disable the default conversion logic of SharePoint Entity Framework.

Adapt Custom Field

SharePoint Entity Framework supports most SharePoint built-in Field type, and also provides the mechanism to associate and convert custom Field types.

Presume SampleEntity class contains a property like:

[Field("CustomField")]
public string Value { get; set; }

CustomField is a custom Field, below is its implementation:

public class CustomField : SPField
{
public override Type FieldValueType
{
get
{
return typeof(CustomFieldValue);
}
}

public CustomField(SPFieldCollection fields, string fieldName) : base(fields, fieldName){ }
}

[Serializable]
public class CustomFieldValue
{
public string Value { get; set; }

public CustomFieldValue(){ }

public CustomFieldValue(string value)
{
this.Value = value;
}

public override string ToString()
{
return this.Value;
}
}

If we want SharePoint Entity Framework can identify and convert this field automatically, we need to write a converter for it. In SharePoint Entity Framework, all converter need to implement IValueConverter interface (located in WindStyle.SPEntity.Converters namespace). Below code snippet shows how to implement a converter for CustomField:

using WindStyle.SPEntity.Converters;

public class CustomFieldConverter : IValueConverter
{
public object ToFieldValue(object value, Type targetType, ConversionContext context)
{
return new CustomFieldValue(value as string);
}

public object ToPropertyValue(object value, Type targetType, ConversionContext context)
{
if (targetType == typeof(string))
{
return (value as CustomFieldValue).Value;
}

return null;
}
}

IValueConverter interface contains a ToFieldValue and a ToPropertyValue method, used to convert property value to field value and convert field value to property value. The value argument means original value, targetType argument means the target type to convert, context argument provides some contextual information to help converting more precisely, such as SPListItem, SPRegionalSettings and the FieldAttribute type applied to the property.

Then associate the CustomField class with CustomFieldConverter by using ConverterAttribute:

using WindStyle.SPEntity.Attributes;

[Converter(typeof(CustomFieldConverter))]
public class CustomField : SPField
{
//…
}

Now SharePoint Entity Framework can identify and handle CustomFIeld field correctly. SharePoint Entity Framework will read the value from CustomField field (or write value to CustomField field) by using CustomFieldConverter, just like handle a SharePoint built-in field.

Adapt Custom Property Type

Besides adapt custom field type, ConverterAttribute and IValueConverter also can adapt custom property type. The usage is totally same.

Let’s demonstrate it through below code snippet, presume SampleEntity class contains a proeprty like:

[Field("Field2")]
public CustomProperty Property1 { get; set; }

CustomProperty is a custom type, below is its implementation:

[Converter(typeof(CustomPropertyConverter))]
public class CustomProperty
{
public char[] Chars { get; set; }
}

The converter associated with CustomProperty is CustomPropertyConverter:

public class CustomPropertyConverter : IValueConverter
{
public object ToFieldValue(object value, Type targetType, ConversionContext context)
{
return new string((value as CustomProperty).Chars);
}

public object ToPropertyValue(object value, Type targetType, ConversionContext context)
{
return new CustomProperty()
{
Chars = value.ToString().ToCharArray()
};
}
}

The only thing to note is SharePoint Entity Framework will firstly try to find the converter of custom property type. If the property type doesn’t have a converter, SharePoint Entity Framework will try to find the Field type associated with the property by using FieldAttribute, then find the appropriate converter.

At this point, the major features of SharePoint Entity Framework was all introduced. Welcome to use and don’t forget it is fully open source, download it now!

发表评论

电子邮件地址不会被公开。 必填项已用*标注