2016-08-15 50 views
0

我使用SQLite,讓通過它通過在標準的方式來實現ISQLite接口[裝配:依賴(typeof運算(SQLite_iOS))] ...棱鏡Xamarin 6.2 IDependencyService服務

而且我正在努力通過Repository模式實現數據訪問模型

我BaseRepository類看起來像這樣

public abstract class BaseRepository<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : class, new() 
{ 
    private readonly SQLiteAsyncConnection _connection; 

    protected BaseRepository(ISQLite sqlite) 
    { 
     _connection = sqlite.GetAsyncConnection(); 

     _connection.CreateTableAsync<TEntity>().Wait(); 
    } 

對於每個模型我有BaseRepository類的實現

public class SectorsRepository : BaseRepository<Sectors, int>, ISectorsRepository 
{ 
    public SectorsRepository(ISQLite context) 
     : base(context) 
    { 

    } 
} 

我現在正在努力如何註冊每個存儲庫類。顯然,這不起作用,因爲我沒有一個dependencyService的實例;

protected override void RegisterTypes() 
    { 

     Container.RegisterType<ISectorsRepository, SectorsRepository>(new InjectionConstructor(dependencyService.Get<ISQLite>())); 
} 

任何想法?

謝謝

回答

1

你不應該使用DependencyService可言。這不是必需的。你已經有了一個真正的DI容器。改爲使用它。即使您在平臺項目中使用DependencyService屬性,也不需要通過調用DependencyService來解決它們。棱鏡會自動爲你解決它們。您也不應該手動向您的ctor注入任何東西,因爲只要您使用容器註冊了服務,您的容器就會自動爲您執行此操作。

+0

謝謝!不知道棱鏡自動解決它們。最後,我的代碼轉換爲: Container.RegisterType (); – James

0

回答我自己。這段代碼使它工作。我唯一不喜歡的是DependencyService的手動實例化。有沒有人有更優雅的解決方案?

感謝

protected override void RegisterTypes() 
     { 
      DependencyService ds = new DependencyService(); 
      Container.RegisterType<IUnitOfWork, UnitOfWork>(new InjectionConstructor(ds.Get<ISQLite>())); 
.... 
}