RKP
RKP

Reputation: 5385

using dependency injection for Entity Framework 4.1 along with "Unit of Work" and "Repository" patterns

I have seen various implementations for Entity Framework using unit of work and repository patterns. ideally I want to use interfaces for unit of work, database context, repository (for both generic and specific repositories) and instantiate them in the BootStrapper code on application startup. Is this actually possible?

samples checked:

http://microsoftnlayerapp.codeplex.com (doesn't have any separate unit of work class, the database context itself implements IUnitOfwork)

http://efmvc.codeplex.com (uses a database factory to instantiate database context without utilising DI and database context does not implement interface)

http://code.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8 (instantiates database context and the repositories inside unit of work without any DI)

some implementations don't use any interface for database context and some instantiate database context and the repositories inside the UnitOfWork class and pass concrete context class in the constructor and some pass the interface for unit of work in the constructor for repository and it is totally confusing. Is there one best practice approach that allows DI and TDD when using Entity Framework 4.1? Is there a sample that demonstrates this?

Upvotes: 1

Views: 1787

Answers (1)

abatishchev
abatishchev

Reputation: 100248

I have next so far:

Declaration:

namespace Contracts // Contracts.dll
{
    public interface IUserRepository : IUserRepository
    {
    }
}

Implementation:

namespace Data // Data.dll
{
    class UserRepository : Contracts.IUserRepository
    {
    }
}

IoC utility:

namespace Core // Core.dll
{
    public static class IoC
    {
        private static IUnityContainer _container = LoadContainer();

        private static IUnityContainer LoadContainer()
        {
            var unitySection = (Microsoft.Practices.Unity.Configuration.UnityConfigurationSection)ConfigurationManager.OpenMappedExeConfiguration(
                new ExeConfigurationFileMap { ExeConfigFilename = System.Web.HttpContext.Current.Server.MapPath(UserSettings.UnityContainerPath) },
                ConfigurationUserLevel.None).GetSection("unity");

            var container = new UnityContainer();
            unitySection.Configure(container);
            return container;
        }

        public static T Resolve<T>()
        {
            return _container.Resolve<T>();
        }
    }
}

Unity.config:

<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    </configSections>
    <unity>
        <containers>
            <container>
                <types>
                    <type type="Contracts.IUserRepository, Contracts" mapTo="Data.UserRepository, Data">
                        <lifetime type="singleton" />
                    </type>
                </types>
            </container>
        </containers>
    </unity>
</configuration>

Upvotes: 0

Related Questions