What's the best way to handle "type" tables with LINQ to SQL? - vb.net

I have some tables that represent various types. They usually just consist of an ID (int), and a name. Ideally I would have this in an enum. Is there a way to map a table like this onto an enum?
EDIT: How would I handle it if there were extra fields other than an ID and Name?

If it's just an id and a name, I generally do this:
public enum FootScent : int
{
Unknown = 0,
Mild = 1,
Sweaty =2,
SteppedInSomething = 3
}
and then on the LINQ entity property:
[Column("foot_scent_id", DbType = "Int NOT NULL")]
public FootScent Scent { get; set; }
For lookup tables with columns other than "id" and "name" that are needed, I usually just make a normal LINQ entity for them, though depending on your implementation it would probably be worth caching these to minimize trips to the DB.

Related

How to set fields non-nullable for JOOQ POJO

I have this DDL
CREATE TABLE user
(
id bigint NOT NULL PRIMARY KEY,
name string NOT NULL
);
org.jooq.codegen.KotlinGenerator generates such a class:
data class Users(
var id: Long? = null,
var name: String? = null,
): Serializable
I expect non-nullable fields to be non-nullable, like:
data class Users(
var id: Long,
var name: String,
): Serializable
I only use these settings:
generate.apply {
isRecords = true
isDaos = true
isPojosAsKotlinDataClasses = true
isSpringAnnotations = true
isPojos = true
}
Jooq ver. 3.15.10
How do I configure the generator so that the fields are non-nullable?
As of jOOQ 3.17, there are pending feature requests to add some additional convenience at the price of correctness to generated classes:
#10212 Add option to handle nullability in KotlinGenerator
#12934 Null-safe views of columns known to be non-null
The main reason why this hasn't been implemented yet is the many ways such non-nullability promises can break in the event of using operators like LEFT JOIN, UNION and many others, in case of which an expression that appears to be non-null will produce null values nonetheless.
While #10212 is very hard to get right, #12934 might be a compromise to be implemented soon, given that it affects only generated data class types, which can be used on a 1:1 basis by users who understand the tradeoffs.

enum as string and number in the database

i have a table
table Foo
(
id,
FooType,
FooTypeName,
)
FooType is an enum with fixed values hence i want to map it to
public virtual FooType FooType { get; set; }
How can i serialize the property in both columns but use only one to read and query?
Which mapping (xml, Mapping by Code, FluentMapping) doesn't matter
You could create FooTypeName as a string property projecting the value of FooType, and map it as readonly.
Alternatively, if you don't need FooTypeName at all, just ignore it in your model and keep it updated with a trigger.
Or, if you feel like coding a lot, create a user type that maps FooType to the two columns.

Conditionally map two columns to one field with Fluent NHibernate

I have an NHibernate entity called Owner that has a SSN column and a TaxID column in the database, and I would like to conditionally map one of those two values to a more generic property on the Owner entity based on the value of a different property, StructureType. So, if the StructureType is "I", I want to map the SSN value to the generic property, and if its "C" I want to map the TaxID value to the generic property. Is this possible using Fluent NHibernate (or even regular NHibernate)? The Owner entity is a read-only entity, nothing will be written back to the database.
I was able to solve this using a Formula in Fluent NHibernate:
Map(x => x.Identification)
.Formula("CASE WHEN StructureType = 'I' THEN SSN ELSE TaxID END");
(In my original post I said it was between 'I' and 'C' but is in fact just between 'I' and every other type)
Why don't you add a readonly property?
public string Identification
{
get
{
string identification = string.Empty;
if (StructureType.Equals("I"))
identification = SSN;
else if (StructureType.Equals("C"))
identification = TaxID;
return identification;
}
}

finding data column wise in nhibernate

I have a Employee class, i am retriving employee data using nhibernate.It gives
complete column.(eq. sql - Select * ). But, if i want to have slected column (like EmployeeName and EmployeeID only),then i need to create a class with these two properties(e.g.Empl class).and using AliasToBeanResultTransformer i.e. Projection i can retrive. I want to know that is there any way in nhibernate (without creating sub classes(Empl class), or without hard coding hql query)so that i can retrive specific column values...
class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int ResidingInCountryId { get; set; }
public virtual Country ResidenceCountry { get; set; }
}
If you only want to project a few columns from a query then you can either
a) use a DTO although you mentioned you don't like this for some reason
b) if you REALLY want a list of anonymous objects then simply omit the result transformer which will result in a list of anonymous objects with the projected values. This would be sufficient for databinding.
Also you do not need to use just HQL for projectons, you can use Criteria, QueryOver or NH Linq.

NHibernate Mapping from multiple tables

Consider the following table structure...
Appointment
-----------
ID integer
Description nvarchar
StatusID smallint
Status
------
ID smallint
DisplayText nvarchar
Now, for good or for evil, we want this situation to map to a class that looks like this
class Appointment
{
public int ID {g;s;}
public string Description { g;s; }
public string Status { g; private s; }
}
I.e. we want to pull the normalised status display text straight into the entity.
The obvious answer is to create a Status entity and make the appointment class have a reference to that and map it in the normal way.
Don't create an entity class. Use an enum and EnumStringType as shown here. This is exactly what you want I think.