views:

54

answers:

2

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

    }
}
A: 

Something like:

        Assembly ass = Assembly.GetCallingAssembly();
        Container.Configure(x => x.Scan(scan =>
        {
            scan.Assembly(ass); 
            scan.LookForRegistries();
        }));

Then the Registry class:

public sealed class MyRegistry : Registry
{
 ...
ozczecho
I was looking for something that would collect them automatically without having to specify each one individually in a registry class, because they'll all have repeated dependent interfaces and classes.
alphadogg
A: 

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:

  • Repository : IRepository

And have a app services that look like:

public AppService(IRepository<Customer> custRepo, IRepository<Order> orderRepo)

You would have something like this. Notice the bit about using the scanner to hook up your custom repositories.

public class SmRegistry : Registry
    {
        public SmRegistry()
        {
            For(typeof (IRepository<>))
                .Use(typeof (Repository<>));

            //using this will find any custom repos, like CustomerRepository : Repository<Customer>
            //Scan(scanner =>
            //         {
            //             scanner.TheCallingAssembly();
            //             scanner.ConnectImplementationsToTypesClosing(typeof (IRepository<>));

            //         });
        }
    }

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/

Corey Coogan