Association in Devexpress XPO models

Asked

Viewed 93 times

0

I have a model for Municipio. This model can be referenced by several other models (Customer, Supplier, Carrier, Seller...). Each of these models has an association N:1 for Municipio, +/- so:

public class Cliente: XPLiteObject {
  ...
  Municipio: fMunicipio;
  [Association]
  public Municipio Municipio {
    get { return fMunicipio; }
    set { SetPropertyValue("Municipio", ref fMunicipio, value); }
}

According to the XPO documentation, I must declare this association at the other end, in the Municipality model, +/- so:

public class Municipio: XPLiteObject {
  ...
  [Association]
  public XPCollection<Cliente> Clientes { 
    get { return GetCollection<Order>("Clientes"); }
  }     
}

I do not want (I would not like) to declare this association in the Municipio model, because I will not reference the list of customers from Municipio. And also because I would have several models referencing Municipio, and I don’t need to get the list of any of them from Municipio. Another aggravating factor is that the Municipio model is already tested, and when I add another model referencing Municipio, e.g. Distributor, I do not want to change the Municipio model, adding another association in it.

My question is whether the association at the other end (Municipio) is mandatory for each associated model (Customer, Seller...), or whether I can keep the reference only in the model that maintains the relationship. And, if it is mandatory, whether I should name each of the associations to disambiguate the references, thus:

public class Municipio: XPLiteObject {
  ...
  [Association("Cliente-Municipio)]
  public XPCollection<Cliente> Clientes { 
    get { return GetCollection<Order>("Clientes"); }
  }     
  [Association("Fornecedor-Municipio)]
  public XPCollection<Fornecedor> Fornecedores { 
    get { return GetCollection<Order>("Formecedores"); }
  }     
}

public class Cliente: XPLiteObject {
  ...
  Municipio: fMunicipio;
  [Association("Cliente-Municipio)]
  public Municipio Municipio {
    get { return fMunicipio; }
    set { SetPropertyValue("Municipio", ref fMunicipio, value); }
}

public class Fornecedor: XPLiteObject {
  ...
  Municipio: fMunicipio;
  [Association("Fornecedor-Municipio)]
  public Municipio Municipio {
    get { return fMunicipio; }
    set { SetPropertyValue("Municipio", ref fMunicipio, value); }
}

1 answer

0


It is mandatory to have the association on both sides of the model, the Xpo inspects both tips to know the type of association that has been made.

The name of the association is usually optional, associations of type N:1 it can solve alone by inspecting the names of the classes, in this case it should only have problems in case you have more than one association between the same models.

The name will be mandatory in the case of associations of type N:N, and in these cases I usually also define the property UseAssociationNameAsIntermediateTableName = true in the association’s statement to have greater control of the creation of tables.

  • If it’s MANDATORY, let’s do it, right? I find it hard, but I can live with it :)

  • You can always create code templates to facilitate this, it will depend on the IDE and plugins used, in my case I use the CodeRush and it already comes with some templates that I customized to get more of my taste, if use the CodeRush test the templates xpa and xpcl

Browser other questions tagged

You are not signed in. Login or sign up in order to post.