I am not sure how to use StructureMap to scan for all repositories in a particular namespace. Most repositories take the form:
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>
{
}
}
I tried:
x.Scan(scanner =>
{
scanner.Assembly(Assembly.GetExecutingAssembly());
scanner.ConnectImplementationsToTypesClosing(typeof(ILinqRepository<>));
})
But, it only picks up the LinqRepository class. What’s the best way to pick up the various repositories I’ll be dumping in there?
And, as per Joshua’s reuest, here’s an example of use:
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();
}
}
}
This can be done with just one line of code in your configuration. Assuming you have this:
Entities:
– Customer
– Order
And have a generic repository model like this:
And have a app services that look like:
You would have something like this. Notice the bit about using the scanner to hook up your custom repositories.
Assuming your Repositories are defined in some other assembly from your application, you can use Registries to hook it all together. Check out this post:
http://blog.coreycoogan.com/2010/05/24/using-structuremap-to-configure-applications-and-components/