使用 spring.NET 配置静态属性

发布于 2024-08-17 11:29:54 字数 1244 浏览 7 评论 0原文

使用 spring.NET 配置以下类的最佳实践是什么?

using System.Collections.Generic;
using Edu3.DTOModel;

namespace Edu3.Data.SubsonicProvider.RepositoryFramework
{
    public static class RepositoryFactory
    {
        private static readonly Dictionary<string, object> Repositories = 
            new Dictionary<string, object>();

        static RepositoryFactory()
        {
            Repositories.Add(typeof(ISsoUrlTemplateRepository).Name, 
                new SsoUrlTemplateRepository());
            Repositories.Add(typeof(IPackageSessionNodeRepository).Name, 
                new PackageSessionNodeRepository());
            Repositories.Add(typeof(IPackageSessionNodeFinishedRepository).Name, 
                new PackageSessionNodeFinishedRepository());
        }

        public static IRepository<TEntity> GetRepository<TEntity, TRepository>()
            where TEntity : IEntity
        {
            var interfaceShortName = typeof(TRepository).Name;

            // The provider was in the cache, so retrieve it
            var repository = (IRepository<TEntity>)Repositories[interfaceShortName];

            return repository;
        }
    }
}

我想使用 Spring.NET 添加存储库。这可能吗?

What are best practices to configure following class with spring.NET?

using System.Collections.Generic;
using Edu3.DTOModel;

namespace Edu3.Data.SubsonicProvider.RepositoryFramework
{
    public static class RepositoryFactory
    {
        private static readonly Dictionary<string, object> Repositories = 
            new Dictionary<string, object>();

        static RepositoryFactory()
        {
            Repositories.Add(typeof(ISsoUrlTemplateRepository).Name, 
                new SsoUrlTemplateRepository());
            Repositories.Add(typeof(IPackageSessionNodeRepository).Name, 
                new PackageSessionNodeRepository());
            Repositories.Add(typeof(IPackageSessionNodeFinishedRepository).Name, 
                new PackageSessionNodeFinishedRepository());
        }

        public static IRepository<TEntity> GetRepository<TEntity, TRepository>()
            where TEntity : IEntity
        {
            var interfaceShortName = typeof(TRepository).Name;

            // The provider was in the cache, so retrieve it
            var repository = (IRepository<TEntity>)Repositories[interfaceShortName];

            return repository;
        }
    }
}

I would like to add the repositories with Spring.NET. Is this possible?

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

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

发布评论

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

