使用 spring.NET 配置静态属性
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将字典公开为公共属性并删除字段的 readonly 修饰符,并且不要使类静态。然后使用这个配置:
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:
我在初始化应用程序时遇到了同样的问题,因为在 spring.net 中不存在调用(静态)类上的静态方法的方法。但是您可以尝试使您的类成为非静态尝试以下操作:
使用 spring-config:
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:
with the spring-config: