Anyone remember WCF RIA Services? It was a great idea: simplify n-tier development with a combination of libraries and tooling. Take something which is inherently difficult, namely data-centric web services consumed by rich clients, and make it easier. Provide a client library that performs change-tracking without dependence on Entity Framework, and a service library that can persist batch updates. But more importantly, provide an extension to Visual Studio that generates entities based on a data model and templates for scaffolding query and update operations on the server.
Ah, but the devil is in the details. The fatal flaw was coupling it to Silverlight. When Silverlight bit the dust, so did RIA Services. And it was also coupled to Windows Communication Foundation, which has declined in popularity since the arrival of ASP.NET Web API. Another problem was the implementation, which had a lot of magic glue that was not well documented (for example, project linking and code projection).
What troubles me, however, is not that RIA Services landed in the dustbin of history – which happens all the time in the world of technology – but that it wasn’t replaced by something new and better. We should be learning from the mistakes of the past to forge a new and better future.
So what to do about it? Gone are the days when we can sit back and complain. This is an era when open source has come into its own, when the infrastructure and tools are in place for developers all over the world to join together to make stuff happen. And that’s what Trackable Entities is all about. It’s a community effort to reduce the needless pain required to build n-tier applications with RESTful services using Entity Framework, which target any .NET client (including iOS and Android via Xamarin) and implement repository and unit of work patterns with dependency injection.
And so it is with great pleasure that I’m announcing that the Trackable Entities project has moved, from its former home on CodePlex, over to GitHub. Why GitHub? Suffice it to say that Microsoft has moved its important projects over to GitHub (such as ASP.NET, Core CLR, TypeScript and Roslyn). It’s where all the cool kids are doing open source, and it has amazing tools for distributed version control and collaboration. Oh, and there’s support for Git in Visual Studio 2013 and GitHub integration for Visual Studio 2015.
GitHub also makes it easy to build online documentation. So I’ve created a home page for Trackable Entities on GitHub which includes getting started tutorials and how-to videos (hosted on YouTube), as well as tips for debugging and ways you can contribute to the project. Here is the URL for the Trackable Entities documentation:
So what makes Trackable Entities a viable successor to RIA Services?
To sum it up: Trackable Entities gives you just the right amount of help, without too much hand-holding or behind the scenes magic. The client library is decoupled from the service, because all it does is set a state property: Unchanged, Added, Modified, Deleted. That’s it really, aside from a few other helper methods. The service library does one thing: read the state property and tell Entity Framework about it.
Secondly, there is entity code-generation based on customizable T4 templates. These templates are used by other tools, such as the Add Entity Data Model wizard or the EF Power Tools. But you can tweak them to your heart’s content. There are templates for separate client / server entities, as well as shared entities, which target either .NET 4.5 or greater, or Portable Class Libraries, for compatibility across desktop and mobile platforms.
Thirdly, the Visual Studio extension for Trackable Entities provides a set of multi-project templates for creating a Visual Studio solution that includes projects for services (WCF or Web API, Repository and Unit of Work), entities (client, service or shared) and a console client (there is also a sample app which includes a WPF client using the MVVM pattern).
This is a HUGE time saver. All of the NuGet packages are installed by the template, and all the cross-project references have been set. And there is also a set of Visual Studio item templates for adding WCF services, Web API controllers, and all the classes you need to implement Repository and Unit of Work patterns.
Can’t wait to get started? Check out these how-to videos, in which you can see me use Trackable Entities to create an n-tier application from scratch in less than 15 minutes!
One of the goals I’ve had for Trackable Entities is for it to serve as a learning tool. N-tier development is just plain hard. But mainly it’s hard to avoid certain pitfalls in your implementation, like sending unchanged entities across the wire, failing to handle cyclical references, or not making proper use of async and await in your services. The nice thing about the generated projects is that you can see how n-tier is done right, and you can use it to learn a set of best practices for building services that perform queries and updates in a disconnected, scalable fashion.
If you’d like to contribute to the project, check out the page in the online documentation on Contributing. There you’ll find links to video tutorials on both Git and GitHub, as well as tips on reporting bugs and submitting good pull requests.
I’ve implemented your trackable entities solution and really like the simplicity and easy-of-use. However, I’m struggling to determine how to regenerate the entities once the entity project has been generated. I’m sure there’s a simple solution and I’m just missing something. Can you point me in the right direction?
Sure, just delete the generated entities, then reverse engineer them again, either using the EF Power Tools or by adding a new ADO.NET Entity Data Model, as shown in the how-to guides and videos.
Makes sense, got it.
Another question: I’m adding a child entity to a parent entity and see the new entity when I get down to the repository. I’m calling Context.ApplyChanges() on the changed entities and then calling Context.SaveChanges(). However, the parent id is never set in my child entity, so I get a foreign key constraint error. The framework should take care of setting the parent id right? What am I missing?
EF should populate FK properties on child entities. Check if your model is properly configured. You can see the samples for Trackable Entities to compare your code to a working example.
Is there an easy way to sort the items in a ChangeTrackingCollection without having to instantiate a new ChangeTrackingCollection? I’m using dynamic linq to sort the list with a string value using the code below. However, if I’ve already deleted an entity in the list, when I instantiate the new list, that deletion is lost. I would like to sort the list using a string value without having to instantiate a new list. Any ideas?
string sortedBy = View.GetGridSortedBy(entityType);
ChangeTrackingCollection sortedEntities = entities;
IQueryable queryable = entities.AsQueryable();
IQueryable sortedList = queryable.OrderBy(sortedBy);
sortedEntities = new ChangeTrackingCollection(sortedList);
If it’s not too much trouble, would you please post your question by creating an issue on the TE GitHub repo: https://github.com/TrackableEntities/trackable-entities/issues. There you can paste your sample code. Or better yet, create a GitHub repo with a small sample which reproduces your conditions and what you are trying to do. Then I can see the code in context and give you a more accurate answer. Thanks you advance!
Good catch. The new project home page is here: http://trackableentities.github.io/