Unique field and double key

Oct 8, 2008 at 9:33 AM
Hi.

I am testing Retina.
I have 2 questions. Here a simple class:

public class Article : Retina.Entity
{
        [FieldStorage(IsKey = true, IsIdentity = true, IdGeneratorType = typeof(IdGeneratorGuid))]
        public Guid ArticleGuid;

        [FieldStorage()]
        public string Name;

        [FieldStorage]
        public int Quantity = 0;

        public bool IsAvailable { get { return this.Quantity > 0; } }

        public Article() { }
}

How to specify that the Name field must be unique in the table?

And, is it possible to define 2 primary keys? How? What about the .Retreive() methods?

Thanks in advance.
Oct 8, 2008 at 10:50 AM
Edited Oct 8, 2008 at 11:48 AM
Hum...

> How to specify that the Name field must be unique in the table?

It seems I can use a ServerConstraint for this point, to have broker handler.

Something likes that:

public Article()
{
    this.AddConstraint(new Retina.ServerConstraint(this.IsNameUnique));
}

public static Expression ByName(string pName)
{
    return new Expression(
        typeof(Article), GetField(typeof(Article), "Name"), Criteria.Equal, pName);
}

private IConstraintResult IsNameUnique(ITxnContext pContext, object pAppContext)
{
     bool bAlreadyUsed =
         pContext.RetrieveMatchExists(pAppContext, Article.ByName(this.Name));

     if (bAlreadyUsed)
     {
         return new BrokenConstraint(string.Format("The [Name] {0} is already used", this.Name));
     }

     return new HeldConstraint(string.Format("The [Name] {0} is not used yet", this.Name));
}


Oct 8, 2008 at 1:30 PM
Edited Oct 8, 2008 at 3:48 PM
Note that Critera class does not contain neither EndWith nor NotEndWith clauses.
Could be usefull, no?

Add : Ok -> http://www.codeplex.com/retina/WorkItem/View.aspx?WorkItemId=6798
Coordinator
Oct 8, 2008 at 11:23 PM
Well, I have a couple of questions here....  let's go step by step.

1) Compound primary keys
Yes, Retina support compound (multiple field) PKs.
How to implement that? Easy, just define more than one field (or property) with a [FieldStorage] attribute using IsKey = true.
How to retrieve such an entity? The Retrieve methods receive an object representing the PK, and in the case of a compound PK we need to pass an object array with the values of the entity PKs we want to retrieve.
Here there is a sample: Customer customer = context.Retrieve<Customer>(new object[]{pk1, pk1});
Where pk1 and pk2 are two variables containing the values of the two PKs you are looking for.

2) Unique field value

Yes, a ServerConstraint is the supported way to implement this. We could do something to implement this kind of constraints in a more declarative way. I will consider this for a future release.

3) EndsWith and NotEndsWith are already implemented, but I haven't uploaded the latests Retina binaries & installer. Look for this new Criteria operators in the latest release (2.0.10910.0) I just published.

Thanks for your time and comments!
Andrés.

 

Oct 9, 2008 at 11:08 AM
> Here there is a sample: Customer customer = context.Retrieve<Customer>(new object[]{pk1, pk2});
> Where pk1 and pk2 are two variables containing the values of the two PKs you are looking for.

How is defined the order of the object[] parameters?
Thanks.
Coordinator
Oct 9, 2008 at 12:06 PM
The order of the params is the same natural order of the properties defined in the class source entity.
Andrés.