What about this approach?
I've created a BussinesObject Factory -->
Code:
public class FactoryBO
{
private static Type[] get_interfaces()
{
Type[] interfaces = new Type[3];
interfaces[0] = typeof(System.ComponentModel.IEditableObject);
interfaces[1] = typeof(System.ComponentModel.INotifyPropertyChanged);
interfaces[2] = typeof(System.ComponentModel.IDataErrorInfo);
return interfaces;
}
public static UserBO createUserBO()
{
return (UserBO)new Castle.DynamicProxy.ProxyGenerator().CreateClassProxy(typeof(UserBO), FactoryBO.get_interfaces(), new BusinessObjectInterceptor<Model.Entities.User>());
}
public static UserBO createUserBO(Model.Entities.User user)
{
return (UserBO)new Castle.DynamicProxy.ProxyGenerator().CreateClassProxy(typeof(UserBO), FactoryBO.get_interfaces(), new BusinessObjectInterceptor<Model.Entities.User>(), new object[] { user.Name, user.Second_name });
}
}
As you can see, the finallity is create a Bussines Object with IEditableObject, INotifyPropertyChanged, IDataErrorInfo implementation.
So, If you has your Model Entities, for example User, you could do something as:
Code:
public class UserBO : Model.Entities.User
{
public UserBO() : base()
{
}
public UserBO(string name, string second_name) : base(name, second_name)
{
}
}
A Bussines Object that inherits from our Model Entities!!!
So, mmm
Code:
public class UsersFormFactory : WinView.Classes.FormsFactory<WinView.Forms.UsersForm, Model.Entities.User>
{
public UsersFormFactory()
{
}
public override WinView.Forms.UsersForm getEmptyForm()
{
return new WinView.Forms.UsersForm(WinView.BusinessObjetcs.FactoryBO.createUserBO());
}
public override WinView.Forms.UsersForm getFilledForm(Model.Entities.User user)
{
return new WinView.Forms.UsersForm(WinView.BusinessObjetcs.FactoryBO.createUserBO(user));
}
}
As you can see I've implemented a FormFactory in order to create a UserForm with user data (from Model (NHibernate)). This function creates a UserBO (that implments IEditableObject, INot... interfaces). When Form contructor is performed -->
Code:
public UsersForm(WinView.BusinessObjetcs.UserBO user) : this()
{
this.user = new Model.Entities.User("nom", "cognom");
this.userBindingSource.DataSource = this.user;
}
All works very well, however there is something that:
Castle.DynamicProxy.ProxyGenerator().CreateClassProxy(typeof(UserBO), creates a new instance of UserBO, so the object returned isn't the original object that we've get from NHibernate!!! So, I'm creating a copy of my entity with binding capabilities. The total solution would be can wrapper NHibernate Entity on Binding features. I need to solve it, yet.
This is a first approach in order to obtain a possible framework. I believe that have to be able to make a framework that creates bindable features on existing objects.
What do you thing about all?