XAML markup binding to dictionary with key of type Type
Tag : .net , By : Arun Thomas
Date : March 29 2020, 07:55 AM
will be helpful for those in need If the indexer has a specific type the conversion should be done automatically so this should work: {Binding theDictionary[ns:OnePrettyType]}
{Binding theDictionary[(sys:Type)ns:OnePrettyType]}
{Binding Path=theDictionary[(sys:Type)ns:OnePrettyType]}
[ContentProperty("Parameters")]
public class PathConstructor : MarkupExtension
{
public string Path { get; set; }
public IList Parameters { get; set; }
public PathConstructor()
{
Parameters = new List<object>();
}
public PathConstructor(string path, object p0)
{
Path = path;
Parameters = new[] { p0 };
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new PropertyPath(Path, Parameters.Cast<object>().ToArray());
}
}
<Binding>
<Binding.Path>
<me:PathConstructor Path="theDictionary[(0)]">
<x:Type TypeName="ns:OnePrettyType" />
</me:PathConstructor>
</Binding.Path>
</Binding>
{Binding Path={me:PathConstructor theDictionary[(0)], {x:Type ns:OnePrettyType}}}
|
Ninject conditional binding based on parameter type
Tag : chash , By : turret
Date : March 29 2020, 07:55 AM
I hope this helps you . I'm using a factory to return a datasender: , After some looking into Ninject source I have found following: public class DummyContext : IContext
{
public IKernel Kernel { get; private set; }
public IRequest Request { get; private set; }
public IBinding Binding { get; private set; }
public IPlan Plan { get; set; }
public ICollection<IParameter> Parameters { get; private set; }
public Type[] GenericArguments { get; private set; }
public bool HasInferredGenericArguments { get; private set; }
public IProvider GetProvider() { return null; }
public object GetScope() { return null; }
public object Resolve() { return null; }
}
kernel.Bind<IDataSender>()
.To<RemotingDataSender>()
.When( a => a.Parameters
.Single( b => b.Name == "connection" )
.GetValue( new DummyContext(), a.Target )
as RemotingConnection != null );
|
How to check if there is Ninject binding exists by type?
Tag : chash , By : ugufugu
Date : March 29 2020, 07:55 AM
will be helpful for those in need In case of auto-injecting the dependencies, I realized that the object creation is not that bad since it will be consumed by a controller or a service anyway.
|
Binding multiple versions of the same type with Ninject
Tag : chash , By : Brian Cupps
Date : March 29 2020, 07:55 AM
With these it helps It's perfectly fine to create two bindings for the same type, which differ only in parameters. So what you've got to do is: Bind<IFoo>().To<Class1>().WithConstructorArgument("boolParameterName", true);
Bind<IFoo>().To<Class1>().WithConstructorArgument("boolParameterName", false);
public interface IBar { }
public class Bar : IBar { }
public interface IFoo { }
class Foo1 : IFoo
{
public Foo1(IBar bar) { }
}
class Foo2 : IFoo
{
public Foo2(IBar bar, bool theParametersName) { }
}
[Fact]
public void FactMethodName()
{
var kernel = new StandardKernel();
kernel.Bind<IBar>().To<Bar>();
kernel.Bind<IFoo>().To<Foo1>();
kernel.Bind<IFoo>().To<Foo2>().WithConstructorArgument("theParametersName", true);
kernel.Bind<IFoo>().To<Foo2>().WithConstructorArgument("theParametersName", false);
List<IFoo> foos = kernel.GetAll<IFoo>().ToList();
foos.Should().HaveCount(3);
}
|
Ninject generic type xml binding
Tag : chash , By : Ben Brown
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You want to bind open generic types, so this type definition should do the trick: <bind service="Base.IJsonProvider`1, Base" to="Base.JsonProvider`1, Base"
name ="Config"/>
|