For this post I am assuming you are familiar with Commerce Server 2009 and operation sequences, which in my opinion are the main reason to use the upgraded Commerce Server product. You can find some background on operation sequences on MSDN.
The problem we are trying to solve here is the addition of new properties to the Commerce Server Profile system, but we want a one-to-many relationship between the user profile and the new property. As an example, we will be adding a number of Tests to the user’s profile. In this case the user needs to participate in a number of tests that we can track, because these tests determine what products a user will be able to purchase. The more tests and test categories the user participates in, the more products they are eligible for.
So how does this look when wired up? That’s a complex question to be sure, but here is a simplified example, assuming we have a User object class with simple common properties:
public User GetUser(string userId)
{
User user = null;
OperationServiceAgent svc= new OperationServiceAgent();
CommerceQuery<CommerceEntity> query = new CommerceQuery<CommerceEntity>("UserProfile");
query.SearchCriteria.Model.Properties["Email"] = userId;
CommercePropertyCollection properties = new CommercePropertyCollection();
properties.Add("Email");
properties.Add("FirstName");
properties.Add("LastName");
query.Model.Properties = properties;
CommerceQueryRelatedItem<CommerceEntity> test= new CommerceQueryRelatedItem<CommerceEntity>("Tests", "Test");
test.Model.Properties.Add("TestId");
test.Model.Properties.Add("TestNumber");
test.Model.Properties.Add("TestCategory");
query.RelatedOperations.Add(test);
CommerceResponse response = svc.ProcessRequest(GetRequestContext(), query.ToRequest());
CommerceQueryOperationResponse queryResponse = response.OperationResponses[0] as ommerceQueryOperationResponse;
if (queryResponse !=null && queryResponse.CommerceEntities.Count > 0)
{
Convert(queryResponse.CommerceEntities[0]);
}
return user;
}
public static User Convert(CommerceEntity userEntity)
{
User result = new User();
result.Id = userEntity.Properties["Id"] as string;
result.Email = userEntity.Properties["Email"] as string;
result.LastName = userEntity.Properties["FirstName"] as string;
result.FirstName = userEntity.Properties["LastName"] as string;
CommerceRelationshipList relationships = userEntity.Properties["Tests"] as CommerceRelationshipList;
result.Tests = ConvertTest(relationships); //Function above in post
return output;
}