nHibernate specific example

42 views
Skip to first unread message

Alex

unread,
Jan 19, 2012, 4:52:11 PM1/19/12
to TelerikMvcGridCustomBindingHelper
Luis,

Can you please provide an example of creating the following queries
with nHibernate? I'm using nHibernate 3.2

ProductService.GetAllAsIqueryable();
ProductService.GetAllAsIqueryableForCount();

It would also be helpful to see the code of your View that's rendered
with this customBinder.

Thanks.

Alex

Luis Fernando

unread,
Jan 19, 2012, 4:57:06 PM1/19/12
to telerikmvcgridcu...@googlegroups.com
I think I already answered this in your message on Bitbucket, check your inbox, please.

Luis Fernando

unread,
Jan 19, 2012, 5:08:10 PM1/19/12
to telerikmvcgridcu...@googlegroups.com
Alex, just to let you know, right now I'm developing a special adapter for NHibernate that will work with QueryOver to support complex queries. It will be a separate library, and soon will be on Bitbucket and Nuget, look for the following nomenclature: TelerikMvcGridCustomBindingHelper.NHibernate



Alex Feldman

unread,
Jan 19, 2012, 5:09:50 PM1/19/12
to telerikmvcgridcu...@googlegroups.com
Great, sounds useful!

Alex

unread,
Jan 19, 2012, 5:31:08 PM1/19/12
to TelerikMvcGridCustomBindingHelper
Thanks, you did.

Can you please take a look at the following? This is a combination of
custom binding example provided by Telerik but using your assembly. I
get the total returned correctly, but gridModel.Data is empty. Do you
know what I'm doing wrong?
Thanks.


[GridAction(EnableCustomBinding = true)]
public ActionResult CustomBindHelper(GridCommand command)
{
var query =
MvcApplication.NorthwindSession.Query<Order>();
var queryToCount = query;

//var gridDataSource = new GridQueryProvider(query,
queryToCount);
var gridDataSource = new GridQueryProvider(query);

var gridHelper = new GridCustomBindingHelper<Order,
OrderViewModel>(command, gridDataSource);

AutoMapper.Mapper.CreateMap<OrderViewModel, Order>();
AutoMapper.Mapper.CreateMap<Order, OrderViewModel>();

gridHelper.BuildGridModel();
var gridModel = gridHelper.BuildGridModel();


//!!!!!!!!!!!!!!!!!!!!!!!!!!
//total works fine. I get a count of 830
ViewData["total"] = gridModel.Total;
//returns and empty IEnumberable
var data = gridModel.Data;
//!!!!!!!!!!!!!!!!!!!!!!!!!!

return View("MyOrdersViewModel",
(IEnumerable<OrderViewModel>)data);

Luis Fernando

unread,
Jan 19, 2012, 5:47:01 PM1/19/12
to telerikmvcgridcu...@googlegroups.com
Why not return the gridmodel? If you do not need it then try one of these alternatives;
  
      var processedData = gridHelper.ProcessedData; // Empty??
      var processedDataWithoutGrouping = gridHelper.ProcessedDataWithoutGrouping; // Empty??




2012/1/19 Alex <alexf...@gmail.com>

Alex Feldman

