【发布时间】:2017-07-10 07:06:56
【问题描述】:
我正在使用 PCL 做一个 Xamarin Native 项目,我有一个用于定义每个平台的数据库位置的第一类:
public class DatabaseConfiguration : IDatabaseConfiguration
{
public string GetConnectionString()
{
//This code is specific to each platform
return PathForEachPlatform;
}
}
我是这样注入的:
builder.RegisterType<DatabaseConfiguration>()
.As<IDatabaseConfiguration>()
.SingleInstance();
现在我不想将SQLiteAsyncConnection 注入我的存储库类。这个类来自这个 nugets:https://www.nuget.org/packages/sqlite-net-pcl
这是我的存储库构造函数:
private SQLiteAsyncConnection Connection { get; }
public SQLiteRepository(IDatabaseConfiguration configuration)
{
Connection = new SQLiteAsyncConnection(configuration.GetConnectionString()); // I wan't to inject the connection not create a new one.
}
}
SQLiteAsyncConnection 没有提供接口,所以我决定使用单例。但是构造函数需要一个数据库路径。此路径由上面的另一个注入使用GetConnectionString() 方法定义。我该怎么做?
现在我有这个代码:
builder.RegisterInstance<SQLiteAsyncConnection>()
.WithParameter(new TypedParameter(typeof(string),"HERE THE PATH TO PASS BUT HOW ?"))
.SingleInstance();
【问题讨论】:
标签: c# sqlite xamarin dependency-injection