ASP.NET MVC 4와 함께 Ninject를 사용하고 있습니다. 저장소를 사용하고 있으며 저장소 중 하나에 저장소에 전달하기 위해 생성자 삽입을 수행하려고합니다.Ninject : 생성자 매개 변수
public class AzureTableRepository<T> : IRepository<T> where T : TableServiceEntity
{
private readonly string _tableName;
private readonly TableServiceContext _dataContext;
private CloudStorageAccount _storageAccount;
private CloudTableClient _tableClient;
public AzureTableRepository(string tableName)
{
// Create an instance of a Windows Azure Storage account
_storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
_tableClient = _storageAccount.CreateCloudTableClient();
_tableClient.CreateTableIfNotExist(tableName);
_dataContext = _tableClient.GetDataServiceContext();
_tableName = tableName;
}
주 I가 데이터를 유지하기 위해 일반 테이블 저장소를 사용했기 때문에 TABLENAME 매개 변수가 필요합니다 :
public interface IRepository<T> where T : TableServiceEntity
{
void Add(T item);
void Delete(T item);
void Update(T item);
IEnumerable<T> Find(params Specification<T>[] specifications);
IEnumerable<T> RetrieveAll();
void SaveChanges();
}
아래
AzureTableStorageRepository
는 IRepository<T>
의 구현입니다 :
이 내 저장소 인터페이스 하늘빛.
그리고 마지막으로 다음 컨트롤러가 있습니다.
public class CategoriesController : ApiController
{
static IRepository<Category> _repository;
public CategoriesController(IRepository<Category> repository)
{
if (repository == null)
{
throw new ArgumentNullException("repository");
}
_repository = repository;
}
는 지금은 컨트롤러에 저장소를 주입합니다. 그래서 바인딩이 포함 된 모듈을 만들었습니다
/// <summary>
/// Ninject module to handle dependency injection of repositories
/// </summary>
public class RepositoryNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IRepository<Category>>().To<AzureTableRepository<Category>>();
}
}
모듈의 로딩이 이루어집니다를 Ninject에의 DependencyResolver
이 System.Web.Mvc.IDependencyResolver
를 구현하고이에 할당 할 수 없기 때문에 DependencyResolver
제작 한 NinjectWebCommon.cs
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
// Load the module that contains the binding
kernel.Load(new RepositoryNinjectModule());
// Set resolver needed to use Ninject with MVC4 Web API
GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
}
WebApi 응용 프로그램의 GlobalConfiguration.Configuration
.
Ninject 부분은 실제로 컨트롤러에 올바른 형식을 주입하지만 Ninject는 생성자 에 tableName 매개 변수를 삽입 할 수 없습니다.
이 경우 어떻게해야합니까? 나는 많은 기사와 ninject 문서를 참조하여 매개 변수를 어떻게 사용할 수 있는지 보았지만 작동시키지 못했습니다.
도움을 주시면 감사하겠습니다.
나는 생성자를 제거 할 것이라고 생각한다. 왜냐하면 실제로는 ctor에서 초기화를 많이하는 것이 좋지 않기 때문이다. Thx! –