NHIBERNATE: Add the entity to many-to-many collections only via ID

I am looking for shortcuts. I have some NH entities with many-to-many relationships. Something like this:

 public class Customer: EntityBase
{
public virtual IList Categories {get; set; }
}

public class Category: EntityBase
{
public virtual IList Customers {get; set; }
}

Please remember that this is a very simple description, so resist the temptation , It is recommended that I do not use a many-to-many arrangement, or I should use value types or something similar.

On the database side, this relationship is done through a separate table with two columns – Customer_Id And Category_Id.

I really want to be able to add existing categories to customers without having to retrieve the complete entity from the database. Maybe something like this:

customerEntity.Categories.Add(new Category {Id = 2 });

The reason is that the application is an ASP.NET MVC application and I am using ViewModels as my views. These Customers The ViewModel ends with List. For category selection, when I map the ViewModel to the corresponding Customer entity, I want to be able to suck these category IDs into the category list without having to hit the database first to retrieve them.

Part of the reason I want to be able to do this is that I want to minimize database calls, but I also want my mapper class to be able to create Customer entities without calling my service layer to seek other items… This seems to be a bad design I also want to avoid adding another layer to call the mapper, and then perform other mapping content that gets the entity from the repository (it itself is accessed through the domain service layer).

I checked the idbag, but for One I am using Fluent NHibernate and it does not support the construct, and from the two I can gather from the documentation will give me a List on the entity, I I still want to be able to access the complete entities in these collections.

Are my requirements for NHibernate too high?

Use ISession.Load:

< pre>customerEntity.Categories.Add(session.Load(2));

Load will return the agent and will not access the database. You can access the ID attribute of the agent without accessing the database , But if you access any other properties, NHibernate will load the proxy.

I am looking for shortcuts. I have some NH entities with many-to-many relationships. Something like this :

public class Customer: EntityBase
{
public virtual IList Categories {get; set; }
}

public class Category: EntityBase
{
public virtual IList Customers {get; set; }
}

Please remember that this is a very simple description, so resist the temptation and suggest that I don’t use a many-to-many arrangement, or I should use value types or something similar.

In terms of databases , This relationship is done through a separate table with two columns-Customer_Id and Category_Id.

I really want to be able to add existing categories to customers without having to retrieve the complete one from the database Entity. Maybe it’s like this:

customerEntity.Categories.Add(new Category {Id = 2 });

The reason is that the application is a ASP.NET MVC application, I am using ViewModels as my views. These Customer ViewModels end with List. For category selection, when I map the ViewModel to the corresponding Customer entity, I want to be able to inhale these category IDs List of categories without having to hit the database first to retrieve them.

Part of the reason I want to be able to do this is that I want to minimize database calls, but I also want my mapper class to be able to create Customer entities without calling my service layer to find other items… This seems to be bad The design. I also want to avoid adding another layer to call the mapper, and then perform other mapping content that gets the entity from the repository (it itself is accessed through the domain service layer).

I checked the idbag, But for one I am using Fluent NHibernate and it does not support the construct, and the two that I can gather from the documentation will give me a List on the entity, I still want to be able to access the complete entity in these collections .

Are my requirements for NHibernate too high?

Use ISession.Load:

customerEntity.Categories.Add(session.Load< Category>(2));

Load will return the proxy and will not access the database. You can access the ID property of the proxy without accessing the database, but if you access any other properties, NHibernate will load Proxy.

Leave a Comment

Your email address will not be published.