unread,
Jan 19, 2012, 5:57:46 PM1/19/12
to telerikmvcgridcu...@googlegroups.com
I guess I'll have to see your View example to know how to wire up the gridHelper in the View.  In the mean time that's not important.  I tried gridHelper.ProcessedData and gridHelper.ProcessedDataWithoutGrouping and got an empty result in both cases.  :(

Luis Fernando

unread,
Jan 19, 2012, 6:14:42 PM1/19/12
to telerikmvcgridcu...@googlegroups.com
Let's try to solve it.

You are using NHibernate, right?

var query = MvcApplication.NorthwindSession.Query<Order>().ToList() // returns something?

If the answers to both questions are yes. would you mind creating a test project with its minimal configuration and send me?

The configuration of my grid in the view;

@(Html.Telerik().Grid(Model).Name("Products")
        .Columns(columns =>
            {
                columns.Bound(x => x.Id).Groupable(false).Width(80);
                columns.Bound(x => x.Name).Groupable(false);
                columns.Bound(x => x.Category.Name).Groupable(true);
            })
        .EnableCustomBinding(true)
        .DataBinding(dataBinding => dataBinding.Ajax().Select<ProductController>(ctr => ctr.GetProducts(null)))
        .Sortable().Filterable().Groupable()
        .Pageable(c => c.PageSize(50)))

Luis Fernando

unread,
Jan 19, 2012, 6:34:45 PM1/19/12
to telerikmvcgridcu...@googlegroups.com
Did you have any luck for it to work?

Alex Feldman

unread,
Jan 19, 2012, 6:41:17 PM1/19/12
to telerikmvcgridcu...@googlegroups.com

Sorry for not responding earlier. I am home from work and will try your suggestion tomorrow morning.
Thank you.

Luis Fernando

unread,
Jan 19, 2012, 6:45:09 PM1/19/12
to telerikmvcgridcu...@googlegroups.com
Ok, I'll be waiting for your response. Good luck!

Alex Feldman

unread,
Jan 20, 2012, 9:33:29 AM1/20/12
to telerikmvcgridcu...@googlegroups.com
Yes, I'm using nHibernate and .ToList() returns results.

I'm attaching the sample project.  It's based on this example provided by Telerik, but with everything updated to latest releases.  You'll need Nuget 1.6 to auto get packages on build.  Home controller has my test action called CustomBindHelper. You will need to make sure you can connect to the Northwind database first.  The project currently connects to a local SQL server that contains this DB, but there is also a mdf copy of the database with the project, which you can use (you'll need to change connections sting). You'll know you're connected OK if the default route (Home/Index) loads the data into the grid.  Once you get to that point change the URL to /Home/CustomBindHelper to execute the failing code.

I may have missed something.  Let me know if you have questions.
Thanks.
Alex
TelerikGridNhibernate.7z

Alex

unread,
Jan 20, 2012, 12:19:05 PM1/20/12
to TelerikMvcGridCustomBindingHelper
By the way, here are instructions that I found useful for installing
the Northwind database in sql server 2008
http://www.codeproject.com/KB/database/NorthwindAndPubs_2008.aspx

On Jan 20, 9:33 am, Alex Feldman <alexfeld...@gmail.com> wrote:
> Yes, I'm using nHibernate and .ToList() returns results.
>
> I'm attaching the sample project.  It's based on this
> example<http://www.telerik.com/community/code-library/aspnet-mvc/grid/nhibern...>provided
> by Telerik, but with everything updated to latest releases.
>  You'll need Nuget 1.6 to auto get packages on build.  *Home *controller
> has my test action called *CustomBindHelper. *You will need to make sure
> you can connect to the Northwind database first.  The project currently
> connects to a local SQL server that contains this DB, but there is also a
> mdf copy of the database with the project, which you can use (you'll need
> to change connections sting). You'll know you're connected OK if the
> default route (*Home/Index*) loads the data into the grid.  Once you get to
> that point change the URL to /Home/CustomBindHelper to execute the failing
> code.
>
> I may have missed something.  Let me know if you have questions.
> Thanks.
> Alex
>
> On Thu, Jan 19, 2012 at 6:45 PM, Luis Fernando <luisfernand...@live.com>wrote:
>
>
>
>
>
>
>
> > Ok, I'll be waiting for your response. Good luck!
>
>
>
>  TelerikGridNhibernate.7z
> 1452KViewDownload

Luis Fernando

unread,
Jan 20, 2012, 4:02:25 PM1/20/12
to telerikmvcgridcu...@googlegroups.com

Alex, thank you for your example.

I will investigate the source of the problem and when I find out the cause I will inform you.

Anyway, after seeing your difficulty in making it work, I have created a sample project with the required configuration.

