Thursday, October 8, 2009

One way associations

If you are tired of writing both parts of an association or if you want to add an association to an object that you do not own check eXpand’s ProvidedAssociation attribute.

Creating OneTo Many

public class Employee:BaseObject
{
    public Employee(Session session) : base(session)
    {
    }
    private EmplyoeeContract _EmplyoeeContract;
    [ProvidedAssociation]
    [Association("EmplyoeeContract-Employees")]
    public EmplyoeeContract EmplyoeeContract
    {
        get
        {
            return _EmplyoeeContract;
        }
        set
        {
            SetPropertyValue("EmplyoeeContract", ref _EmplyoeeContract, value);
        }
    }
}

in the above code i have decorated EmplyoeeContract with ProvidedAssociation and that is the only needed to create an collection of Employess at EmplyoeeContract class for use in both runtime and designtime.

public class Employee:BaseObject
{
    public Employee(Session session) : base(session)
    {
    }
    [Association("Employee-EmplyoeeContracts")]
    [ProvidedAssociation]
    public XPCollection<EmplyoeeContract> EmplyoeeContracts
    {
        get
        {
            return GetCollection<EmplyoeeContract>("EmplyoeeContracts");
        }
    }
}

in the above code I decorate a colection thus saying to eXpand to create a reference object of type Employee at EmplyoeeContract type

Many To Many

public class Employee:BaseObject
{
    public Employee(Session session) : base(session)
    {
    }
    [Association("Employee-EmplyoeeContracts")]
    [ProvidedAssociation(RelationType.ManyToMany)]
    public XPCollection<EmplyoeeContract> EmplyoeeContracts
    {
        get
        {
            return GetCollection<EmplyoeeContract>("EmplyoeeContracts");
        }
    }
}

to make that happen just used RelationType.ManyToMany with ProvidedAssociation

What about adding more attributes to the other type than the Association attribute?

for eg how about making in the first example to make the collection of the other part aggregated?

public class Employee:BaseObject
{
    public Employee(Session session) : base(session)
    {
    }
    
    public static IEnumerable<Attribute> MoreAttributes
    {
        get { yield return new AggregatedAttribute(); }
    }
    
    private EmplyoeeContract _EmplyoeeContract;
    [ProvidedAssociation("Employees",RelationType.Undefined, "MoreAttributes")]
    [Association("EmplyoeeContract-Employees")]
    public EmplyoeeContract EmplyoeeContract
    {
        get
        {
            return _EmplyoeeContract;
        }
        set
        {
            SetPropertyValue("EmplyoeeContract", ref _EmplyoeeContract, value);
        }
    }
}
ps: In order ProvidedAssociation to do its magic you have to use eXpand.ExpressApp.dll
image

see also : How to get eXpand latest version

Technorati Tags: ,

Subscribe to XAF feed
Subscribe to community feed

DiggIt!
blog comments powered by Disqus