Webinar: N-Tier Entity Framework with DTOs

I recently delivered a free webinar for DevelopMentor on n-tier application development using Entity Framework 4.0.  In it I explained how to use what I call “Trackable Data Transfer Objects” to achieve the same result as “Self-Tracking Entities” but using a more lightweight tracking mechanism to achieve better interoperability, as I outlined in this blog post.

The screencast video is now available to be streamed or downloaded.  The slides and code for the presentation are also available.

About Tony Sneed

Sr. Software Solutions Architect, Hilti Global Application Software
This entry was posted in Technical and tagged . Bookmark the permalink.

21 Responses to Webinar: N-Tier Entity Framework with DTOs

  1. Dominik says:


    when I execute first the service and then the clien, I get a “The underlaying provider failed to open” error.

    I don’t have any idea what the problem is…

    Any advice to solve this error?



  2. Tony says:

    It’s most likely that your connection string is not valid. Check to make sure that your service host has the connection string in app.config and that it’s valid. You need to first attached the Northwind database to your local SQL Server Express instance.

  3. John says:


    I have been looking at your code sample and i had it working for awhile but now its coming up with a odd error, even when i restore your original code i get the same issue.

    Error is:

    Running transformation: System.IO.DirectoryNotFoundException: Could not find a part of the path ‘C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\\.edmx’.

    Any advice to what i’ve done wrong?


  4. Philip says:


    Is there a way to auto generate DataAccess classes?
    Or do you know a tool that can do such? This would help a lot in to migrate faster.



  5. nax says:

    Your presentation is very helpful and i’ve updated my project to be compliant with your advices.
    Nonetheless, I encounter an issue:
    I’ve a USER class with a TRANSACTION collection associated to my user.
    If I create a new user, with some new transactions associated, it works fine, like in your “consoleClient”.
    But if I create a new user without TRANSACTION child, it fails.
    I think the issue is in the “XXXXClientObjects” project.
    The trackableCollection is instanciated only in the “onXXXXCreated” method.
    So, if I get my user from context (the one without any transaction).
    I’ve the error :
    USER.TRANSACTION is not a ITrackabe collection

    Do you have an idea ?

    (sorry for my english, i’m french)

  6. Tony Sneed says:

    @nax: Thanks very much for your question. Would you please send me your project so I can have a look (tony@tonysneed.com)? It may be that your “Transaction” class does not implement ITrackable. However, all classes generated by the xxx-Client.tt template will implement that interface. Additionally, collection properties will materialize as ChangeTrackingCollection. Note, however, that the collection properties will initialize to null, hence the use of a partial class that implements OnXXXCreated in order to initialize the property to a new collection. Ideally, code from the partial class should be placed in the T4 template (I just didn’t have time to do that initially).


  7. Mark says:

    Great article & demo once again!

    The only problem I have with this approach is that your Tracking helper functions e.g. ApplyChanges(), AcceptChanges() etc rely heavily on OBjectContext & ObjectSet – this makes it very difficult to make a simple interfaces (e.g. IObjectContext, IObjectSet) that could be used for faking in tests. Do you have any suggestions for making your pattern testable.

  8. Tony Sneed says:

    @mark: I would suggest using the Repository and Unit of Work patterns to implement testing with fakes. There’s nothing about Trackable DTO’s, or Self-Tracking Entities, that precludes using these patterns. For example, I could have a repository interface like this:

    interface INorthwindRepository
    IObjectSet Customers { get; }
    IObjectSet Order_Details { get; }
    IObjectSet Orders { get; }
    IObjectSet Products { get; }

    For the NorthwindEntities class, all I have to do is change the context template to type the properties as IObjectSet, which is no big deal because ObjectSet already implements that interface. Then I can implement IObjectSet with a FakeObjectSet class that uses in-memory objects and create a FakeNorthwindEntities class that implements INorthwindRepository with properties that explose FakeObjectSet :

    public IObjectSet Customers
    get { return customers; }
    FakeObjectSet customers = new FakeObjectSet();

    Then you create an IUnitOfWork interface with a Save method (and optionally a Delete method). Both NorthwindEntities and FakeNorthwindEntities would implement the interface. NorthwindEntities would call ApplyChanges, SaveChanges, and AcceptChanges, but FakeNorthwindEntities would not. The thing you have to keep in mind is that in an n-tier architecture each operation is atomic and change state is stored in entities themselves.

    Here is a sample showing EF testability features: http://tonysneed.com/elinq/download/ef-tdd.zip. I will have to take some time to come up with an n-tier sample that incorporates unit tests. Stay tuned for that. 🙂


  9. Kevon says:

    Thanks Tony for you work. It does definitely show one what possible with EF and POCOs. We need more Architects like you.

  10. Roger says:

    Thank you Tony for an excellent tutorial, I have been looking for something like this that explain the N-tier architecture on a deep level. I do have a question for you though which I hope that you have time to answer. I’m developing a web application and I have a couple of question in regards to that, first where should I put my aspx file, second should I create multiple projects for all DTO’s and also where do I put them? I’m totally new to development of asp.Net applications. So it would be much apprecited if you could the time to answer my questions.
    Thank you

  11. Phil says:

    Hi Tony!

    Excellent tutorial, but where can I find the sources?


  12. Tony Sneed says:

    Links to the source code are right in the blog post – first or second paragraph. 🙂

  13. Phil says:

    Hi again Tony!

    Any possibility for getting the sources via mail in the meantime?

    That would be very much appreciated! 🙂


  14. Mike says:

    Hi, I run the application, and get the error “Error de proveedor subyacente”:

    El código de usuario no controló System.ServiceModel.FaultException
    Message=Error del proveedor subyacente en Open.
    en Service.NorthwindService.CreateOrder(Order order) en C:\work\N-Tier\TrackableDTO-2\TrackableDTO-2\NorthwindService\NorthwindService.cs:línea 37
    en SyncInvokeCreateOrder(Object , Object[] , Object[] )
    en System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)

    Sorry, by my bad English.

  15. Jaison Readoux says:

    I’ve looked at your T4 code to read the metadata of the EDM that reference in the NorthwindService project. I can’t seem to get any ClientObjects to generate for the Code First approach through the service. Is it even possible to this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.