Let's say that i have an OrderDetails object, that is composed of OrderDetail objects. Each OrderDetail object(that is an Editable Child) have a Price field of type Money.
Money is a normal class that hold an Amount field of type integer...
I think that this is a normal object oriented design ! Money encapsulate different thinks related to money:Amount,Currency ... and it have a clear responsibility:Converting from currency to another for example, And (or) formating the Amount value to a string value...
Now the problem is in data binding. I can't bind any control to this field. the TextBox control for example require a field of a primitive type(integer,double,string...).
I am now obliged to create a new control that accept a Money object (the same problem exist with DataGridView). or I am obliged to create 2 properties in the OrderDetail object, one of type Decimal, and the other of type Money, so when the value of the first property change, the second property also change. and this is not a good design. the same problem exist when I try to fetch the Money property value from the DataBase. it is persisted as a Decimal data type, so I am obliged to create 2 properties, one is Private and of Decimal data type, and the other is public of Money data type. when the first change, the second automatically change ... ( I am obliged to use this technic because I use an object persistence framework...).
so what I should do in this situation, what is the best solution, specially when i think that CSLA is about creating "Data Collector Objects", that is, an object that is responsible to collect data and valiadte it( it is similar to DataSet !), so CSLA always deal with data binding...
Sorry for my bad english...
You could try to wrap your OrderDetail object in a façade that would flatten your design. This would make binding easier.
Maybe I'm missing something in the discussion, but I have many custom value types (structs) defined in our framework, Currency being one of them. I do not have a problem databinding to properties of these types.
Only thing I can think of causing a problem is if you have not overriden the ToString() method. All of my value types implement IConvertible, override ToString and have an implicit conversion to string that delegates to the ToString method. Maybe your problem lies somewhere in here.
HTH
I think you should implement a ICustomTypeDescriptor on your Detail class. For details please visit.
http://msdn.microsoft.com/msdnmag/issues/05/04/NETMatters/
http://msdn.microsoft.com/msdnmag/issues/05/05/NETMatters/
For a collection of Details you could implement ITypeList
thinks for all the participants in this discussion and i found the solution.
Simply a create a MoneyConverter class that inherits from TypeConverter:
Public
Class MoneyConverterInherits System.ComponentModel.TypeConverter
Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
Debug.WriteLine(
"CanConvertFrom " & sourceType.ToString) Return sourceType Is GetType(String) End Function Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As BooleanDebug.WriteLine(
"CanConvertTo " & destinationType.ToString) Return destinationType Is GetType(String) End Function Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As ObjectDebug.WriteLine(
"ConvertFrom " & value.ToString) Return New Money(value.ToString) End Function Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As ObjectDebug.WriteLine(
"ConvertTo " & value.ToString & "(" & destinationType.ToString & ")") Return value.ToString End FunctionEnd Class
Now i will decorate my Money class with this attribute:
<System.ComponentModel.TypeConverter(GetType(MoneyConverter))>
I have tested this in a DataGridView and it is worked fine !!
Think's again and good luck...
Copyright (c) Marimer LLC