1つの基本汎用リポジトリと、基本リポジトリから継承する多くのクラスリポジトリがあります。文字列値を汎用リポジトリに渡す必要があります。
これが私の汎用リポジトリです
public class Repository<T> where T : EntityBase
{
private string SessionId;
public Repository(string sessionId)
{
this.SessionId = sessionId;
}
protected virtual IDbConnection GetCn()
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["SalesDb"].ConnectionString);
}
public virtual int Insert(T entity)
{
entity.ChUser = "anders.persson";
entity.ChTime = DateTime.Now;
using (IDbConnection cn = GetCn())
{
cn.Open();
return cn.Insert(entity);
}
}
// MORE CODE
}
}
そしてインターフェース
public interface IRepository<T>
{
int Insert(T entity);
}
マイクラスリポジトリ
public class MarketRepository : Repository<Market>, IMarketRepository
{
}
そしてインターフェース
public interface IMarketRepository : IRepository<Market>
{
}
次に、sessionIdをGenericRepositoriesコンストラクターに渡します。どうやってやるの。この場合、すべてのクラスリポジトリにコンストラクタを実装し、それをベースリポジトリに渡す必要があります。そして、インターフェースはそのコンストラクターについてさえ知りません。
これがNinjectバインディングです
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>));
kernel.Bind<ILeadRepository>().To<LeadRepository>();
kernel.Bind<IPricelistRepository>().To<PricelistRepository>();
kernel.Bind<IOptionalGroupRepository>().To<OptionalGroupRepository>();
kernel.Bind<IProductGroupRepository>().To<ProductGroupRepository>();
kernel.Bind<IProductRepository>().To<ProductRepository>();
kernel.Bind<IMarketRepository>().To<MarketRepository>();
kernel.Bind<IModelRepository>().To<ModelRepository>();
kernel.Bind<IOrderRepository>().To<OrderRepository>();
}