我刚从Ninject更改为TinyIoC以进行依赖注入,而我在构造函数注入方面遇到了麻烦.
我已设法将其简化为此代码段:
public interface IBar { }
public class Foo
{
public Foo(IBar bar) { }
}
public class Bar : IBar
{
public Bar(string value) { }
}
class Program
{
static void Main(string[] args)
{
var container = TinyIoCContainer.Current;
string value = "test";
container.Register<IBar, Bar>().UsingConstructor(() => new Bar(value));
var foo = container.Resolve<Foo>();
Console.WriteLine(foo.GetType());
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致抛出TinyIoCResolutionException:
"Unable to resolve type: TinyIoCTestApp.Foo"
Run Code Online (Sandbox Code Playgroud)
在该异常内部是一系列内部异常:
"Unable to resolve type: TinyIoCTestApp.Bar"
"Unable to resolve type: System.String"
"Unable to resolve type: System.Char[]"
"Value …Run Code Online (Sandbox Code Playgroud) 我正在使用Nancy和TinyIoC来解决依赖关系.
特别需要一个依赖项是应用程序生命周期单例.
如果我使用默认构造函数,它可以工作:
container.Register<IFoo, Foo>().AsSingleton(); // WORKS
Run Code Online (Sandbox Code Playgroud)
但是如果我在构造函数上使用一些参数来尝试它,它不会:
container.Register<IFoo>((c, e) => new Foo("value", c.Resolve<ILogger>())).AsSingleton();
// FAILS with error "Cannot convert current registration of Nancy.TinyIoc.TinyIoCContainer+DelegateFactory to singleton"
Run Code Online (Sandbox Code Playgroud)
没有.AsSingleton(),它再次起作用,但我没有得到一个单身人士:
container.Register<IFoo>((c, e) => new Foo("value", c.Resolve<ILogger>()));
// Works, but Foo is not singleton
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我认为错误应该是显而易见的,但我找不到它.我用完了所有google-foo.
代码在这里运行:
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);
// here
}
}
Run Code Online (Sandbox Code Playgroud) 所以我使用Nancy + TinyIoC来运行一个小型的Web服务.这有效.现在我需要创建一个需要一些相同依赖关系的Quartz作业,理想情况下我想使用Nancy的TinyIoC注入这些,如Quartz Tutorial中所述.
我已经找到了一个使用Windsor的例子,他们直接访问了IoC Container,但是根据这里提出的类似问题,南希显然是粗暴的,不必要的.
那么我的问题就是,这样做的正确方法是什么?我的JobFactory的代码如下所示:
public class MyJobFactory : IJobFactory
{
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
return (IJob) TinyIoCContainer.Current.Resolve(bundle.JobDetail.JobType);
}
}
Run Code Online (Sandbox Code Playgroud)
但是这不会返回正确注入实例的作业,而是返回具有新依赖项实例的作业.(这应该是Singletons,这让我相信TinyIoCContainer.Current返回的TinyIoCContainer与Nancy使用的容器不同).
更新
我通过Nancy Bootstrapper设置IoC容器:
public class MyBootStrapper : DefaultNancyBootstrapper
{
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
var push = new PushService();
// object initialization and Event Registration snipped
container.Register(cp);
}
}
Run Code Online (Sandbox Code Playgroud) 我得到TinyIOC无法解决类型异常.我使用Xamarin studio for ipad开发了我的应用程序.在模拟器中它工作正常.但在部署ipad时,我遇到了错误.
TinyIoC.TinyIoCResolutionException:无法解析类型:IOSLoginViewModel ---> System.Exception:无法解析类型:IOSLoginViewModel ---> System.Exception:无法解析类型:
private IEnumerable<ConstructorInfo> GetTypeConstructors(Type type)
{
//#if NETFX_CORE
// return type.GetTypeInfo().DeclaredConstructors.OrderByDescending(ctor => ctor.GetParameters().Count());
//#else
return type.GetConstructors().OrderByDescending(ctor => ctor.GetParameters().Count());
//#endif
}
Run Code Online (Sandbox Code Playgroud)
上面的方法是在模拟器上返回值,但对于ipad,它给出null.
我用过的代码行是
var tContainer = TinyIoC.TinyIoCContainer.Current;
tContainer.Register<IOnlineAuthenticationService, Framework.OnlineProvider.AuthenticationService>().AsMultiInstance();
DataContext = TinyIoC.TinyIoCContainer.Current.Resolve<IOSLoginViewModel>();
Run Code Online (Sandbox Code Playgroud)
IOS构建选项我已经尝试了所有,链接 - SDK,ALL,No但仍然是相同的错误.
我是依赖注入模式的新手,我在从容器中获取类的新实例时遇到问题.在tinyioc中解析它只是继续返回相同的实例而不是新实例.现在为代码
public abstract class HObjectBase : Object
{
private string _name = String.Empty;
public string Name
{
get
{
return this._name;
}
set
{
if (this._name == string.Empty && value.Length > 0 && value != String.Empty)
this._name = value;
else if (value.Length < 1 && value == String.Empty)
throw new FieldAccessException("Objects names cannot be blank");
else
throw new FieldAccessException("Once the internal name of an object has been set it cannot be changed");
}
}
private Guid _id = new Guid(); …Run Code Online (Sandbox Code Playgroud)