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.




--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to