You can grab it on my Bitbucket repository or use this direct link: https://bitbucket.org/Lunadie/telerikmvcgridcustombindinghelper/downloads/TelerikMvcGridCustomBindingHelper.Examples.rar


From now on let's keep the conversations on the forum. In this way we help more people.

Fórum: http://groups.google.com/group/telerikmvcgridcustombindinghelper

Luis Fernando

unread,
Jan 20, 2012, 4:10:38 PM1/20/12
to telerikmvcgridcu...@googlegroups.com
Forget what I said about the forum in the last message, you're already using it. 
330.gif

Luis Fernando

unread,
Jan 20, 2012, 6:17:45 PM1/20/12
to telerikmvcgridcu...@googlegroups.com
I made the changes necessary for your project work.

I left some notes where I made the changes.

The files that have changed are:
  1. Web.config
    • only the connectionString
  2. MyOrdersViewModel.aspx
    • // Add this line
    • .DataBinding(dataBinding => dataBinding.Ajax().Select("CustomBindHelper", "Home"))
    • // And remove this line
    • .Pageable(paging => paging.Total((int)ViewData["total"]))
  3. HomeController.cs
    • // Basically added this method
    • public ActionResult MyOrdersViewModel() { return View(); }
    • // And a lot of changes on this other (see the  attached the sample project.)
    • public ActionResult CustomBindHelper(GridCommand command)
Well, that's it. Check the attached project and also the example in the repository on Bitbucket.

I hope I was helpful.
Good luck!
TelerikGridNhibernate_FIXED.rar

Alex Feldman

unread,
Jan 20, 2012, 8:09:16 PM1/20/12
to telerikmvcgridcu...@googlegroups.com
OK, everything is working now.  I like your fixed example because it's using Ajax binding.  I was actually trying to do server binding in my example, as demonstrated here:  http://demos.telerik.com/aspnet-mvc/grid/customserverbinding 

I got server binding to work successfully in the broken project I sent you.  My first bug, the reason results were empty, was due to the initial call to the action producing an empty GridCommand object (unless you specify the GridCommand options on the first call somehow).  If you look at the example, there needs to be a check for that like this:

if (command.PageSize == 0)
    command = new GridCommand();

I also had a few bugs in my View.  When Telerik asks for an IEnumerable model they're not kidding.  IEnumerable<OrderViewModel> will break GroupBy results.  So model must be IEnumerable.  Also, I forgot to add the line 
.DataBinding(dataBinding => dataBinding.Server().Select("CustomBindHelper", "Home"))
to my View.  So, once I got all those bugs (and maybe a few other small ones) resolved I was able to get custom server binding to work with your helper as well.  Everything worked just like with the custom binding.

I'm wondering if your helper should do the empty GridCommand object check, and maybe throw an exception?  An exception may be too drastic...what do you think?

So that's it.  I actually had to verify this after work hours since I just couldn't wait until Monday.  I will definitely use your custom binding helper in my project.  I'm also curious to see how the QueryOver plugin works, since I really like QueryOver approach more than Linq.

Have a great weekend.  Thank you very much for your work and support.

Alex

Luis Fernando

unread,
Jan 23, 2012, 6:31:37 AM1/23/12
to telerikmvcgridcu...@googlegroups.com
Oh, of course, I was completely blind and ignored the ServerBinding, but I've done an update that fixes the problem, I chose to recreate a new GridCommand if it is not initialized correctly. Please check the sample project, not the one on the downloads page, but straight to the source.

Support for NHibernate QueryOver API has been added to a project apart, see it here: https://nuget.org/packages/TelerikMvcGridCustomBindingHelper.NHibernate

I am also working hard on better integration with AutoMapper, and soon there should be another update, the project is growing fast...

Please inform me of any bugs or difficulties you encounter.

Alex

unread,
Jan 23, 2012, 9:25:14 AM1/23/12
to TelerikMvcGridCustomBindingHelper
The new command support looks good. I will write my other comments in
new topics to better manager the different issues.

Thanks.

Alex
Reply all
Reply to author
Forward
0 new messages