2010-05-27 109 views
0

我不確定如何使用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類。拾取各種知識庫的最佳方式是什麼?我會在那裏傾倒?

而且,按照約書亞的reuest,這裏使用的例子:

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

    } 
} 
+0

你有很多接口/基類在那裏。你能舉一個例子說明你將如何檢索一個存儲庫?你會要求什麼界面? – 2010-05-27 12:53:02

回答

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

      //   }); 
     } 
    } 

假設您的存儲庫在你的應用程序的一些其他組件定義,您可以使用註冊來把它掛在一起。看看這篇文章:

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

0

喜歡的東西:

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

然後在註冊表類:

public sealed class MyRegistry : Registry 
{ 
... 
+0

我一直在尋找能自動收集它們的東西,而不必在註冊表類中單獨指定每一個,因爲它們都具有反覆的依賴接口和類。 – alphadogg 2010-05-27 02:21:47