EntityBag – Wrap-up and Future Directions

Over the course of quite a few posts during the last several weeks I’ve shared source code that adds up to an implementation for EntityBag<T>.Piecing together a project from all those snippets, though, would be a pretty painful task, so I put the whole thing together in a zip file and posted it up at the new MSDN Code Gallery site: http://code.msdn.microsoft.com/entitybag/

Since I finished the series, though, I’ve done a bit more testing on the project and ran into a small bug.ContextSnapshot includes a copy of the connection string from the context which is the initial source of the EntityBag, and it uses that connection string to create its own internal context for tracking changes to the graph.In my initial testing, I always constructed the context by passing a connection string to the constructor, and everything worked fine, but an even more common pattern is to use the parameterless constructor which causes the context to have a connection string of the form: name=”containerName” and the full connection string is actually in the application’s config file.The problem with this is that on the client the config file would not be present.So, we add the following code to the ContextSnapshot.ConnectionString setter after the line which constructs the connection string builder:

The project file on code gallery includes this fix.I’ve also had some questions from folks who were having trouble getting a WCF service working using EntityBag, so I included a sample service as well.

Creating an EntityBag-based Service

I’m no WCF expert, but I’ve been able to muddle my way through.It seems like it might be useful to point out some of the trickier steps involved in putting together the sample:

Known types for the service methods are an issue—especially since EntityBag is generic and can contain a graph of many entity types.This is something which will be addressed automatically in the next release, but for now this is worked around with a static method (GetKnownEntityTypes) and an operation contract attribute which points to it as described in this previous post.

If you create your entities in a separate DLL like I’ve done in the sample, then you need to copy the connection string from the model.dll’s app.config file to the web.config for the service.

If you are going to use regular metadata files, you need to make sure they are in the right directory--otherwise, you can embed them as resources, but then you need to deal with a beta 3 bug by building with the metadata not embedded first and then rebuilding set to embed.

One common and frustrating problem is that it’s easy for the size of the message to grow beyond the default maximum message size.This can be addressed by changing the maxReceivedMessageSize in the app.config for the client and in the web.config for the service.

Finally, it’s important to make sure the service is set to re-use referenced types.This is turned on by default with VS 2008, but it’s worthwhile to make sure that it is on, and of course you need to reference system.data.entity.dll, perseus and your model on the client before adding the service reference.

Future Directions

With all of this we have a decent proof of concept, but clearly there’s a lot of room for additional improvements.Here are a few ideas both to give you an idea of current limitations and future possibilities:

·It needs to be tested against a wider array of models.

oEntities with complex types will likely present problems because there are currently limitations in the CurrentValues and OriginalValues records on ObjectStateEntry when dealing with properties that are complex types.

·There are various limitations due to bugs or not yet implemented features in the Entity Framework.

oEntities used with EntityBag must implement IEntityWithKey.Classes generated by the system implement this interface, but IPOCO classes are not required to implement it.Unfortunately, an API change in beta 3 makes it impossible to do certain operations in a general-purpose component unless the entities in question implement the interface.A future release will remedy the problem, and it will be possible to remove the requirement.

oCurrently the system uses reflection to set the EntityKey property on IRelatedEnd when the end in question is an EntityReference.Future releases will remove that requirement.For the most part this isn’t an issue, but it could create partial trust issues—I haven’t yet had a chance to investigate fully.

·The message size could be further optimized.

oRight now the system can’t distinguish between the serialization used when transmitting from the service to the client and that used when transmitting from the client back to the service.This causes unnecessarily large messages because when transmitting to the client you clearly want to include everything, but when transmitting back to the service in most cases you could skip all unchanged entities.

oThe component could also take advantage of work underway for the Entity Data Source Control to store and transmit only the minimum set of original values which the system needs rather than a whole copy of the original entity.Of course in some cases you will need the whole entity anyway because business logic may prevent constructing an entity with only this minimum set of properties, but in many cases this would be a useful optimization.

·Additional steps could be taken to strengthen a web service’s contract.

oEvents or some other mechanism could be added to allow validation when an EntityBag is passed from the client back to the service for updates.

oEven better, code could be generated to create a stronger contract and just use EntityBag as an internal storage/serialization mechanism.

I’m sure there are other things we could do as well, but that’s probably enough to set you thinking.As always, if you have questions or ideas, please don’t hesitate to share them.

To be completely honest, I just haven’t had time to work on EntityBag for quite a while. I’m trying to get cycles (or find someone else on the team who has cycles) to update it for sp1 beta, but so far that just hasn’t happened.

I still point people here as a resource for thinking through the problems and learning more of the details about the framework, but this was never intended to be a production component like what we ship with the EF directly. Sadly, the fact that we don’t have something more like this is a definite lack in v1 of the EF which we’re working to address with a real, supported component in v2.