cwlaualex wrote:
I'm new to NHibernate and I'm testing it right now as I can't find any simple solution in saving master/detail records in a single transaction using DataSet.
I have already built the models and able to read and update record from DB. However, the problem comes up when I try to make it work with the databinding. I don't know how to databind my model to the web UI. For display only, it's fine to set the DataSource to my model directly in source.
Me.GridView1.DataSource = objName
Me.GridView1.DataBind()
However, if I want to have those update ability, it seems that it's better to create an ObjectDataSource and databind to it. However, how can my ObjectDataSource link up with my model? Please let me know if there's any tutorial available.
I found that some people will suggest making a facade? However, I've no idea what's it. I'm not strong in OO design. So, please give me a path to achieve my goal. Thanks!
Something like this:
Code:
[DataObject]
public class ObjectDataSourceHelper
{
[DataObjectMethod(DataObjectMethodType.Select)]
public IList<Foo> FooFindAll(string searchText, string orderBy, int startRowIndex, int maxRows)
{
IQuery q = session.CreateQuery(MyQueries.GetAllFooHQL);
q.SetFirstResult(startRowIndex);
q.SetMaxResults(maxRows);
q.SetString("mySearchText", searchText);
List<Foo> results = new List<Foo>();
foreach (Foo item in q.List())
{
results.Add(item);
}
return results;
}
[DataObjectMethod(DataObjectMethodType.Select)]
public Foo FooGetSpecific(int fooId)
{
return session.Get(typeof(Foo), fooId) as Foo;
}
[DataObjectMethod(DataObjectMethodType.Insert)]
public int FooInsert(Foo newFoo)
{
session.SaveOrUpdate(newFoo);
return newFoo.ID;
}
[DataObjectMethod(DataObjectMethodType.Insert)]
public int FooInsert(string prop1, int prop2, DateTime prop3)
{
Foo newFoo = new Foo();
newFoo.Prop1 = prop1;
newFoo.Prop2 = prop2;
newFoo.Prop3 = prop3;
return FooInsert(newFoo); // call previously declared overload.
}
}
Basicly, this is a simple wrapper around NHibernate or a wrapper around your DataLayer that is itself a wrapper around NHibernate. You mark it with attributes that identify it as a DataObject and idenitify the purpose of the individual methods, as that helps the GUI tools pick the right objects and methods for you. Likewise, the naming convention of FooInsert is preferred by some over InsertFoo because of the way the GUI tools will sort the methods.
The ObjectDataSource then looks at all the parameters it has and tries to find the right method signature that has all the right parameters. Be sure to specify the DataKeyName on all your GridViews and such.
It's not without pain and it's definitely time consuming. Is the tradeoff worth it? I haven't completely decided myself. For read-only stuff, I'd definitely say just stick with binding directly to strongly-type collections returned directly from your DataLayer. But once you do get it working, it gets easier and easier the farther you go with it.
Perhaps most importantly, any other ASP.NET developer can work on your UI without having to know NHibernate first. Seeing how bloody hard it is to find anyone who already knows NHibernate on short notice, that's a very good thing.