Monday, April 9, 2012
Monday, January 23, 2012
Poor Man's Implementation of Dependency Object
Points to Note:
* Dependency Object is an abstract class. In only provides helper methods to deriving classes to be able to use Dependency property (Well, that is a lot of work because DependencyObject also takes care of the facilities provided by DependencyProperty).
* We have one instance of DependecyObject for each control. Thus, one instance of DependencyProperty-Value map. That makes it easy for a control to be able to store and retrieve value for a any dependency property.
* Again, DependencyObject maintains an instance level mapping between dependecyProperty and it's value (if not set by user/control then uses the default value - saves memory)
* How are attached properties stored in DependencyObject ? It's the same. For attached properties controls create a wrapper static property which accepts DependencyObject and then call Set/Get on the dependencyObject. Both of the set methods are passed the target object to which the property is to be attached (Note: In all this process there is NO role of the instance of the object which has defined the attached property).
* Is it allowed to Register a property in one type with owner as another type? Looks like yes, it is possible. Of course with condition that the owner does not already register a property with same name.
public abstract class MockDependenctObject
{
private readonly Dictionary _dp2Value;
protected MockDependenctObject()
{
_dp2Value = new Dictionary();
}
public void SetValue(MockDependencyProperty dependencyProperty,object value)
{
_dp2Value[dependencyProperty] = value;
}
public object GetValue(MockDependencyProperty dependencyProperty)
{
if(_dp2Value.ContainsKey(dependencyProperty))
{
return _dp2Value[dependencyProperty];
}
return null;
}
}
* Dependency Object is an abstract class. In only provides helper methods to deriving classes to be able to use Dependency property (Well, that is a lot of work because DependencyObject also takes care of the facilities provided by DependencyProperty).
* We have one instance of DependecyObject for each control. Thus, one instance of DependencyProperty-Value map. That makes it easy for a control to be able to store and retrieve value for a any dependency property.
* Again, DependencyObject maintains an instance level mapping between dependecyProperty and it's value (if not set by user/control then uses the default value - saves memory)
* How are attached properties stored in DependencyObject ? It's the same. For attached properties controls create a wrapper static property which accepts DependencyObject and then call Set/Get on the dependencyObject. Both of the set methods are passed the target object to which the property is to be attached (Note: In all this process there is NO role of the instance of the object which has defined the attached property).
* Is it allowed to Register a property in one type with owner as another type? Looks like yes, it is possible. Of course with condition that the owner does not already register a property with same name.
public abstract class MockDependenctObject
{
private readonly Dictionary
protected MockDependenctObject()
{
_dp2Value = new Dictionary
}
public void SetValue(MockDependencyProperty dependencyProperty,object value)
{
_dp2Value[dependencyProperty] = value;
}
public object GetValue(MockDependencyProperty dependencyProperty)
{
if(_dp2Value.ContainsKey(dependencyProperty))
{
return _dp2Value[dependencyProperty];
}
return null;
}
}
Sunday, January 22, 2012
Poor Man's Implementation of Dependency Property
Points to note:
* Attached event has IsRegistered=false.
* Key of a dependency property = PropertyName ^ OwnerType.
* Check for a static field with name = PropertyName + Property.
public class MockDependencyProperty
{
private static readonly Dictionary _hashCode2MockDependencyProperty;
private readonly bool _isEvent;
private readonly bool _isRegistered;
private readonly Type _ownerType;
private readonly string _propertyName;
private readonly Type _propertyType;
static MockDependencyProperty()
{
_hashCode2MockDependencyProperty = new Dictionary();
}
private MockDependencyProperty(string propertyName, Type propertyType, Type ownerType, PropertyMetadata metadata,
Type validatorType, bool isRegistered)
{
_propertyName = propertyName;
_ownerType = ownerType;
_propertyType = propertyType;
_isRegistered = isRegistered;
//Out of scope for this discussion
_isEvent = typeof (Delegate).IsAssignableFrom(_propertyType);
}
public static MockDependencyProperty Register(string name, Type propertyType, Type parentType)
{
return ValidateAndRegister(name, propertyType, parentType, null, null, true);
}
public static MockDependencyProperty RegisterAttached(string name, Type propertyType, Type parentType)
{
return ValidateAndRegister(name, propertyType, parentType, null, null, false);
}
private static MockDependencyProperty ValidateAndRegister(string name, Type propertyType, Type ownerType,
PropertyMetadata defaultMetadata, Type validatorType,
bool isRegistered)
{
//All Null Checks
FieldInfo field = ownerType.GetField(name + "Property",
BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.DeclaredOnly);
if (field == null)
{
throw new ArgumentException("Error_DynamicPropertyNotSupported" + ownerType.FullName + name);
}
var property = new MockDependencyProperty(name, propertyType, ownerType, defaultMetadata, validatorType,
isRegistered);
lock (((ICollection) _hashCode2MockDependencyProperty).SyncRoot)
{
if (_hashCode2MockDependencyProperty.ContainsKey(property.GetHashCode()))
{
throw new InvalidOperationException("Error_DPAlreadyExist" + ownerType.FullName);
}
_hashCode2MockDependencyProperty.Add(property.GetHashCode(), property);
}
return property;
}
public override int GetHashCode()
{
string propertyName = _propertyName;
Type ownerType = _ownerType;
return GetHashCode(propertyName, ownerType);
}
private static int GetHashCode(string propertyName, Type ownerType)
{
return propertyName.GetHashCode() ^ ownerType.GetHashCode();
}
//Properties
}
* Attached event has IsRegistered=false.
* Key of a dependency property = PropertyName ^ OwnerType.
* Check for a static field with name = PropertyName + Property.
public class MockDependencyProperty
{
private static readonly Dictionary
private readonly bool _isEvent;
private readonly bool _isRegistered;
private readonly Type _ownerType;
private readonly string _propertyName;
private readonly Type _propertyType;
static MockDependencyProperty()
{
_hashCode2MockDependencyProperty = new Dictionary
}
private MockDependencyProperty(string propertyName, Type propertyType, Type ownerType, PropertyMetadata metadata,
Type validatorType, bool isRegistered)
{
_propertyName = propertyName;
_ownerType = ownerType;
_propertyType = propertyType;
_isRegistered = isRegistered;
//Out of scope for this discussion
_isEvent = typeof (Delegate).IsAssignableFrom(_propertyType);
}
public static MockDependencyProperty Register(string name, Type propertyType, Type parentType)
{
return ValidateAndRegister(name, propertyType, parentType, null, null, true);
}
public static MockDependencyProperty RegisterAttached(string name, Type propertyType, Type parentType)
{
return ValidateAndRegister(name, propertyType, parentType, null, null, false);
}
private static MockDependencyProperty ValidateAndRegister(string name, Type propertyType, Type ownerType,
PropertyMetadata defaultMetadata, Type validatorType,
bool isRegistered)
{
//All Null Checks
FieldInfo field = ownerType.GetField(name + "Property",
BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.DeclaredOnly);
if (field == null)
{
throw new ArgumentException("Error_DynamicPropertyNotSupported" + ownerType.FullName + name);
}
var property = new MockDependencyProperty(name, propertyType, ownerType, defaultMetadata, validatorType,
isRegistered);
lock (((ICollection) _hashCode2MockDependencyProperty).SyncRoot)
{
if (_hashCode2MockDependencyProperty.ContainsKey(property.GetHashCode()))
{
throw new InvalidOperationException("Error_DPAlreadyExist" + ownerType.FullName);
}
_hashCode2MockDependencyProperty.Add(property.GetHashCode(), property);
}
return property;
}
public override int GetHashCode()
{
string propertyName = _propertyName;
Type ownerType = _ownerType;
return GetHashCode(propertyName, ownerType);
}
private static int GetHashCode(string propertyName, Type ownerType)
{
return propertyName.GetHashCode() ^ ownerType.GetHashCode();
}
//Properties
}
Garbage Collection and Large Object Heap.
I have posted a query on SO for better understanding of LOH, that was mainly driven by a bad answer in an interview. Following is the better answer.
Garbage collector, other than providing support for removing unused objects from heap, also support memory compation i.e. removing fragments from memory resulted in removing the objects that were not being used. Memory compaction is helpful because objects are stored in memory in contiguous memory locations, and if there are multiple fragment (note: fragment means unutilized spaces in memory) than a object may not get enough space and trigger GC for that generation, which may not be required.
The other facility, or advantage, that Large Object Heap provides is no memory compaction, that is no movement of used objects once some objects have been removed. This is done as a performance measure considering that moving too many objects from one memory location to another would consume CPU and the benefit may not be enough to compensate for that. As this article illustrates. Compacting large objects costs a lot. Similar with the examples in the article, say moving a byte in memory needs 2 cycles, then compacting a 8MB object in a 2GHz computer needs 8ms, which is a large cost.
Now, against the argument: why do we need LOH? It is only for the benefit of separating it from Generation 0 (an object gets memory in Gen0 or LOH), since Gen0 support memory compaction, which is useful for small object and not for large ones. Thus, a separate memory which does not support memory compaction, pins the large objects in heap.
Garbage collector, other than providing support for removing unused objects from heap, also support memory compation i.e. removing fragments from memory resulted in removing the objects that were not being used. Memory compaction is helpful because objects are stored in memory in contiguous memory locations, and if there are multiple fragment (note: fragment means unutilized spaces in memory) than a object may not get enough space and trigger GC for that generation, which may not be required.
The other facility, or advantage, that Large Object Heap provides is no memory compaction, that is no movement of used objects once some objects have been removed. This is done as a performance measure considering that moving too many objects from one memory location to another would consume CPU and the benefit may not be enough to compensate for that. As this article illustrates. Compacting large objects costs a lot. Similar with the examples in the article, say moving a byte in memory needs 2 cycles, then compacting a 8MB object in a 2GHz computer needs 8ms, which is a large cost.
Now, against the argument: why do we need LOH? It is only for the benefit of separating it from Generation 0 (an object gets memory in Gen0 or LOH), since Gen0 support memory compaction, which is useful for small object and not for large ones. Thus, a separate memory which does not support memory compaction, pins the large objects in heap.
Subscribe to:
Posts (Atom)