3

I have two projects that use the following Unity logic:

container.RegisterType<IUnitOfWork, MyDbContext>(
    new HierarchicalLifetimeManager(),
    new InjectionFactory(
        c => new MyDbContext(configurationService.MySqlConnectionString)
    )
);
container.RegisterType<DbContext, MyDbContext>(
    new HierarchicalLifetimeManager()
);

The first project is a web application that utilises the Unity.MVC4 package so has a bespoke DependencyResolver doing some of the work - this works perfectly.

The second is a non-web application so uses a normal Unity package instance but errors when a call is made that uses MyDbContext. The exception is

System.Data.Entity.Core.MetadataException: Schema specified is not valid. Errors: EntityDataModel.MyProject.ssdl(2,2) : error 0152: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' for the 'System.Data.SqlClient' ADO.NET provider could not be loaded. Make sure the provider assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

I've setup both projects to call the same service, which is in a separate project, in an attempt to isolate the source of the problem to the second project's Unity logic. I should also note I'm using Entity Framework 6 as the ORM.

My question is what Unity code do I need to get the second project to work, or is there some app.config entry I can add to reference the EF assemblies?

Update: After some additional work I noticed that if I reference the DbContext assemblies:

  • EntityFramework
  • EntityFramework.SqlServer

in the second projects the problem disappears. I want to avoid referencing these assemblies because my client projects shouldn't have any knowledge of the ORM.

I've also tried updating the connection string so I'm manually specifying the ORM project's assembly (where my EDMX file is) as mentioned in this StackOverflow question but that hasn't made any difference.

metadata=res://nameOfDll/Model.csdl|res://nameOfDll/Model.ssdl|res://nameOfDll/Model.msl

4
  • are you sure you are providing all the assemblies needed? what about the runtime used? Is the second app built for Any CPU? Commented Jun 5, 2013 at 12:17
  • definitely. both projects share the same repository (and DbContext), and connection string. the only difference I can seem to identify is the Unity.MVC3 doesn't some dependency resolution that I need to resolve manually for the second project. That's the question I'm trying to answer. Commented Jun 5, 2013 at 14:09
  • Your composition root (it is place where you init container) should know about all your dependencies. Commented Jun 11, 2013 at 14:53
  • That's right and I have that in place, for some reason unless I'm using Unity.MVC4 over Unity I have to include the EF assemblies in the client project - which seems odd. I have a feeling I need to manually resolve them - which is the question I'm looking to answer. Commented Jun 12, 2013 at 5:51

1 Answer 1

2
+50

You're doing it in the right way.

With DI you can remove dependencies from your application. So that you can get a "Repository agnostic" application. And you have effectively done it. At least on the projects that 'declare' the dependencies.

However, when the application has to run, you need to specify the concrete objects which will be used for the "declared" abstract dependencies (interface, abstract class) is required.

You do this by registering the types which object will be used for each abstract dependency. In you sample, when a IUnitOfWork or a DbContext, an instance of MyDbContext is provided.

So the projects that 'declare' the dependencies are completely independent of a particular implementation.

However, when you register the dependent types, you're loosing that independence.

Let's see it with an example:

If I say "I'm thirsty, I need to drink but I don't mind what I drink", I'm dependent on any drink, not on a particular one. But If I say "When I'm thirsty I want to drink coke" I'm dependent on coke.

The first part is the abstract definition of the dependency: "any drink" (like the abstract IUnitOfWork or DbContext). And the second part is the concrete dependency: "coke" (like MyDbContext).

So, I'm independent from coke as long as I don't specify that's what I want to drink. But once I say it, I'm dependent.

Perhaps what you're looking for is a way to change the repository at runtime. You can do it: don't register the dependencies in your code, because you need to make a reference to the project with the chosen concrete types. Do it in an external configuration (i.e. a file), so that you can compile your project without reference to the dependencies, and provide the required assemblies at runtime.

NOTE: When I say "declare" I mean using any of the patterns for injecting dependencies, like constructor injection (most advisabke) or any other injection pattern (property dependencies).

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your time but the the question isn't about DI or IoC (as I understand all that), it's really to do with getting those two EF assemblies to be recognised by my composition root. Unity.MVC4 addresses it automatically somehow (project 1) but with just Unity (project 2) I have to include the EF assemblies in the root project even though I never reference them there. They're referenced in the composition root project. If this helps clarify the issue I'd still be interested in some help.
Ok, now I understand it. You should have a look at the assemblies in the bin folder. I bet that in the deployed web app the EF asssemblies are included, and, in the second project they are missing. Can you check it?
How odd. There is no project reference but the EntityFramework.SqlServer.dll was sitting in the \bin folder - even after a Clean Solution it didn't get removed. If I remove it manually and run the project I get the same exception. Nice one @JotaBe. Any thoughts on how to resolve this?
By "resolve" I mean get my composition root using the EF assembly without having to include it in my client projects? FYI: It's included in the composition root project.
Which projects are you using, which references are there between them and what is the composition root? Did you include the Nuget package for EF6 in your composition root? It does include the missing dll.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.