- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 SM 和泛型在我的存储库层中实现策略模式。为此,我有一个接口(interface) IPocoRepository,它有一个使用 Entity Framework 的具体实现。我已经设法在我的 Bootstrapper 文件中连接起来:
For(typeof(IPocoRepository<>)).Use(typeof(EntityFrameworkRepository<>));
当我尝试为此接口(interface)实现缓存时出现问题。在我的缓存类中,我想要一个基本存储库类的实例,这样我就可以保持我的设计干燥。让我概述一下这三个文件的外观:
public interface IPocoRepository<T>
{
IQueryable<T> GetAll();
...
public class EntityFrameworkRepository<T> : IPocoRepository<T> where T : class
{
public IQueryable<T> GetAll()
{
...
public class CachedRepository<T> : IPocoRepository<T> where T : class
{
private IPocoRepository<T> _pocoRepository;
public CachedRepository(IPocoRepository<T> pr)
{
_pocoRepository = pr;
}
public IQueryable<T> GetAll()
{
var all = (IQueryable<T>)CacheProvider.Get(_cacheKey);
if (!CacheProvider.IsSet(_cacheKey))
{
all = _pocoRepository.GetAll();
...
编辑:我希望 StructureMap 在请求 IPocoRepository 时返回 CachedRepository,除非在 CachedRepository 中请求 - 然后我希望它返回 EntityFrameworkRepository。
我知道在处理非泛型类时这很简单:
For<ICountyRepository>().Use<CachedCountyRepository>()
.Ctor<ICountyRepository>().Is<CountyRepository>();
我尝试搜索文档以了解如何执行此操作,但找不到任何内容。任何帮助将不胜感激!
最佳答案
好的,这并不难。您可以使用类型拦截器。鉴于您有以下类(class):
public interface IRepository<T>{}
public class Repository<T>:IRepository<T>{}
public class RepositoryCache<T> : IRepository<T>
{
private readonly IRepository<T> _internalRepo;
public RepositoryCache(IRepository<T> internalRepo)
{
_internalRepo = internalRepo;
}
public IRepository<T> InternalRepo
{
get { return _internalRepo; }
}
}
然后您需要创建一个类型拦截器。为此,您可以使用 StructureMap 提供的可配置“MatchedTypeInterceptor”。拦截器将需要查找您的存储库,然后找出泛型类型参数是什么。一旦它有了类型参数,它就可以声明它需要的缓存类型并对其进行初始化。作为初始化的一部分,它将在其构造函数中获取原始存储库。然后拦截器会将完成的缓存返回给从 ioc 上下文请求的任何内容。这是测试中的完整序列。
这可以移出到您的注册表中,我只是将它们放在一起作为一个最小示例。
[Test]
public void doTest()
{
MatchedTypeInterceptor interceptor = new MatchedTypeInterceptor(
x => x.FindFirstInterfaceThatCloses(typeof (IRepository<>)) != null);
interceptor.InterceptWith(original =>
{
Type closedType = original.GetType()
.FindFirstInterfaceThatCloses(typeof(IRepository<>));
var genericParameters = closedType.GetGenericArguments();
var closedCacheType = typeof(RepositoryCache<>)
.MakeGenericType(genericParameters);
return Activator.CreateInstance(closedCacheType, new[] {original});
});
ObjectFactory.Initialize(x =>
{
x.For(typeof (IRepository<>)).Use(typeof (Repository<>));
x.RegisterInterceptor(interceptor);
});
var test = ObjectFactory.GetInstance<IRepository<int>>();
Assert.That(test is RepositoryCache<int>);
}
关于c# - 使用泛型和 StructureMap 实现策略模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912564/
http://docs.structuremap.net/似乎有使用已弃用成员的非常古老的示例。 有什么地方可以找到最新的 StructureMap doco 吗? 最佳答案 我似乎在以下位置找到了一
我似乎无法弄清楚如何将对象定义为单例并为构造函数定义两个参数。 我可以做/或 .. 只是不能同时做。 例如。 (这不起作用)... ForRequestedType() .TheDefault
有没有办法确定在 StructureMap 中是否配置了特定类型? 如果在 StructureMap 中没有专门配置,我想返回一个泛型类型。 最佳答案 在 v2.6 你想要: IContainer.M
我想配置结构图以使用工厂类创建服务。工厂本身有一个需要填充的依赖项。目前我的注册表类中有以下内容: For().Singleton().Use(() => new DoStuffWebServ
我正在尝试使用 StructureMap 2.6.1 使用基于约定的注册一次注册我的所有存储库。见下面的代码: x.Scan(s => { s.TheCallingAssembly();
在 StructureMap 中你可以声明一个 Forward声明,这将允许注册单个具体实例,以由来自 StructureMap documentation 的多个接口(interface)解析: v
在 StructureMap 2 中,我有这样的事情: For().HybridHttpOrThreadLocalScoped().Use(); 使用 Structure Map 3 时,我应该使用以
如何将 StructureMap 与 OpenRasta 一起使用?我可以使用它来代替内部依赖解析器,还是只能将它与内置 DI 结合使用(即用于我自己的应用程序的依赖项)? 谢谢 最佳答案 Struc
我刚刚开始使用 StructureMap,之前使用过 Spring.Net。我喜欢 DefaultConventionScanner 以及扫描程序集和使用约定优于配置来查找类的能力。但是似乎有一个限制
我有一个通用接口(interface) public interface IDomainDataRepository { T[] GetAll(); } 有一个通用的实现 public cla
我正在尝试从 Structuremap 2.6.4.1 迁移到 3.1.4.143,但无法弄清楚如何处理 HybridHttpOrThreadLocalScoped。我能找到的所有 SO Q/A 似乎
所以我有一种情况,我想注册 n 个特定查找类型的映射。即: x.For().Add(); x.For().Add(); 我想让 SM 将它们的可枚举(或数组)注入(inject)到类的构造函数中: p
以下哪种语法被认为是最佳实践? For().LifecycleIs(new HybridLifecycle()).Use(); For().LifecycleIs(Lifecycles.GetLife
StructureMap 有没有办法用一行或约定来做这种重复映射? For>().Use(); For>().Use(); For>().Use(); For>().U
似乎 NLog 不能对 GetCurrentClassLogger() 使用反射,即使我的 MVC 3 应用程序部署在 中完全信任 IIS7 环境。我使用的是 StructureMap 2.6.1,问
StructureMap 定义了一个接口(interface) IBootStrapper,我看到很多人在他们的 Bootstrap 类中实现了这个接口(interface)。 但是我找不到任何可以说
我们将 Caliburn.Micro 用于我们的 MVVM 框架,将 StructureMap 用于我们的 IoC 容器,并将 MediatR 用于我们的调解器实现。这一切都工作正常,除了注册 Med
使用方法DecorateAllWith用 DynamicProxy 装饰所有实例都实现了一个接口(interface)? 例如: public class ApplicationServiceInte
在 autoFac 中,我可以注册一个接口(interface)的多个实现。当 autofac 实例化我的对象时,所有实例都传递给构造函数。 来自 autofac 的文档:here For examp
我有一个 Repository 的基类。在一个特定的项目中,我有几个这个基类的实现。例如 PersonRepository : Repository EmployerRepository : Repo
我是一名优秀的程序员,十分优秀!