使用 Unity XML 设置属性

发布于 2024-11-18 12:43:43 字数 654 浏览 10 评论 0原文

我想在存储库类中设置实体框架 ObjectContext - ObjectContext 是存储库的属性。

我正在使用 Unity XML 配置:

<register type="IUsersRepository" mapTo="SqlUsersRepository" >
  <property name="MyObjectContext">
    <value value="Per-Request" typeConverter="ObjectContextTypeConverter" />
  </property>
</register>

ObjectContextTypeConverter 解释属性的值 - 在本例中为“Per-Request” - 并使用存储在 HttpContext.Current.Items(asp.net 中的每个请求集合)中的 ObjectContext。

仅当在 Global.asax 文件的 Application_Start 方法中加载 Unity 配置时,才会调用 ObjectContextTypeConverter.ConvertFrom 方法。然而,当我尝试使用 Unity 解析接口时,不会再次调用 ConvertFrom 方法。

有办法解决这个问题吗?

I want to set the Entity Framework ObjectContext in a repository class - the ObjectContext being a property of the repository.

I am using Unity XML configuration:

<register type="IUsersRepository" mapTo="SqlUsersRepository" >
  <property name="MyObjectContext">
    <value value="Per-Request" typeConverter="ObjectContextTypeConverter" />
  </property>
</register>

The ObjectContextTypeConverter interprets the value of the property - in this case "Per-Request" - and uses an ObjectContext stored in HttpContext.Current.Items (a per request collection in asp.net).

The ObjectContextTypeConverter.ConvertFrom method is only called once when the Unity configurations is loaded in the Application_Start method of the Global.asax file. Yet when I try to resolve an interface using Unity - the ConvertFrom method won't be called again.

Is there a way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2024-11-25 12:43:43

原因是您将其设置为一个值。您可以将其设置为依赖项:

<property name="MyObjectContext">
    <dependency />
</property>

并通过注入工厂注入上下文:

Container.RegisterType<ObjectContext>(
    new InjectionFactory(c => GetTheContextInstance()));

The reason is that you're setting it as a value. You can set it as a dependency:

<property name="MyObjectContext">
    <dependency />
</property>

and inject the context through an in injection factory:

Container.RegisterType<ObjectContext>(
    new InjectionFactory(c => GetTheContextInstance()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文