Something like this might work for you as well... new to 2.1 http://fabiomaulo.blogspot.com/2008/10/entity-name-in-action-strongly-typed.html
On Fri, Jul 24, 2009 at 7:36 PM, Anne Epstein<[email protected]> wrote: > Please excuse any code typos, I'm writing this from memory on a small > keyboard, and I've been flipping between VB.Net and C# enough lately > that I may be useless at this point for either :) > > Maybe one of the table-per-concrete class inheritance schemes might > work for you? > http://www.nhforge.org/doc/nh/en/index.html#inheritance-tableperconcrete > > As you said though, the order items would be the trickiest part. > Here's one idea to consider: map nHibernate to *the backing field* for > your orderitem/incompleteorderitem collections. then, you expose a > property like this on each of then: > > private List<IncompleteOrderItem> incompleteOrderItems = new > List<IncompleteOrderItem>(); > public List<AbstractOrderItem> AbstractOrderItems { > get{return incompleteOrderItems.Cast<abstractOrderItem>().ToList();} > } > > BUT... that leads to another issue-adding and removing items. You may > have seen in NH that it's common to do list adds via a specific method > on the parent object to manage bidrectional relationships. If you put > methods like that on AbstractOrder, it would wrongly seem ok, at least > from outside AbstractOrder to add an upcast > IncompleteOrderItem(appears to be an abstractincompletorder) to a > CompleteOrderItem I think you'd get a runtime error, and having to > downcast is something you want to avoid-that's get you into > trouble.... > > This is going to be weird, but one answer may be to keep orderitem > add/removal *entirely* internal to order. when you need to add/remove > an order item, create an orderitem dto, pass that in, and new up an > object based on that. > > so there might be this somewhere... > public class Orderitemdto{ > public guid Id {get;set} > public int Quantity {get;set;} > public Product Product {get;set} > } > > then, > this might be on your abstract interface: > AddNewOrderItem (Orderitemdto dto){}; > > and this might be on your incompleteorder class > ... > AddNewOrderItem(Orderitemdto dto) { > orderitems.Add(new orderitem(dto)) > } > ... > RemoveOrderItem would be similar, matching on id, perhaps... > > And you'd need an extra contructor on that order item that could build > a new order item based on the dto. > > I have no idea if any of that makes sense. Hope it helps, or at least > gives you some new ideas... > > > > On Fri, Jul 24, 2009 at 5:54 PM, Jeffrey<[email protected]> wrote: >> >> Ok, let me explain a bit further. I am working against existing >> database so the data model can not be changed and I consider the >> database has been real poorly designed! >> >> 1 customer can have more than 1 order >> 1 order can have more than 1 order item >> >> order will first be issue an unique reference and saved into >> InCompleteOrders table (Items will be saved into InCompleteOrderItems >> table, there's no status in the InCompleteOrders table as it is only >> for incomplete ones), most of the columns in InCompleteOrders table >> are nullable to allow for incomplete data, and this is the whole >> purpose and this is where all the problems come from!!! >> >> once an order has been submitted it will be copied over to >> CompleteOrders/CompleteOrderItems table and marked as delete in >> InCompleteOrders table >> >> current system has an if !exist in InCompleteOrders else get from >> CompleteOrders method, adn order and order item have been split into 4 >> distinct classes (incompOrder, incompOrderItem, compOrder and >> compOrderItem class), and it's painful to have 2 objects in code that >> do exactly the same things. from a business point of view, >> InCompleteOrders and CompleteOrders are exactly the same thing, so all >> the logic and code are duplicated twice, and the report sql state is >> a huge union and join!!! it's only that the original programmer didn't >> know where to put the incomplete data therefore a table for >> InCompleteOrders was created. >> >> I've taken over the maintenance job and i am allowed for a few weeks >> to modify the code. so i chose nhibernate as orm (the mapping freedom) >> and i am trying to generalize InCompleteOrder/InCompleteOrderItem and >> CompleteOrder/CompleteOrderItem into only Order and OrderItem class. i >> was able to map InCompleteOrder and CompleteOrder class into something >> like: >> >> InCompOrder ---> abstract order >> CompOrder --->abstract order >> >> then i was able to do var abstractOrder = >> repository<abstractOrder>.getbyref, and this will issue 2 select >> statement. >> >> however when it comes map 1 incomp order can have more than 1 incomp >> order item, i totally have no idea what to do. >> >> i want to be able to do abstractOrder.abstractItems, and this will >> also issue 2 select statement (1 for incomplete item the other for >> complete orders). >> >> as i am quite new to nhibernate maybe more seasoned user can suggest >> some other way to deal with this sh*t data model. >> >> thanks! >> >> On Jul 25, 3:25 am, Nexus <[email protected]> wrote: >>> Jeffrey, >>> >>> I am puzzled how the model should be able to work, In completeOrders >>> table we have a status field that could be Status.Incomplete, when is >>> a record added to Incomplete orders table then ? >>> >>> Please advise on existing functionality. >>> >>> Kind regards, >>> >>> On 24 jul, 15:48, Jeffrey <[email protected]> wrote: >>> >>> >>> >>> > Hi, >>> >>> > Thank you for your reply. However I do not think discriminator is the >>> > way to go. As you can see from my code sample that I don't have a >>> > discriminator column in any of the tables. My issue is that I need to >>> > be able to map CompleteOrders table and InCompleteOrders table to a >>> > single object (preferably an Order class) and I could do this using >>> > 2nd or 4th example from this >>> > linke:http://ayende.com/Blog/archive/2009/04/10/nhibernate-mapping-ndash-in.... >>> > However in the Order class I also need to map a collection of >>> > OrderItems and I have no idea how to do this as OrdersItems are >>> > separated into 2 separate tables (InCompleteOrderItems table and >>> > CompleteOrderItems table). >>> >>> > Kind regards, >>> >>> > On Jul 24, 10:16 pm, Nexus <[email protected]> wrote: >>> >>> > > Hi, >>> >>> > > You should be using a discriminator in your mapping >>> > > seehttp://www.nhforge.org/doc/nh/en/index.html#inheritance >>> >>> > > Kind regards >>> >>> > > On 24 jul, 09:22, Jeffrey <[email protected]> wrote: >>> >>> > > > I am working against an existing database. The databse itself is >>> > > > poorly designed. I am trying to use NHibernate to map my domain models >>> > > > in C# to the database. >>> >>> > > > The database structure is as below: >>> >>> > > > Customers table >>> > > > CustomerId INT PK >>> > > > Property1 varchar(255) NOT NULL >>> >>> > > > InCompleOrders table >>> > > > InCompleOrderId INT PK >>> > > > CustomerId INT FK >>> > > > Property1 varchar(255) NULL >>> >>> > > > CompleteOrders table >>> > > > CompleteOrderId INT PK >>> > > > CustomerId INT FK >>> > > > Status INT NOT NULL >>> > > > DeliveredOn datetime NOT NULL >>> > > > PaidOn datetime NOT NULL >>> > > > Property1 varchar(255) NOT NULL >>> >>> > > > And then I have tables like: >>> >>> > > > InCompleOrderItems table >>> > > > InCompleOrderItemId INT PK >>> > > > InCompleOrderId INT FK >>> > > > Property1 varchar(255) NOT NULL >>> >>> > > > CompleteOrderItems table >>> > > > CompleteOrderItemId INT PK >>> > > > CompleteOrderId INT FK >>> > > > Property1 varchar(255) NOT NULL >>> >>> > > > My C# domain models are as below: >>> >>> > > > public enum Status >>> > > > { >>> > > > InComplete = 1, >>> > > > Pending = 2, >>> > > > Complete = 3} >>> >>> > > > public abstract class BaseOrder : Entity >>> > > > { >>> > > > public string Property1 {get;set;} >>> > > > public Status Status {get;set;} >>> > > > public string Reference {get;set;} //this is unique per each order >>> > > > public ISet<BaseItem> Items {get;set}} >>> >>> > > > public class InCompleteOrder : BaseOrder >>> > > > { >>> > > > //select from InCompleOrders table >>> > > > public override Status Status >>> > > > { >>> > > > get { return Status.InComplete; } >>> > > > }} >>> >>> > > > public class CompleteOrder : BaseOrder >>> > > > { >>> > > > //select from CompleteOrders table >>> > > > public DateTime DeliveredOn {get;set;} >>> > > > public DateTime PaidOn {get;set;}} >>> >>> > > > public abstract class BaseItem : Entity >>> > > > { >>> > > > public BaseOrder Order {get;set;}} >>> >>> > > > public class InCompleteItem : BaseItem >>> > > > { >>> > > > //select from InCompleOrderItems table} >>> >>> > > > public class CompleteItem : BaseItem >>> > > > { >>> > > > //select from CompleteOrderItems table >>> >>> > > > } >>> >>> > > > I want to unify Order and Item, however due to the existing databse >>> > > > structure I am really struggling mapping it. >>> >>> > > > I want to be able to do things like: >>> > > > public Order GetByReference (string reference) >>> > > > { >>> > > > var repoOrder = new Repository<Order>(); >>> > > > var bo = repoOrder.FindOne(query); >>> > > > //query = Restrictions.Eq("Reference", reference) //reference = >>> > > > "abc" >>> > > > //and this will generate a SQL similar to: >>> > > > // >>> > > > //SELECT CompleteOrderId >>> > > > // , Status >>> > > > //FROM CompleteOrders >>> > > > //WHERE Reference = 'abc' >>> > > > // >>> > > > //UNION >>> > > > // >>> > > > //SELECT InCompleOrderId >>> > > > // , 1 AS 'Status' >>> > > > //FROM InCompleOrders >>> > > > //WHERE Reference = 'abc' >>> >>> > > > return bo; >>> >>> > > > } >>> >>> > > > Also be able to do the same for: >>> >>> > > > var baseOrder = repository.get base order; >>> > > > if baseOrder type is InCompleteOrder >>> > > > then will select from InCompleteOrderItems table >>> >>> > > > and vice versa. >>> >>> > > > I am using NHibernate 2.1 and c# 3.0. Any help would be much >>> > > > appretiated.- Tekst uit oorspronkelijk bericht niet weergeven - >>> >>> > - Tekst uit oorspronkelijk bericht weergeven - >> >> >> > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
