c# - CaSTLe Windsor Inversion of Control (IoC) : using the Web. 配置解决依赖关系

标签 c# asp.net-mvc asp.net-mvc-2 inversion-of-control castle-windsor

我正在根据 related question 的答案探索 CaSTLe Windsor 的安装程序功能.我想使用 Web.config 来指定数据库的名称,我不想在我的代码中显式设置数据库名称。我尝试了 Krzysztof Koźmic 的示例,当我首次亮相该项目时,我在 container.Register 处的断点被命中,但依赖关系仍未解决:

public class RepositoriesInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(AllTypes.FromThisAssembly()
                            .Where(Component.IsInSameNamespaceAs<SqlUsersRepository>())
                            .WithService.DefaultInterface()
                            .Configure(c => c.LifeStyle.Transient
                            .DependsOn(new { databaseName = "MyDatabaseName" })));
    }
}

Sanders 建议我们可以使用 Web.config 来解决依赖关系(请注意,他这样做是为了获取连接字符串,但我的连接字符串是加密的,所以我使用的方式略有不同):

<castle>
    <components>
      <component id="SqlUsersRepository"
          service="MyDevArmyModel.Entities.IUsersRepository, MyDevArmyModel"
          type="MyDevArmyModel.Entities.SqlUsersRepository, MyDevArmyModel">
        <parameters>
          <databaseName>MyDatabaseName</databaseName>
        </parameters>
      </component>
    </components>
</castle>

我的 SqlUsersRepositoryIUsersRepository 在同一个命名空间中,但它们是在当前项目中引用的类库的一部分 . SqlUsersRepository 从 Web.Config 数据库名称中查找连接字符串:

public interface IUsersRepository
{
    IQueryable<User> Users { get; }
    // ...
    // and some other things 
    // ...

}

public class SqlUsersRepository : IUsersRepository
{
    private DataContext dataContext;
    private Table<User> usersTable;

    public IQueryable<User> Users { get { return usersTable; } }

    public SqlUsersRepository(string databaseName)
    {
        HttpRequestWrapper request = new HttpRequestWrapper(System.Web.HttpContext.Current.Request);
        Configuration config = WebConfigurationManager.OpenWebConfiguration(request.ApplicationPath);
        dataContext = new DataContext(config.GetConnetionString(databaseName));
        usersTable = dataContext.GetTable<User>();
    }

    // .... the rest of the repository goes here
}

有什么帮助吗?

附言

即使我使用硬编码的数据库名称(如 RepositoriesInstaller 中所示),我仍然遇到异常:

Server Error in '/' Application. Can't create component 'MyProjectName.Controllers.UserController' as it has dependencies to be satisfied. MyProjectName.Controllers.UserController is waiting for the following dependencies:

Services: - MyProjectName.Entities.IUsersRepository which was not registered. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Castle.MicroKernel.Handlers.HandlerException: Can't create component 'MyProjectName.Controllers.UserController' as it has dependencies to be satisfied. MyProjectName.Controllers.UserController is waiting for the following dependencies:

Services: - MyProjectName.Entities.IUsersRepository which was not registered.

更新 我已经发布了一个解决异常问题的答案,但我还没有弄清楚如何使用 Web.config 来存储温莎城堡的特定部分。

最佳答案

您应该能够通过将 XML 缩减为以下内容来实现这一点:

<castle>
    <components>
      <component id="SqlUsersRepository">
        <parameters>
          <databaseName>MyDatabaseName</databaseName>
        </parameters>
      </component>
    </components>
</castle>

然后确保您的注册在正确的 key /ID 下注册存储库 - 例如像这样:

container.Register(AllTypes(...).Configure(c => c.Named(c.Implementation.Name)))

所有键都将设置为每个具体类型的短名称。

请注意,此命名方案可能不适合您,因此您可能应该将 c.Named(...) 调整为您认为适合您的用例的任何内容。只需确保存储库的注册 key 与 XML 中的 id 属性相匹配即可。

关于c# - CaSTLe Windsor Inversion of Control (IoC) : using the Web. 配置解决依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5129713/

相关文章:

c# - 不支持关键字 : 'data source' in ASP.net Entity Framework 6

asp.net-mvc-2 - ASP.NET MVC session 超时和 TempData

asp.net-mvc - 错误 "The view at ' ~/Views/Page/home.aspx' 必须派生自 ViewPage、ViewPage<TViewData>、ViewUserControl 或 ViewUserControl<TViewData>"

asp.net-mvc-2 - Azure MVC2 应用程序的表单例份验证提供程序?

c# - 在典型的 MVC 应用程序中使用 Akka.NET。如何?

c# - MemoryBarrier 是否保证所有内存的内存可见性?

c# - 在 C# 中查找两个列表之间的重叠

c# - 使用 AspNet.Identity UserManager 时检测更改

c# - 如何使用 jQuery 将 JSON 数据绑定(bind)到 Asp.net MVC 中的下拉列表

asp.net - 无法从程序集中加载 BuildTasks.Csc 任务?