【发布时间】:2011-02-24 11:21:39
【问题描述】:
我不确定如何使用 StructureMap 扫描特定命名空间中的所有存储库。大多数存储库采用以下形式:
namespace CPOP.Infrastructure.Repositories
{
public class PatientRepository : LinqRepository<Patient>, IPatientRepository
{
}
}
namespace CPOP.Infrastructure.Repositories
{
public class LinqRepository<T> : Repository<T>, ILinqRepository<T>
{
}
}
namespace CPOP.Domain.Contracts.Repositories
{
public interface IPatientRepository : ILinqRepository<Patient>
{
}
}
我试过了:
x.Scan(scanner =>
{
scanner.Assembly(Assembly.GetExecutingAssembly());
scanner.ConnectImplementationsToTypesClosing(typeof(ILinqRepository<>));
})
但是,它只选择 LinqRepository 类。获取我将在其中转储的各种存储库的最佳方式是什么?
并且,根据 Joshua 的要求,这里有一个使用示例:
namespace CPOP.ApplicationServices
{
public class PatientTasks : IPatientTasks
{
private readonly IPatientRepository _patientRepository;
public PatientTasks(IPatientRepository patientRepository)
{
_patientRepository = patientRepository;
}
public Patient GetPatientById(int patientId)
{
int userId; // get userId from authentication mechanism
return _patientRepository.FindOne(new PatientByIdSpecification(patientId));
}
public IEnumerable<Patient> GetAll()
{
int userId; // get userId from authentication mechanism
return _patientRepository.FindAll();
}
}
}
【问题讨论】:
-
那里有很多接口/基类。您能否举例说明如何检索存储库?你会请求什么接口?
标签: interface dependency-injection structuremap