I am using a third party library GraphDiff which adds Extension methods to DBContext class. My Context class is inherited from Interface like following
MyContext: DbContext,IMyContext
IoC contained register MyContext as IMyContext. Interface doesn't have extension method's signature and third. Now i am not getting how MyContext will have that extension method? If i create Object of MyContext it has that method but when it gets Inject it doesn't
Extensions methods are not part of the type, it is a C# syntactic sugar. When you do :
the compiler will generate the following code :
Where
ExtensionContaineris defined like this :When you use an extension method, the compiler will call a static method. See Extension Methods (C# Programming Guide) for more information.
You can't use the extension method in your case because
contextis no longer aDbContextbutIMyContextand the extension methods are defined forDbContextnot forIMyContext.If you want to use these extension methods, one possible solution is to add them to your interface.
And in your concrete context you will be allowed to use the extension method
Another solution is not to rely anymore on
IMyContextbut injectingMyContext. This solution will make your application more difficult to test and will introduce a strong dependency with Entity Framework.By the way doing so may break Single Responsibility Principle but I don't see an easy way to solve this without big refactoring.