【问题标题】:Picking Up Repositories With Structuremap使用 Structuremap 获取存储库
【发布时间】: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


【解决方案1】:

类似:

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

然后是注册表类:

public sealed class MyRegistry : Registry
{
 ...

【讨论】:

  • 我一直在寻找可以自动收集它们而不必在注册表类中单独指定每个的东西,因为它们都会有重复的依赖接口和类。
【解决方案2】:

只需在配置中添加一行代码即可完成此操作。假设你有这个:

实体: - 顾客 - 订购

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

  • 存储库: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<>));

            //         });
        }
    }

假设您的存储库是在您的应用程序的某个其他程序集中定义的,您可以使用 Registries 将它们连接在一起。看看这篇文章:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2011-02-14
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2010-10-28
    相关资源
    最近更新 更多