public static BindingBuilder <T> InSingletonScope <T>(this BindingBuilder <T> builder) { builder.CheckBound(true); var binding = new SingletonBinding(builder.Binding); return(builder.SetBinding(binding.Get)); }
public IBindingContext CreateContext() { var context = BindingContext.Create(); foreach (var binding in m_bindings) { List <IBindingRequirement> requirements = new List <IBindingRequirement>(); foreach (var req in binding.Dependencies) { var realReq = BindingRequirements.Instance.With(req.name, req.BindingType); requirements.Add(realReq); } var args = new List <Type>(binding.Factory.GetParameters().Select(p => p.ParameterType)); args.Add(binding.Factory.ReturnType); var delegateType = Expression.GetFuncType(args.ToArray()); var factory = System.Delegate.CreateDelegate(delegateType, binding.Factory); IBinding realBinding = new Binding(factory, requirements.ToArray()); if (binding.Singleton) { realBinding = new SingletonBinding(realBinding); } AddToContext(context, binding.Root, realBinding, binding.Subcontexts); } return(context); }
public void BindingAsSingletonRetunsAlwaysTheSame() { var ret = 0; Func <int> func = () => ret++; var singletonBinding = new SingletonBinding(new Binding(func)); var mock = new Moq.Mock <IBindingContext>(); var value = (int)singletonBinding.Get(mock.Object); var value2 = (int)singletonBinding.Get(mock.Object); Assert.AreEqual(value, value2); }