Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 Autofac and Generic Repository pattern

I am utilizing the Unit Of Work and Generic Repository pattern in my MVC 4 app. The problem I am trying to solve is creating Repository stubs for every entity in my system. In order to utilize the Autofac Ioc I am having to create a repository class and interface for every entity so that I can register it in Autofac.

app start...

builder.RegisterType<SchoolDetailRepository>().As<ISchoolDetailRepository>().InstancePerHttpRequest();

Repository class

 public class SchoolDetailRepository : RepositoryBase<SchoolDetail>, ISchoolDetailRepository
{
    public SchoolDetailRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
    {
    }
}

Interface

public interface ISchoolDetailRepository : IRepository<SchoolDetail>
{
}

It seems like a lot of extra work.

Is there a way to register the generic repository of Type rather than creating all these empty classes?

Then in my service class I can just have the generic type passed into the constructor via Ioc like...

public class SchoolService : ISchoolService
{
    private readonly IRepository<SchoolDetail> _schoolRepository;
    private readonly IUnitOfWork _unitOfWork;

    public SchoolService(IRepository<SchoolDetail> schoolRepository, IUnitOfWork unitOfWork)
    {
        this._schoolRepository = schoolRepository;
        this._unitOfWork = unitOfWork;
    }
}

Container config

// Autofac iOC
        var builder = new ContainerBuilder();
        // register controllers
        builder.RegisterControllers(Assembly.GetExecutingAssembly());

        // register services
        builder.RegisterType<MembershipService>().As<IMembershipService>().InstancePerHttpRequest();
        builder.RegisterType<SchoolService>().As<ISchoolService>().InstancePerHttpRequest();
        builder.RegisterType<StudentService>().As<IStudentService>().InstancePerHttpRequest();
        builder.RegisterType<ClassRoomService>().As<IClassRoomService>().InstancePerHttpRequest();
        builder.RegisterType<CourseService>().As<ICourseService>().InstancePerHttpRequest();
        builder.RegisterType<SchoolYearService>().As<ISchoolYearService>().InstancePerHttpRequest();
        builder.RegisterType<EnrollmentService>().As<IEnrollmentService>().InstancePerHttpRequest();
        builder.RegisterType<TeacherService>().As<ITeacherService>().InstancePerHttpRequest();

        // register data infrastructure
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
        builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();

        // register repositories
        builder.RegisterType<SchoolRepository>().As<ISchoolRepository>().InstancePerHttpRequest();
        builder.RegisterType<TeacherRepository>().As<ITeacherRepository>().InstancePerHttpRequest();
        builder.RegisterType<MembershipRepository>().As<IMembershipRepository>().InstancePerHttpRequest();
        builder.RegisterType<RoleRepository>().As<IRoleRepository>().InstancePerHttpRequest();
        builder.RegisterType<ProfileRepository>().As<IProfileRepository>().InstancePerHttpRequest();
        builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerHttpRequest();
        builder.RegisterType<StudentRepository>().As<IStudentRepository>().InstancePerHttpRequest();
        builder.RegisterType<ClassRoomRepository>().As<IClassRoomRepository>().InstancePerHttpRequest();
        builder.RegisterType<CourseRepository>().As<ICourseRepository>().InstancePerHttpRequest();
        builder.RegisterType<EnrollmentRepository>().As<IEnrollmentRepository>().InstancePerHttpRequest();
        builder.RegisterType<SchoolYearRepository>().As<ISchoolYearRepository>().InstancePerHttpRequest();
        builder.RegisterType<GradeLevelRepository>().As<IGradeLevelRepository>().InstancePerHttpRequest();
        //builder.RegisterType<SchoolDetailRepository>().As<ISchoolDetailRepository>().InstancePerHttpRequest();
        builder.RegisterGeneric(typeof(RepositoryBase<SchoolDetail>)).As(typeof(IRepository<SchoolDetail>));

        // build and setup resolver
        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

the exception is coming from the above code where the expression you gave me runs..

builder.RegisterGeneric(typeof(RepositoryBase<SchoolDetail>)).As(typeof(IRepository<SchoolDetail>));

RepositoryBase

public abstract class RepositoryBase<T> where T : class
{
private LearningCompactPilotContext _dataContext;
private readonly IDbSet<T> _dbset;
protected RepositoryBase(IDatabaseFactory databaseFactory)
{
    DatabaseFactory = databaseFactory;
    _dbset = DataContext.Set<T>();
}

protected IDatabaseFactory DatabaseFactory
{
    get; private set;
}

protected LearningCompactPilotContext DataContext
{
    get { return _dataContext ?? (_dataContext = DatabaseFactory.Get()); }
}

... more code

}

like image 545
JBeckton Avatar asked Sep 12 '12 14:09

JBeckton


People also ask

How do I register a generic repository in Autofac?

You need to exactly write like this: builder. RegisterGeneric(typeof(RepositoryBase<>)) . As(typeof(IRepository<>)); note the empty <> and it will register your Repository for all of your entites.

What is generic repository pattern?

The repository pattern is intended to create an Abstraction layer between the Data Access layer and Business Logic layer of an Application. It is a data access pattern that prompts a more loosely coupled approach to data access.

Is Autofac A IOC?

Autofac is an addictive IoC container for . NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .

What is Repository pattern in MVC C#?

What is a Repository Design Pattern? By definition, the Repository Design Pattern in C# mediates between the domain and the data mapping layers using a collection-like interface for accessing the domain objects. Repository Design Pattern separates the data access logic and maps it to the entities in the business logic.


1 Answers

You need the open generics feature of Autofac:

builder.RegisterGeneric(typeof(RepositoryBase<>))
   .As(typeof(IRepository<>));

Then you use your repositories exactly as you described:

public class SomeService
{
    private readonly IRepository<SomeEntity> _repository;

    public SchoolService(IRepository<SomeEntity> repository)
    {
        this._repository= repository;
    }
}
like image 156
nemesv Avatar answered Oct 24 '22 14:10

nemesv