假设我有一个通用接口和一个通用实现.我如何注册所有用法?
具体来说,我有以下(简化为简化):
public interface IRepository<T> where T : TableEntity
{
T GetById(string partitionKey, string rowKey);
void Insert(T entity);
void Update(T entity);
void Update(string partitionKey, string rowKey, Action<T> updateAction);
void Delete(T entity);
IQueryable<T> Table { get; }
}
public class AzureRepository<T> : IRepository<T> where T : TableEntity
{
...
}
Run Code Online (Sandbox Code Playgroud)
我是否需要逐个注册所有实现,如下所示:
container.Register<IRepository<Entity1>, AzureRepository<Entity1>>();
container.Register<IRepository<Entity2>, AzureRepository<Entity2>>();
container.Register<IRepository<Entity3>, AzureRepository<Entity3>>();
...
Run Code Online (Sandbox Code Playgroud)
或者有更短的方式?
Autofac允许使用.AsImplementedInterfaces()或链接非常容易地将多个接口解析到同一个实例.As <>()与.SingleInstance()一起调用.这也可以用TinyIoC完成吗?我只发现了如何注册同一个接口的多个实现,但没有办法链接注册等.
根据我的理解,这是IoC容器的一个非常重要的功能,不是吗?
关于在TinyIoc中注册其他依赖项以便在NancyFX中使用,我还有另一个新手问题.
运行应用程序时,我将继续获得以下异常...
Unable to resolve type: AdvancedSearchService.Interfaces.IResponseFactory
Exception Details: TinyIoC.TinyIoCResolutionException: Unable to resolve type: AdvancedSearchService.Interfaces.IResponseFactory
Source Error:
Line 25: var container = TinyIoCContainer.Current;
Line 26:
Line 27: _responseFactory = container.Resolve<IResponseFactory>();
Line 28:
Line 29:
Run Code Online (Sandbox Code Playgroud)
我目前正在错误地注册我的依赖项,但我似乎无法弄清楚正确的方法.下面是我的自定义引导程序中的代码.另请注意,我目前没有调用base.ConfigureRequestContainer方法,因为我似乎无法弄清楚如何将当前上下文传递给它.
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
container.Register<IRavenSessionManager>(new RavenSessionManager());
base.ConfigureApplicationContainer(container);
ConfigureRequestContainer(container);
}
protected void ConfigureRequestContainer(TinyIoCContainer applicationContainer)
{
var requestContainer = applicationContainer.GetChildContainer();
requestContainer.Register<ISearchRepository>(new SearchRepository(requestContainer.Resolve<IRavenSessionManager>().GetSession()));
requestContainer.Register<IResponseFactory>(new ResponseFactory(requestContainer.Resolve<ISearchRepository>()));
//base.ConfigureRequestContainer(requestContainer,[I NEED THE CONTEXT])
}
Run Code Online (Sandbox Code Playgroud)
真的很感激任何帮助......显然我的无知没有限制:)
我刚刚开始学习IoC和依赖注入.我打算做一个MonoTouch项目,想要使用TinyIoC,但我想先测试一下.我正在创建一个虚拟信用卡处理控制台应用程序,我遇到了如何配置TinyIoC的问题,因为我有多个接口实现.这是我的测试应用.
界面:
public interface IPaymentProcessor
{
void ProcessPayment(string cardNumber);
}
Run Code Online (Sandbox Code Playgroud)
界面的两个实现:
VisaPaymentProcessor
public class VisaPaymentProcessor : IPaymentProcessor
{
public void ProcessPayment(string cardNumber)
{
if (cardNumber.Length != 13 && cardNumber.Length != 16)
{
new ArgumentException("Card Number isn't the correct length");
}
// some code for processing payment
}
}
Run Code Online (Sandbox Code Playgroud)
AmexPaymentProcessor
public class AmexPaymentProcessor : IPaymentProcessor
{
public void ProcessPayment(string cardNumber)
{
if (cardNumber.Length != 15)
{
new ArgumentException("Card Number isn't the correct length");
}
// some code for processing the …Run Code Online (Sandbox Code Playgroud) 我正在建立一个小型的Nancy网络项目.
在我的一个类(不是南希模块)的方法中,我想基本上做:
var myThing = TinyIoC.TinyIoCContainer.Current.Resolve<IMyThing>();
Run Code Online (Sandbox Code Playgroud)
但是,.Current(非公共成员,_RegisteredTypes)只有一个注册 :
TinyIoC.TinyIoCContainer.TypeRegistration
当然,在上面的代码中,我得到:
无法解析类型:My.Namespace.IMyThing
所以,我想我没有在我的引导程序中注册相同的容器?
有没有办法搞定它?
编辑
为了充实我正在尝试做的事情:
基本上,我的网址结构看起来像:
/ {的myType}/{myMethod的}
所以,想法是:/ customer/ShowAllWithTheNameAlex将加载Customer服务,并执行showAllWithTheNameAlex方法
我这样做是:
public interface IService
{
void DoSomething();
IEnumerable<string> GetSomeThings();
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个抽象基类,使用返回服务的方法GetService.
在这里,我正在尝试使用TinyIoC.TinyIoCContainer.Current.Resolve();
在这种情况下,它将是TinyIoC.TinyIoCContainer.Current.Resolve("typeName");
public abstract class Service : IService
{
abstract void DoSomething();
abstract IEnumerable<string> GetSomeThings();
public static IService GetService(string type)
{
//currently, i'm doing this with reflection....
}
}
Run Code Online (Sandbox Code Playgroud)
这是我对服务的实现.
public class CustomerService : Service
{
public void DoSomething()
{
//do stuff
} …Run Code Online (Sandbox Code Playgroud) 我是南希的菜鸟.我一直在使用它作为生成REST API的框架.我熟悉Json.NET所以我一直在玩这个Nancy.Serialization.JsonNet包.
我的目标:自定义行为(即更改设置)的JsonNetSerializer和JsonNetBodyDeserializer.
具体来说,我想加入以下设置......
var settings = new JsonSerializerSettings { Formatting = Formatting.Indented };
settings.Converters.Add( new StringEnumConverter { AllowIntegerValues = false, CamelCaseText = true } );
Run Code Online (Sandbox Code Playgroud)
我想使用内置的TinyIoC容器来执行此自定义,以避免继承链并限制Nancy.Serialization.JsonNet包中任何更改引起的潜在问题.
注意:作为临时解决方法,我利用继承来创建CustomJsonNetSerializer和CustomJsonNetBodyDeserializer.
我已经尝试了几种方法来合并这种配置至少为JsonNetSerializer.我还没有尝试配置JsonNetBodyDeserializer使用TinyIoC.我想它会以同样的方式完成.我尝试过的所有工作都在我的CustomNancyBootstrapper(继承自DefaultNancyBootstrapper).
到目前为止最成功的方法:覆盖 ConfigureApplicationContainer
protected override void ConfigureApplicationContainer( TinyIoCContainer container )
{
base.ConfigureApplicationContainer( container );
// probably don't need both registrations, and I've tried only keeping one or the other
var settings = …Run Code Online (Sandbox Code Playgroud) 设置:我有一个几乎开箱即用的Nancy + TinyIoC设置运行web服务工作正常.它取决于各种(AsSingleton)服务类.然而,这些不作为单身人员注入,每次都会创建一个新实例.
我按如下方式设置了Nancy bootstrapper:
class MyBootStrapper : DefaultNancyBootstrapper
{
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
var cp = new CertificateProvider();
container.Register(cp).AsSingleton();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试让 TinyIoC 在 Xamarin.iOS 上运行,但运气不太好。我的项目链接器设置设置为“仅链接 SDK 程序集”。
我实际上正在做这么简单的事情:
public interface IPerson { int age { get; } }
public class Person : IPerson { public int age { get { return 99; } } }
Run Code Online (Sandbox Code Playgroud)
然后我的注册代码如下所示(我刚刚将其放在玩具应用程序的 AppDelegate 中):
TinyIoCContainer.Current.Register<IPerson,Person>.AsMultiInstance();
Run Code Online (Sandbox Code Playgroud)
当我尝试获取 IPerson 时,出现运行时异常,指出 IPerson 无法解析(此代码可在玩具应用程序的 AppDelegate 中的注册代码之后立即找到):
IPerson person = TinyIoCContainer.Current.Resolve<IPerson>();
Run Code Online (Sandbox Code Playgroud)
这是错误:
Unable to resolve type: TinyTest.IPerson
Run Code Online (Sandbox Code Playgroud)
但是,如果我将链接器设置更改为“不链接”,则一切正常。但这显然是站不住脚的,因为二进制文件变得非常巨大。
我尝试将 [Preserve] 属性放在 IPerson 接口和 Person 类上,但没有成功。我还尝试手动声明 IPerson 类型的变量并使用 new Person() 实例化它,然后获取 Age 属性,只是为了确保该类型包含在构建中,但也没有运气。
感觉我在这里错过了一些东西 - 有人能指出我正确的方向吗?
谢谢你!
我一直在尝试使用 JWT 在 dotnet core 上使用 Nancy 来获得无状态身份验证。我在 Ubuntu 虚拟机上运行它。cannot resolve type但我在我的Startup.csat中不断遇到错误x.UseNancy();
我的启动类如下所示:
public class Startup
{
public static IConfigurationRoot Configuration;
public Startup(IHostingEnvironment env)
{
Configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
}
public void Configure(IApplicationBuilder app)
{
app.UseOwin(x =>
{
x.RequiresStatelessAuth(
new MySecureTokenValidator(new ConfigProvider(Configuration)),
new StatelessAuthOptions() { IgnorePaths = new List<string>(new[] { "/login", "/content" }) });
x.UseNancy();
});
}
}
Run Code Online (Sandbox Code Playgroud)
我的 ConfigProvider 类:
public class ConfigProvider : IConfigProvider
{
private readonly IConfigurationRoot config; …Run Code Online (Sandbox Code Playgroud) 介绍
我们正试图捕获潜在的内存泄漏BenchmarksDotNet.
为了简单的例子,这里是一个简单的TestClass:
public class TestClass
{
private readonly string _eventName;
public TestClass(string eventName)
{
_eventName = eventName;
}
public void TestMethod() =>
Console.Write($@"{_eventName} ");
}
Run Code Online (Sandbox Code Playgroud)
我们正在通过NUnit测试实现基准测试netcoreapp2.0:
[TestFixture]
[MemoryDiagnoser]
public class TestBenchmarks
{
[Test]
public void RunTestBenchmarks() =>
BenchmarkRunner.Run<TestBenchmarks>(new BenchmarksConfig());
[Benchmark]
public void TestBenchmark1() =>
CreateTestClass("Test");
private void CreateTestClass(string eventName)
{
var testClass = new TestClass(eventName);
testClass.TestMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
测试输出包含以下摘要:
Method | Mean | Error | Allocated |
--------------- |-----:|------:|----------:|
TestBenchmark1 | NA | …Run Code Online (Sandbox Code Playgroud)