C# 带参数的LightInject寄存器泛型?
我有接口IRepository和带有参数构造函数的类存储库,它们实现了IRepository。类和接口都是泛型的。我想在注册时传递此参数。代码如下:C# 带参数的LightInject寄存器泛型?,c#,dependency-injection,light-inject,C#,Dependency Injection,Light Inject,我有接口IRepository和带有参数构造函数的类存储库,它们实现了IRepository。类和接口都是泛型的。我想在注册时传递此参数。代码如下: public interface IRepository<T> where T : class, new() { public Task<int> InsertAsync(T entity); } public class Repository<T> : IRep
public interface IRepository<T> where T : class, new()
{
public Task<int> InsertAsync(T entity);
}
public class Repository<T> : IRepository<T> where T : class, new()
{
public MobileServiceClient MobileService { get; set; }
public Repository(MobileServiceClient mobileService)
{
MobileService = mobileService;
}
public async Task<int> InsertAsync(T entity)
{
var table = MobileService.GetSyncTable<T>();
await table.InsertAsync(entity);
return 1;
}
}
公共接口i假设,其中T:class,new()
{
公共任务插入同步(T实体);
}
公共类存储库:IRepository,其中T:class,new()
{
公共移动服务客户端移动服务{get;set;}
公共存储库(mobileService客户端mobileService)
{
移动服务=移动服务;
}
公共异步任务插入同步(T实体)
{
var table=MobileService.GetSyncTable();
等待表。插入同步(实体);
返回1;
}
}
我像这样注册普通班
Container.Register(typeof(IRepository<>), typeof(Repository<>));
Container.Register(typeof(IRepository)、typeof(Repository));
我在注册时找不到传递参数的方法。是什么阻止您将
MobileServiceClient
注册为Singleton
?@Steven我正在一个.Net标准库(Xamarin表单)中初始化MobileServiceClient
,在其中注册所有服务。我想将此实例传递到我的业务逻辑库。我不确定我是否理解。为什么下面的调用不能解决问题?:Container.Register(=>client,new percontainerlifest())
。在注册存储库(Container.Register)(typeof(IRepository),typeof(Repository));
)时,有没有办法注册客户端,因为它会像这样初始化属性公共存储库(mobileService客户端mobileService){mobileService=mobileService;}
在构造函数中?