评论(2

云雾 2024-08-24 11:29:54

将字典公开为公共属性并删除字段的 readonly 修饰符,并且不要使类静态。然后使用这个配置:

  <object id="RepositoryFactory" type="Edu3.Data.SubsonicProvider.RepositoryFramework. RepositoryFactory, Edu3.Data.SubsonicProvider.RepositoryFramework">
    <property name="Repositories">
      <dictionary key-type="System.Type" value-type="System.Object">
        <entry value-ref="SsoUrlTemplateRepository">
          <key>
            <expression value="T(Edu3.Data.SubsonicProvider.RepositoryFramework.SsoUrlTemplateRepository, Edu3.Data.SubsonicProvider.RepositoryFramework)"/>
          </key>           
        </entry>
      </dictionary>
    </property>
  </object>

  <object id="SsoUrlTemplateRepository" type="Edu3.Data.SubsonicProvider.RepositoryFramework.SsoUrlTemplateRepository, Edu3.Data.SubsonicProvider.RepositoryFramework" singleton="false"/>

Expose the dictionary as a public property and remove the readonly modifier of the field and don't make your class static. Then use this configuration:

  <object id="RepositoryFactory" type="Edu3.Data.SubsonicProvider.RepositoryFramework. RepositoryFactory, Edu3.Data.SubsonicProvider.RepositoryFramework">
    <property name="Repositories">
      <dictionary key-type="System.Type" value-type="System.Object">
        <entry value-ref="SsoUrlTemplateRepository">
          <key>
            <expression value="T(Edu3.Data.SubsonicProvider.RepositoryFramework.SsoUrlTemplateRepository, Edu3.Data.SubsonicProvider.RepositoryFramework)"/>
          </key>           
        </entry>
      </dictionary>
    </property>
  </object>

  <object id="SsoUrlTemplateRepository" type="Edu3.Data.SubsonicProvider.RepositoryFramework.SsoUrlTemplateRepository, Edu3.Data.SubsonicProvider.RepositoryFramework" singleton="false"/>
So尛奶瓶 2024-08-24 11:29:54

我在初始化应用程序时遇到了同样的问题,因为在 spring.net 中不存在调用(静态)类上的静态方法的方法。但是您可以尝试使您的类成为非静态尝试以下操作:

public class RepositoryFactory
{
    private static RepositoryFactory instance;

    private static readonly Dictionary<string, object> Repositories =
        new Dictionary<string, object>();

    private RepositoryFactory()
    {
    }

    public static RepositoryFactory GetInstance()
    {
        if (instance == null)
        {
            instance = new RepositoryFactory();
        }
        return instance;
    }

    public static void AddRepository(object repository)
    {
        Repositories.Add(repository.GetType().Name, repository);
    }
}

使用 spring-config:

<objects xmlns="http://www.springframework.net" >

  <object id="RepositoryFactory" type="Edu3.Data.SubsonicProvider.RepositoryFramework.RepositoryFactory, %ASSEMBLY_NAME%" factory-method="GetInstance">
    <property name="AddRepository" value="SsoUrlTemplateRepository" ></property>
    <property name="AddRepository" value="PackageSessionNodeRepository" ></property>
    <property name="AddRepository" value="PackageSessionNodeFinishedRepository" ></property>
  </object>

  <object id="SsoUrlTemplateRepository" type="SsoUrlTemplateRepository, %ASSEMBLY_NAME%" singleton="false"/>
  <object id="PackageSessionNodeRepository" type="PackageSessionNodeRepository, %ASSEMBLY_NAME%" singleton="false"/>
  <object id="PackageSessionNodeFinishedRepository" type="PackageSessionNodeFinishedRepository, %ASSEMBLY_NAME%" singleton="false"/>

</objects>

I have the same problem with initializing my application because there doesn't seam to exist a way to call static methods on a (static) class in spring.net. But you can try to make your class non-static try following:

public class RepositoryFactory
{
    private static RepositoryFactory instance;

    private static readonly Dictionary<string, object> Repositories =
        new Dictionary<string, object>();

    private RepositoryFactory()
    {
    }

    public static RepositoryFactory GetInstance()
    {
        if (instance == null)
        {
            instance = new RepositoryFactory();
        }
        return instance;
    }

    public static void AddRepository(object repository)
    {
        Repositories.Add(repository.GetType().Name, repository);
    }
}

with the spring-config:

<objects xmlns="http://www.springframework.net" >

  <object id="RepositoryFactory" type="Edu3.Data.SubsonicProvider.RepositoryFramework.RepositoryFactory, %ASSEMBLY_NAME%" factory-method="GetInstance">
    <property name="AddRepository" value="SsoUrlTemplateRepository" ></property>
    <property name="AddRepository" value="PackageSessionNodeRepository" ></property>
    <property name="AddRepository" value="PackageSessionNodeFinishedRepository" ></property>
  </object>

  <object id="SsoUrlTemplateRepository" type="SsoUrlTemplateRepository, %ASSEMBLY_NAME%" singleton="false"/>
  <object id="PackageSessionNodeRepository" type="PackageSessionNodeRepository, %ASSEMBLY_NAME%" singleton="false"/>
  <object id="PackageSessionNodeFinishedRepository" type="PackageSessionNodeFinishedRepository, %ASSEMBLY_NAME%" singleton="false"/>

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