Here is a snippet of how we are doing the fetches:
Single Entity:
CompanyEntity company = new CompanyEntity();
PrefetchPath2 prefetch = new PrefetchPath2(EntityType.CompanyEntity);
prefetch.Add(CompanyEntity.PrefetchPathCompanyAddress).SubPath.Add(CompanyAddressEntity.PrefetchPathAddress);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntity(Company, prefetch );
EntityCollection:
EntityCollection<CompanyEntity> companies= new EntityCollection<CompanyEntity>();
IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(CompanyFields.CompanyId == companyID);
PrefetchPath2 prefetch = new PrefetchPath2(EntityType.CompanyEntity);
prefetch.Add(CompanyEntity.PrefetchPathCompanyAddress).SubPath.Add(CompanyAddressEntity.PrefetchPathAddress);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(companies, filter, PrefetchPath);
As for the entity being the same, yes and no.
The underlying address entry is the same but when it is changed we do not want it to be changed everywhere.
We cannot change the design at this point as we are too far along.
In your simple design you miss the association of the company with addresses.
We originally had the address type as a property of the address until we realized that the data the client was using would cause issues with the intention of the design.
The design is to allow for Addresses to be stored as single atomic units that will never change.
For example:
123 Main Street, Florida, USA
The above entry should only exist in the database 1 time.
Any time we need the address we associate an entity with it, such as company.
If the company needs to modify their address, we take the information they entered and determine if an entry already exists in the database, if it does we use that ID, if it does not we insert and use the new ID.
This system will eventually have many many entities that will need address information.
This design allows us to more easily use this information in code as well as keeping down on redundancy in the database.
Consider this, each company will have Employees, each Employee will have a list of Addresses assoicated with them.
The default address for a new employee will be the companies main address.
To do this we simply say:
Employee.EmployeeAddress.Address = Company.CompanyAddress.Address
;
But if we need to make a change to the Employees address we do not want it to affect the companies address.
I hope that what I have posted helps.
Thanks.