NHibernate, DTOs to Entities vs keys and relations

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
kindbergs.dk
Posts: 13
Joined: Sun 27 Jan 2013 21:14

NHibernate, DTOs to Entities vs keys and relations

Post by kindbergs.dk » Thu 27 Mar 2014 16:18

Hi,
Using Entity Developer for NHibernate (running v 5.5.164) I am trying figure out how to do the following correctly:
An model of entities server side, plus DTOs generated for each entity going via WCF to client side.
Now server side entities I would prefer having full navigation properties, while client side I would prefer having foreign keys available in DTOs - or optimally fine tune which relations that the generated converters should map into nested objects/navagation properties, and which should just result in visible/editable foreign keys in the DTOs.
1) But I am fighting with limitations in both NHibernate and Entity Developer, which does not allow fine tuning as above!?
2) To get keys in DTOs it seams like model settings need the "Include foreign key columns" whereby they are also visible in the Entities, but that confuses NHibernate (two things referring to the same database field) - at least I have not yet been able to get it to save a entity.
3) The generated converters cannot handle DTO to Entity for navigation properties/relations. What should be the strategy for reestablishing these in a way that works consistently so that NHibernate's lock()/update()/save()/merge() works?
(As I started with writing, there is the problem of keys versus navigation properties in the two different objects, so while I don't mind writing custom glue code for checking and reestablishing integrity of relations, I do need the right data available for doing it.)

Note: Previously I have bypassed this problem by generating all entities without any navigation properties at all, and with all foreign keys visible as plain fields, so the model basically becomes DTOs in itself. That I have gotten to work well. But for my next project I wanted to use the power of generated DTOs client side plus full model server side.

Bests
Allan K.

kindbergs.dk
Posts: 13
Joined: Sun 27 Jan 2013 21:14

Re: NHibernate, DTOs to Entities vs keys and relations

Post by kindbergs.dk » Fri 28 Mar 2014 00:34

Okay, after formulating the question it was easier discovering some answers myself...
For #2 with both relations and foreign keys in the model, for each navigation property I need to set Insert and Update to False, and then NHibernate is able to persist an entity generated from a DTO without giving any "Error dehydrating property value" errors.
For #1 I found another setting on each navigation property called "Generate DTO" and by setting it to false, it seams like I can have all possible navigation directions available on the server, while the client only get selected sub objects via the ToDtosWithRelated converter. For making specific trees with only some links expanded deeper, I guess the DTOs+Entities could be looped to include the extra ToDto* calls.
For #3 I guess the above disables all handling of relations by NHibernate (except for reading), but with both keys and possibly objects available in both DTOs and Entities I guess they can be compared server side to trigger the appropriate handling.

Does that cover the 'correct' way of doing things, or is there a more optimal combination of settings?
(Like easier ways to persist updated DTOs that contains updated relations server side; or a way for an Entity created from a DTO to be intelligent enough to know to recreate its navigation property based on the duplicate foreign key passed; or in general link two properties that are based on the same database field so that NHibernate still works; or anything else I should know to avoid NHibernate pitfalls due to invalid combination of settings?...)

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: NHibernate, DTOs to Entities vs keys and relations

Post by Shalex » Tue 01 Apr 2014 10:32

Sorry but we don't have a clear understanding of the scenario you are implementing.

You can control the generation of navigation properties for DTO classes with the DTO Classes In Associations setting of the Data Transfer Object template (the detailed information about it is available in the Description section of the Properties window).
If this doesn't help, please send us a test model to show a current code generation, and specify the code generation you want to have.

kindbergs.dk
Posts: 13
Joined: Sun 27 Jan 2013 21:14

Re: NHibernate, DTOs to Entities vs keys and relations

Post by kindbergs.dk » Fri 04 Apr 2014 19:27

Hi.
Sorry, to summarize my goal: I am trying to find the fastest/easiest development way that solves the problem of getting a DTO and storing it as an entity while preserving links.
A example of 4 classes with link relations between them:
* LogOfOrders (a table containing records with the ID of Order - just as an example)
* Customer
* Order (contains the ID of Customer)
* OrderLine (contains the ID of Order)

I found that I can specify that a DTO for Order will be created with an embedded list of OrderLines DTOs. I can exclude the Customer from the DTO and update/inserts and just have its CustomerID in the Order+OrderDto classes, while Nhibernate is still aware of the relation between the Customer and Orders (meaning: to change the relation I need to change the ID instead of assigning a different Customer class, since the Order.Customer relation will be read only). Similarly I can exclude LogOfOrders from the DTO.
Now the problem is when I get an OrderDto back - I need to convert it to an Order without breaking the NHibernate link to Customer and LogOfOrders, and for the embedded list of OrderLineDto I need to delete/update/create OrderLines to match.
What is the best practice for solving this situation with the entities/dtos generated by Entity Developer for NHibernate - both in settings of Entity Developer and in code pattern?

My current workaround is now based on:
Load the Order object corresponding to the OrderDto (so the Order object will have the correct NHibernate link to Customer and LogOfOrders) or create a new if none found. Possibly need an extra check to ensure the passed ID of the Customer is still the same as it used to be.
Copy matching properties from OrderDto to Order.
Check if any of the OrderLines linked to by the Order is no longer present among the OrderDto.OrderLineDtos and if so delete them.
Foreach OrderDto.OrderLineDto copy its properties to the matching Order.OrderLine properties or create a new Order.OrderLine if not found.

I just feel like I am violating something, and worried that changing one setting on the Entity Developer model can cause updates to fail or even worse cause NHibernate to remove links, so I am wondering if there is a better design/pattern for doing all this?

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: NHibernate, DTOs to Entities vs keys and relations

Post by Shalex » Thu 10 Apr 2014 12:19

kindbergs.dk wrote:My current workaround is now based on:
Load the Order object corresponding to the OrderDto (so the Order object will have the correct NHibernate link to Customer and LogOfOrders) or create a new if none found. Possibly need an extra check to ensure the passed ID of the Customer is still the same as it used to be.
Copy matching properties from OrderDto to Order.
Check if any of the OrderLines linked to by the Order is no longer present among the OrderDto.OrderLineDtos and if so delete them.
Foreach OrderDto.OrderLineDto copy its properties to the matching Order.OrderLine properties or create a new Order.OrderLine if not found.
This looks like a correct approach.
Another way is to modify the template to generate the code, which preserves links, automatically.

Post Reply