0

I'm trying to build a library project which will assist me in my other projects with some extension methods. The type which will be extended is DbContext. Here is some example

public bool Insert<TEntity>(this DbContext Context, TEntity entity) where TEntity : class
    {
        if (Context.Entry(entity).State == EntityState.Detached) //Entity is detached
        {
            Context.Set<TEntity>().Add(entity);
        }
        else //Entity is attached
        {
            Context.Entry(entity).State = EntityState.Added;
        }

        var str = Context.GetValidationErrors();

        if (Context.GetValidationErrors().Any()) return false;

        Context.SaveChanges();
        return true;

    }

Where is the problem ?

I'm exposing the context through WCF Data Services, and it shrinks my context capabilities, which for me is contraditory, since the goal here is to expose data, how can you expose data without the meanings to reach it ? So, how can i accomplish this task, exposing my entension methods as extensions methods to my client side context operations.

EDIT

I have been reading arround and found this answer from Ladislav Mrnka

Implement WCF Data Service using the Repository Pattern

Community
  • 1
  • 1

1 Answers1

1

DbContext API differs from API of generated context, when you're adding a reference to a data service. Moreover, possibilities of generated context are limited comparing to DbContext. Client side context is a helper for building OData queries, and it is not perfect. I don't think, that you could port every extension method without re-implementation (if it will be possible at all).

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • I understand, but this is contraditory, since the main goal is to expose data from server to client side. – Diego Garcia Vieira Dec 02 '13 at 14:47
  • Can you imagine any workaround ? I have great methods to use with context, which I'll have to do with WCF Service instead WCF Data Services. But, one the reasons that I choose data services over wcf service, is the easy way to work with Disconnected Entities Graphs – Diego Garcia Vieira Dec 02 '13 at 17:47
  • @DiegoGarciaVieira: it really depends on what functionality you want to have at client side. DS context is quite similar with DBContext, but there's difference. It will be better, if you'll post a question for every particular case, because now you're asking very generic question. – Dennis Dec 02 '13 at 19:54
  • Yes, I understand. I updated my question with a answer from Ladislav Mrnka, and what he said about over engineering took me with him... I'll mark your answer as right, since you gave the overall context. – Diego Garcia Vieira Dec 03 '13 at 10:31