How to add a new method

Posts   
 
    
Austinn
User
Posts: 38
Joined: 25-Aug-2008
# Posted on: 09-Jun-2010 22:05:38   

Hello, I have generated the code successfully of an entity Customer. I am able to search the databause using the primarykey field(CustomerId) of entity customer. But what if I want to implement another constructor in the entity class where instead of passing he primary key field I want to pass for Non PrimaryKey field for example(FirstName) of Customer entity?

Appreciate your help

Regards

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Jun-2010 06:25:58   

Are you using Adapter or SelfServicing? Anyway, adding custom code it's easy. I recommend to read Adding your own code to the generated classe. You can add code to the same file using USER_CODE_REGIONS and that code is kept when you re-generate from LLBLGen Designer. You also can use partial classes (preferred). Be aware that your new constructor makes sense. Please let us know if you need further help.

David Elizondo | LLBLGen Support Team
Austinn
User
Posts: 38
Joined: 25-Aug-2008
# Posted on: 08-Aug-2010 15:38:05   

Hello I have added a new constructor in the ClientGroupEntity and trying to pass a ClientGroupName as string. I am doing the same thing what i saw in the generated code as mentioned below


public ClientGroupEntity(System.Decimal clientGroupId):base("ClientGroupEntity")
        {
            InitClassEmpty(null, null);
            this.ClientGroupId = clientGroupId;
        }

This what I have implemented as a new constructor to fetch the entity by a customer name. But Unable to get the entity, It is returning null. I have added my code in the custom entity code region. Please help..

public ClientGroupEntity(System.String clientGroupName)
            : base("ClientGroupEntity")
        {
            InitClassEmpty(null, null);
            this.ClientGroupName = clientGroupName;
        }

In the client code I am writing the below code.


          ClientGroupEntity clientGroup = new ClientGroupEntity("ABC");
            DataAccessAdapter adapter = new DataAccessAdapter();
            adapter.FetchEntity(clientGroup);
            string s = clientGroup.ClientGroupCode;


Now in the client code i am expecting s should be filled bby ClientGroupCode. But it is returning as null....

Please help, what I am doing wrong.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Aug-2010 19:31:23   

Mmm. It doesn't work like that, because you are Using Adapter. So you didn't implement the logic to find the correct entity. So you don't need a method in your Entity. Instead outside write this:

EntityCollection<ClientGroupEntity> clientGroups = new EntityCollection<ClientGroupEntity>();
IRelationPredicateBucket filter= new RelationPredicateBucket(ClientGroupFields.ClientGroupName == "AAA");

DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(clientGroups, filter);

ClientGroupEntity theClientGroup = null;
if (clientGroups.Count > 0)
{
     theClientGroup = clientGroups[0];
}

You can put that in a special method in your business logic.

David Elizondo | LLBLGen Support Team
Austinn
User
Posts: 38
Joined: 25-Aug-2008
# Posted on: 08-Aug-2010 21:46:49   

Thanks buddy, by following this approach I have done it but what if I need to put this type of method as a constructor in the entity class. So it means I will do this type of code inside the entity class. By the way thanks for the tip it is working now...

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-Aug-2010 07:41:50   

In the Adapter model, the entities are not aware of the persistence info, and they should not be aware of it. So you should not put any fetching logic inside the entity class.

The entity default CTor that acepts a PK parameter, just sets the PK value, and the DataAccessAdapter is the class that is responsible of fetching he entity based on the PK value.

It seems to me that maybe the FirstName in this case is unique, that's why you want to use it to fetch a customer, if this is the case then you may define a Unique Constraint on the database field, which let's you do something like the following (provided that you have refreshed the schema and regenerated the code using the Designer):

DataAccessAdapter adapter = new DataAccessAdapter();
CustomerEntity customer = new CustomerEntity();
customer.FirstName = "Austin";
adapter.FetchEntityUsingUniqueConstraint(customer, customer.ConstructFilterForUCFirstName());
Austinn
User
Posts: 38
Joined: 25-Aug-2008
# Posted on: 09-Aug-2010 11:16:10   

Walaa wrote:

In the Adapter model, the entities are not aware of the persistence info, and they should not be aware of it. So you should not put any fetching logic inside the entity class.

The entity default CTor that acepts a PK parameter, just sets the PK value, and the DataAccessAdapter is the class that is responsible of fetching he entity based on the PK value.

It seems to me that maybe the FirstName in this case is unique, that's why you want to use it to fetch a customer, if this is the case then you may define a Unique Constraint on the database field, which let's you do something like the following (provided that you have refreshed the schema and regenerated the code using the Designer):

DataAccessAdapter adapter = new DataAccessAdapter();
CustomerEntity customer = new CustomerEntity();
customer.FirstName = "Austin";
adapter.FetchEntityUsingUniqueConstraint(customer, customer.ConstructFilterForUCFirstName());

Thankx alot friend. your detail description really helped me alot. appreciate.

regards Austinn