Using NHibernate with search tables

If you have a set of tables in a database that strictly consist of a row description and an identifier, what is the best way to load them through NHibernate?

So, suppose I have a sandwich class, and the sandwich has meat, cheese and vegetables, where these three things are lookup tables in the database. This seems to be the most consistent with NHibernate's philosophy to

public class Meat { string name; int id; } public class Cheese { string name; int id; } public class Vegetable { string name; int id; } public class Sandwich { Meat meat; Cheese cheese; Vegetable vegetable; } 

But with dozens of tables like this in the database, classes seem to multiply quickly. Suppose I configured it like this:

 public class NameAndID { string name; int id; } public class Sandwich { NameAndID meat; NameAndID cheese; NameAndID vegetable; } 

Is it possible? How will this be implemented in Fluent NHibernate?

+4
source share
1 answer

You will need another column to determine the type. You can use an enumeration for this. Then all your requests should include this restriction ....

 CreateCriteria<NameAndID>.Add(Restrictions.Eq("ntype", E.Meat) 

However, I would prefer separate tables so that you have the best foreign keys. Otherwise, there is nothing in the database constraints that would prevent you from making a sandwich, which is just 3 pieces of cheese.

+1
source

Source: https://habr.com/ru/post/1303254/


All Articles