1

我有一个缓存的存储库

public interface IRepository
{
    void LogWebUsage(string html);
    IEnumerable<ApiKey> GetApiKeys();
    ApiKey GetApiKey(Guid key);
}

public class Repository : IRepository
{
    private static readonly ILog Log = LogManager.GetLogger("API.Repository");

    public IDbConnectionFactory DbFactory { get; set; }

    public void LogWebUsage(string request)
    {
        Log.Debug(request);
    }

    public virtual IEnumerable<ApiKey> GetApiKeys()
    {
        List<ApiKey> result = null;
        using (var db = DbFactory.OpenDbConnection())
        {
            result = db.SelectParam<ApiKey>(q => q.Active);
        }

        return result;
    }

    public ApiKey GetApiKey(Guid key)
    {
        ApiKey result = null;
        using (var db = DbFactory.OpenDbConnection())
        {
            result = (db.SelectParam<ApiKey>(q => q.Id == key)).FirstOrDefault();
        }

        return result;
    }
}

public class CachedRepository : Repository
{
    public ICacheClient Cache { get; set; }

    public override IEnumerable<ApiKey> GetApiKeys()
    {
        const string cacheKey = "GetApiKeys";

        var result = Cache.Get<IEnumerable<ApiKey>>(cacheKey);

        if (result == null)
        {
            result = base.GetApiKeys();

            if (result.Any())
            {
                Cache.Add(cacheKey, result, TimeSpan.FromMinutes(30));
            }
        }
        return result;
    }
}

我像这样配置它。

//Register any dependencies you want injected into your services
container.Register<IDbConnectionFactory>(new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("DBConnstr"), true, SqlServerOrmLiteDialectProvider.Instance));
container.Register<ICacheClient>(new MemoryCacheClient());
container.Register<IRepository>(new CachedRepository());
container.RegisterAutoWired<CachedRepository>();

所以我希望的是 IDbConnectionFactory 和 ICacheClient 都将在运行时注入,但它们是空的。你如何正确解释这种类型的依赖图?

谢谢你,斯蒂芬

更新

谷歌搜索了几个小时后,我终于找到了一个可行的解决方案。通过配置进行构造函数注入。

public class CachedRepository : Repository
{
    private ICacheClient Cache { get; set; }

    public CachedRepository(IDbConnectionFactory dbFactory, ICacheClient cache) : base(dbFactory)
    {
        Cache = cache;
    }

    public override IEnumerable<ApiKey> GetApiKeys()
    {
        const string cacheKey = "GetApiKeys";

        var result = Cache.Get<IEnumerable<ApiKey>>(cacheKey);

        if (result == null)
        {
            result = base.GetApiKeys();

            if (result.Any())
            {
                Cache.Add(cacheKey, result, TimeSpan.FromMinutes(30));
            }
        }
        return result;
    }
}

配置

//Register any dependencies you want injected into your services
container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("DBConnstr"), true, SqlServerOrmLiteDialectProvider.Instance));        
container.Register<ICacheClient>(c => new MemoryCacheClient());
container.Register<IRepository>(c => new CachedRepository(c.Resolve<IDbConnectionFactory>(), c.Resolve<ICacheClient>()));

它有效,但我仍然想知道如何连接属性注入。

保重,斯蒂芬……再一次

4

1 回答 1

4

ServiceStack 的 Funq IOC 中用于 AutoWiring 的 API在这里:

使用通用 API:

container.RegisterAutoWired<MyType>();
container.RegisterAutoWiredAs<MyType,IMyType>();

使用运行时类型的 API:

container.RegisterAutoWiredType(typeof(MyType));
container.RegisterAutoWiredType(typeof(MyType),typeof(IMyType));
container.RegisterAutoWiredTypes(typeof(MyType),typeof(MyType2),typeof(MyType3));

因此,基本上您可以使用上述任何 API 来自动连接您的依赖项,例如:

container.Register<IDbConnectionFactory>(c => new 
  OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("DBConnstr"), true, 
  SqlServerDialect.Provider));        

container.Register<ICacheClient>(c => new MemoryCacheClient());
container.RegisterAutoWiredAs<CachedRepository,IRepository>(); //auto-wired
于 2013-04-10T12:28:36.320 回答