代码之家  ›  专栏  ›  技术社区  ›  alphadogg

使用结构映射提取存储库

  •  0
  • alphadogg  · 技术社区  · 16 年前

    我不知道如何使用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的Reest,这里有一个使用示例:

    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();
            }
    
        }
    }
    
    2 回复  |  直到 16 年前
        1
  •  2
  •   Corey Coogan    16 年前

    这可以通过配置中的一行代码来完成。假设你有这个:

    实体: -客户 -秩序

    并有一个这样的通用存储库模型:

    • 存储库:iRepository

    还有一个应用服务,看起来像:

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

    你会有这样的东西。注意使用扫描器连接您的自定义存储库。

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

    假设您的存储库是在应用程序的其他程序集中定义的,您可以使用注册表将其连接在一起。查看此日志:

    http://blog.coreycoogan.com/2010/05/24/using-structuremap-to-configure-applications-and-components/

        2
  •  0
  •   ozczecho    16 年前

    类似:

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

    然后是注册表类:

    public sealed class MyRegistry : Registry
    {
     ...