Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate POCO / Basics

I've formerly used L2S and am looking at using NHib along with the Sharp Architecture on a project. I've started prototyping and come up against the first issue which i have no idea how to google for.

Given a POCO with some simple properties, and one reference property (Category - class not shown here):

public class Post
{
  public Post()
  {
    this.DateCreated = DateTime.Now;
  }

  public virtual string Title {get;set;}
  public virtual DateCreated {get;set;}
  public virtual Category {get;set;}
}

This is handy if i want to say produce a summary of posts in tabular format - if i want a column named "Category" and i want to show the category title i can simply use Post.Category.Title.

However a user creates a new post (they select a category from a drop down list or similar). They click submit, and i new up a Post object. However before i can persist the new Post, i have to retrieve an instance of the Category (by id) to assign to the Category property?

How to get the best of both worlds? If i update the Post POCO, making the Category an int, I've made it simpler to create new instances. But harder for some rendering code which will now have to resolve the category name given the Id?

I feel i'm missing some basic concept here?

I know with Linq to Sql given a schema where the Post table had an integer foriegn key column named CategoryId, would generate me both the underlying table column (CategoryId) and and EntitySet which contained the foreign key row.

How to acheive similar in NHibernate? How is this generally managed?

Thanks

like image 231
6footunder Avatar asked Nov 19 '09 00:11

6footunder


People also ask

What is the difference between NHibernate and fluent NHibernate?

Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.

What is NHibernate in .NET core?

NHibernate is the ORM, an object-relational platform that works on the ASP.Net core. Therefore, we can easily deploy the core project developed in ASP.NET that uses NHibernate on the Linux server without any effort. The application works in the same way as it will do on a windows platform.


2 Answers

Associated entities should be mapped as entities - e.g. Category, not int CategoryId.

When you're creating a new Post, if you don't have an actual Category instance (and why not? if you're picking categories from a list you've already loaded them) but only have its id you can use a Load() method to get a persistent Category instance for given id without actually hitting a database:

post.Category = (Category) sesssion.Load(typeof(Category), categoryId);

You can also use the generic version:

post.Category = session.Load<Category>(categoryId);
like image 159
ChssPly76 Avatar answered Oct 19 '22 03:10

ChssPly76


In NHibernate, you would use a reference property.

Using fluent mapping, it looks something like this:

mapping.References(x => x.Category, "CategoryId").PropertyRef(x=>x.Id).Cascade.All();

in XML, it's roughly

<many-to-one name="Category" column="category_id" class="YourNamespace.Category" cascade="all" property-ref="Id" />

You can use not-found to pick the behavior when there's no associated category.

Essentially, you want the semantics of the category object, not the ID, so you just tell NHibernate how they are associated.

like image 1
JasonTrue Avatar answered Oct 19 '22 02:10

JasonTrue