Question

I've asked a similar question, but I've given up on the idea I had there to solve this problem so I would like some help solving this in a neat way instead.

I've got tables

Image - (Id, Name, RelativeFilePath)
ImageFilter - (Id, Type)
ImageContext - (Id, Name, ...)
ImageContextImage - (Id, ImageContextId, ImageId, ImageFilterId)

Example of data:

ImageContextImage      Id        ImageContextId         ImageId        ImageFilterId
                       1         1                      1              1
                       2         1                      1              2
                       3         2                      1              1
                       4         3                      2              1

As you can see, an image in a context can have several filters applied.

All of my entities are very simple, except this mapping of the above. Currently I've got

ImageContext
    public virtual int Id
    public virtual string Name
    public virtual IList<ImageContextImage> Images

ImageContextImage
    public virtual int Id
    public virtual ImageContext Context
    public virtual Image Image
    public virtual ImageFilter ImageFilter

The above is very easy to map, but for each image I then get multiple ImageContextImage objects. I would rather have ImageContextImage contain a list of ImageFilter, so that I can simply iterate through that collection. I've tried alot of permutations of AsTernaryAssociation() and it complains that I need a Dictionary, but I want multiple values per key! Any ideas?

Any ideas? Thanks!

Was it helpful?

Solution

Ternary association can be replaced with binary associations but a new entity will appear after the replacement (Removing Ternary relationship types). It is ImageContextImage entity in your case and the hard part is to find best name for such entity. Your example is very close to this replacement.

ImageContextImage entity:

public virtual int Id { get; set; }
public virtual Image Image { get; set; }
public virtual ImageContext Context { get; set; }
public virtual IList<ImageFilter> Filters { get; set; }

and it's mapping:

Id(x => x.Id);
References(x => x.Image);
References(x => x.Context);
HasManyToMany(x => x.Filters); // filters are referenced via many-to-many relation

ImageContext entity:

public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<ImageContextImage> ImageContextImageList { get; set; }

and it's mapping:

Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.ImageContextImageList)
    .Inverse(); // to aggregate all ImageContextImage that are fererencing this instnstance of ImageContext
    .KeyColumn("Context_id");

Corresponding database schema is slightly different:

ImageContextImage(Id, Image_id, Context_id)

and new association table must be created for the many-to-many relation:

ImageFilterToImageContextImage(ImageContextImage_id, ImageFilter_id)

Note that this is only a sketch of one possible approach. Many details depends on your problem domain and must be tweaked before it is ready for production:) - e.g.cascades.

I have never used AsTernaryAssociation but it seems interesting. I will investigate it later, thank you for the inspiration:).

EDIT: Ternary association can be implemented by slightly different mapping (using composite-element) but there is still additional entity - ImageContextImage which is mapped as component in this case:

public class ImageContext
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<ImageContextImage> ImageContextImageList { get; set; }

    public ImageContext()
    {
        ImageContextImageList = new List<ImageContextImage>();
    }
}

public class ImageContextMap : ClassMap<ImageContext>
{
    public ImageContextMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        HasMany(x => x.ImageContextImageList).Component(c =>
        {
            c.References(x => x.Image);
            c.References(x => x.Filter);
        }).Cascade.AllDeleteOrphan();
    }
}

public class ImageContextImage
{
    public virtual Image Image { get; set; }
    public virtual ImageFilter Filter { get; set; }
}

ImageContext class can be extended by these methods to make things simpler:

public virtual IEnumerable<Image> AssociatedImages
{
    get
    {
        return ImageContextImageList.Select(x => x.Image).Distinct().ToList();
    }
}

public virtual IEnumerable<ImageFilter> GetFilters(Image image)
{
    return ImageContextImageList.Where(x => x.Image == image).Select(x => x.Filter).ToList();
}

No success with AsTernaryAssociation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top