Many-To-One relationships in NHibernate - asp.net

I am still trying to learn NHibernate best practices, so please be easy on me... :-)
I have a Product class with a Many-To-One relationship set up to a Category class. On my Add A Product page, I am loading a DropDownList with the Categories. Now when the user enters the product information and clicks submit, I have to pull down the Category by ID (DropDownList SelectedValue) from the database to populate the Category property of my Product object so I can save it.
It seems like the Category by ID lookup is a waste. Is there a way I can simply use the ID value that was retrieved from the DropDownList?
Thanks for any advice!

Use ISession.Load() to create a proxy object. eg:
myProduct.Category = session.Load<Category>(userSuppliedCategoryId);
Note that the Load() method doesn't actually hit the database unless you try accessing a property besides the primary key, so there is no extra DB hit.
Ayende has a good post about this

Related

configure entitydatasource with entity set that includes multiple tables

I have an entity data model with two entities. One entity called Posts contains blog posts and another entity called Comment contains comments. I set up the association as a one to many relationship and the primary keys are both postid in both Posts and Comment.
However, when I try to bind this to an entitydatasource control it only lets me select either the Posts or the Comment entity set not both. What I want to do is to be able to bind the post with its comments to the data source, but I can't seem to figure out a way to do this.
Any help would be appreciated!

When assigning values to EntityRef ID fields in Linq to Sql, can EntityRef still delay load?

I've got an ASP.NET MVC app that uses Linq to Sql for data access.
Say I have two objects: An Order object that has a foreign key to a Customer object by CustomerID. So, in my Order class, you would see two properties: an int CustomerID field, and an EntityRef member accessible by a Customer property.
When the edits or submits an Order, my MVC app will update the CustomerID field directly of the Order class, instead of updating the Customer property. This saves us from having to fetch a customer record, and I can use the default model binding code to fill the property automatically as long as the submitted form request has a customerID entry.
This works ok, however, later on in some other part of the code--say a business rules portion, some logic will access the Customer property of the Order object. For example:
if (order.Customer.HasPreviousOrders) then ...
Even though the CustomerID field is set, the Customer field is null, so this business rule throws an exception.
I know Linq 2 Sql uses EntityRefs to do delayed loading. My question is: is there a way to trigger the delayed loading on an object's EntityRef if the ID field has been modified?
We have a dynamic rules engine, so I don't have control of what foreign key objects are going to be needed. I'd rather not have to go through all my controllers to set the EntityRef<> values directly.
Thanks for the help.
Ok, no takers. It looks like what I'm trying to do is just not doable--or maybe not a good idea.
I went ahead and implemented code so I am setting the association object property instead of the ID property so the business rules can be processed.

Drupal: Display only specific NodeReferrer field in Views

I have a content type appointment with a date field that references nodes of the content type person using the Nodereference module. In the content type person I added a Nodereferrer field that shows the reverse of this references (Person -> appointments).
I now want to create a view of person nodes that shows the last appointment date of that person. I can create a View of persons with a relationship to appointments that displays all appointments, but I have no idea on how to display only the most recent one.
Any ideas on how to achieve this?
Personally I have not had much success with using views and node reference. It never seems to work out the relationships properly.
So my advice would be to write your own query. If you look here you can see a way to override the SQL generated by views, so you still get a lot of the goodness which comes with views.
By the way I would be very interest to see if there is a better answer than this
Instead of adding the Nodereferrer field to the view, try adding the Nodereferrer relationship then adding the Node title (using that relationship) as a field. You should be able to sort from newest to oldest, and then set the view to be Distinct so that only the first row for each person shows up.

Business logic layer, multiple tables, relationship

In the business logic we map, Tables to Objects and fields of this table to properties.
What to do in one-to many relationship? Just an example: I have Table, Products and Categories.
I need to drag all products and instead of Category_ID (Products table) need to display actual Category name wich is stored in Category table.
What is propal way of doing it? In similar situations?
You can have a property on the Products Entity that is of type Category Entity. Check this link out from the EntitySpaces (an ORM for .Net) documentation for ideas:
link text
Also, you may want to look into using an ORM.
Either use an ORM like LINQ to SQL or ADO.NET Entity Framework or strongly-typed datasets. But if you want to custom code your business layer, your Category class could have a Products collection property which contains those products, loaded from the DB, which you could infer the name.
HTH.

ASP.NET and objects

Let's say I have a class Person, with a string[] nickNames, where Person can have 0 or more nicknames stored. I want to create an asp.net page where a user can go and add/edit/delete nicknames.
Question is- how to i persist the Person object between postbacks? I query the DB and create the object then display it on a form, but then the user has the option to edit/delete fields of that object.. once the page is displayed with the fields of Person, how do I update that object with the changes the user made, to store to db?
Thanks!
Well if your Person Object is serializable you could store it in ViewState and if not, you could stick it in Session, but it sounds like you might have a general lack of understanding about Data Persistance in general
Depending on your implementation, and whether you're coding this all by hand or using the built in DataSource/DataAdapter controls, theres a bunch of ways to do it.
You could have a look at some basic ASP.NET/ADO.NET Tutorials to point you in the right direction
http://aspnet101.com/aspnet101/tutorials.aspx?id=17
Query the object it again (you could store it in a session variable but that doesn't scale), gather and apply changes from user upon postback.

Resources