开发者

1 to N Relationship Mappings with Castle ActiveRecord

This is my first post on Stack Overflow! (yay!)

I'm having trouble with a Castle ActiveRecord mapping, which I thought would be a pretty common scenario, but maybe I didn't "google" right.

I have one entity called Product, and another entity called Warranty. Product has two properties on it (among others), each of type Warranty. So the model looks something like this (attributes and other properties are omitted):

public class Product
{
  ...
  public Warranty StandardWarranty { get; set; }
  public Warranty ExtendedWarranty { get; set; }
}

public class Warranty
{
  ...
  public Product Product { get; set; }
}

At first I thought that I would need to make warranty a base class and then create two concrete classes, StandardWarranty and ExtendedWarranty, but the classes do not diverge in any kind of data or behavior, so using a single table inheritence with a discriminator column seemed overkill and over compl开发者_如何学Cex.

The closest thing I could come up with in the ActiveRecord documentation would be to use the "Any" attribute. But the documentation seemed to hint that it should be used in scenarios where there is polymorphic behavior involved with separate concrete classes. But I wanted to check with the community before I started winding down that rabbit hole.

Perhaps I should just use a one to many relationship instead? One product has many warranties? But the product class will have at most 2 and at least 0, so this still didn't seem correct.

Any input would be helpful!

Thanks, Matt


I haven't done too much mapping with Castle AR, but I've done my share with Rails. My guess is you should be able to do something like the following:

public class Product 
{ 
     ... 
     [OneToOne]
     public Warranty StandardWarranty { get; set; } 

     [OneToOne]
     public Warranty ExtendedWarranty { get; set; } 
} 

public class Warranty 
{ 
    ... 
    [PrimaryKey(PrimaryKeyType.Foreign)]
    public int ProductId { get; set; }

    [OneToOne]
    public Product Product { get; set; } 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