read unique constraints in LPT

Posts   
 
    
Posts: 56
Joined: 08-Jun-2010
# Posted on: 18-Jan-2011 13:50:57   

Hello

I've written a set of templates which generate a set of service wrappers around llblgen. I'm using v3 / adapter / and I'm writing .lpt templates.

I would like to generate methods which fetch an entity based on a unique constraint. I would like to generate 1 method for each unqiue constraint on an entity.

now I know how to make the call to llblgen adapter.FetchEntityUsingUniqueConstraint() what I am struggling with is generating the method signatures.

I need to enumerate over each uniqueconstraint on an entity and for each one get the .net type and the mapped entity field name.

I've been digging about in the docs and not found anything :-(

Can you give me some help or a pointer to the docs

thanks ~Brett

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 18-Jan-2011 15:07:56   

Could you give some template code / context in which you want to use this / what you have tried yourself? It might help looking at the EF / NH / Linq to Sql templates perhaps for ideas how things can be solved / how to obtain meta-data.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 56
Joined: 08-Jun-2010
# Posted on: 18-Jan-2011 16:31:42   

Hi Franz

Thanks for getting back quickly. Here is what I am trying to do.


  public partial interface I<%=_executingGenerator.ProjectDefinition.ProjectName%>Service
    {
<%  
    foreach(EntityDefinition entity in _executingGenerator.Entities)
    {
        foreach(FieldElement field in entity.Fields)
        {
            if (field.IsUnique)
            {
                    string typeName = GeneratorUtils.ProduceDotNetTypeName(field.FieldType.RepresentedType, field.IsOptional, "{0}?");
%>
        <%= entity.Name%>Entity  Fetch<%= entity.Name%>By<%= field.Name %>(<%= typeName %> <%= field.Name %>);
<%
            }
        }
    }
%>
  }

the above won't work because there is no such thing as field.IsUnique but I hope it gives you an idea of what I am trying to achieve

thanks ~Brett

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 18-Jan-2011 21:45:30   

Rather than looking at the entity fields, perhaps you need to look instead at the database field definitions. I'll check the documentation and get back to you on how to do this...

Matt

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Jan-2011 07:50:28   

Try this (approximate code):

public partial interface I<%=_executingGenerator.ProjectDefinition.ProjectName%>Service
    {
<%  
    foreach(EntityDefinition entity in _executingGenerator.Entities)
    {
        foreach(FieldElement field in entity.Fields)
        {

            var uniqueConsAsStrings = (from ucs entity.UniqueConstraints
                   select ucs.FieldsAsString).ToList();

            bool isUnique = uniqueConsAsStrings.Contains(field.Name);

            if (isUnique)
            {
                   ...
David Elizondo | LLBLGen Support Team
Posts: 56
Joined: 08-Jun-2010
# Posted on: 19-Jan-2011 13:17:50   

That worked a treat, with one minor mod:

 ... from ucs in entity.UniqueConstraints....

Thanks for the help!

~Brett