sách gpt4 ai đã đi

dependency-injection - 忍者: Constructor parameter

In lại 作者:行者123 更新时间:2023-12-04 17:23:57 27 4
mua khóa gpt4 Nike

我将 Ninject 与 ASP.NET MVC 4 一起使用。我正在使用存储库并希望进行构造函数注入(inject)以将存储库传递给其中一个 Controller 。

这是我的存储库界面:

public interface IRepository where T : TableServiceEntity
{
void Add(T item);
void Delete(T item);
void Update(T item);
IEnumerable Find(params Specification[] specifications);
IEnumerable RetrieveAll();
void SaveChanges();
}
AzureTableStorageRepository下面是 IRepository 的实现:
public class AzureTableRepository : IRepository 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;
}

请注意所需的 tableName 参数,因为我使用通用表存储库将数据持久保存到 Azure。

最后我有以下 Controller 。
public class CategoriesController : ApiController
{
static IRepository _repository;

public CategoriesController(IRepository repository)
{
if (repository == null)
{
throw new ArgumentNullException("repository");
}

_repository = repository;
}

现在我想将一个存储库注入(inject)到 Controller 中。所以我创建了一个包含绑定(bind)的模块:
/// 
/// Ninject module to handle dependency injection of repositories
///

public class RepositoryNinjectModule : NinjectModule
{
public override void Load()
{
Bind<>>().To<>>();
}
}

模块的加载在 NinjectWebCommon.cs 中完成。
/// 
/// Creates the kernel that will manage your application.
///

/// The created kernel.
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind().To();

RegisterServices(kernel);
return kernel;
}

///
/// Load your modules or register your services here!
///

/// The kernel.
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);
}
DependencyResolver创建是因为 Ninject 的 DependencyResolverhoàn thành System.Web.Mvc.IDependencyResolver这不能分配给 GlobalConfiguration.Configuration WebApi 应用程序。

所以有了这一切,Ninject 部分实际上是在 Controller 中注入(inject)正确的类型,但 Ninject 无法在 AzureTableRepository 的构造函数中注入(inject) tableName 参数.

在这种情况下我怎么能做到这一点?我查阅了很多文章和 ninject 文档来了解如何使用参数,但我似乎无法让它工作。

任何帮助,将不胜感激。

1 Câu trả lời

我会使用 WithConstructorArgument()方法如...

Bind<>>().To<>>()
.WithConstructorArgument("tableName", "categories");

存储库设计的其余部分可能是另一个问题。恕我直言,在 Actor 中创建一张 table 或做任何繁重的工作似乎是一个很大的禁忌。

关于dependency-injection - 忍者: Constructor parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13893268/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com