14

我在我的 MVC 4 应用程序中使用了工作单元和通用存储库模式。我要解决的问题是为系统中的每个实体创建存储库存根。为了利用 Autofac Ioc,我必须为每个实体创建一个存储库类和接口,以便我可以在 Autofac 中注册它。

应用开始...

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

存储库类

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

界面

public interface ISchoolDetailRepository : IRepository<SchoolDetail>
{
}

这似乎是很多额外的工作。

有没有办法注册 Type 的通用存储库而不是创建所有这些空类?

然后在我的服务类中,我可以通过 Ioc 将泛型类型传递给构造函数,例如......

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;
    }
}

容器配置

// 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));

异常来自上面的代码,您给我的表达式在其中运行..

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

存储库库

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

}

4

1 回答 1

36

您需要Autofac的开放泛型功能:

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

然后您完全按照您的描述使用您的存储库:

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

    public SchoolService(IRepository<SomeEntity> repository)
    {
        this._repository= repository;
    }
}
于 2012-09-12T14:46:49.870 回答