Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not expose DefaultLifecycle from IContainer. #716 #717

Merged
merged 1 commit into from
Apr 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions Source/Bifrost.Autofac/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
namespace Bifrost.Autofac
{
/// <summary>
/// Represents an implementation of <see cref="IContainer"/> for AutoFac
/// Represents an implementation of <see cref="Execution.IContainer"/> for AutoFac
/// </summary>
public class Container : IContainer
{
global::Autofac.IContainer _container;
readonly global::Autofac.IContainer _container;

/// <summary>
/// Initializes a new instance of <see cref="Container"/>
Expand All @@ -30,8 +30,9 @@ public Container(global::Autofac.IContainer container)
_container = container;
}


#pragma warning disable 1591
public virtual BindingLifecycle DefaultLifecycle => BindingLifecycle.Transient;

public T Get<T>()
{
return _container.Resolve<T>();
Expand All @@ -45,7 +46,10 @@ public T Get<T>(bool optional)
}
catch
{
if (!optional) throw;
if (!optional)
{
throw;
}
}

return default(T);
Expand All @@ -60,11 +64,7 @@ public object Get(Type type)

public object Get(Type type, bool optional)
{
if (optional)
{
return _container.ResolveOptional(type);
}
return _container.Resolve(type);
return optional ? _container.ResolveOptional(type) : _container.Resolve(type);
}

public IEnumerable<T> GetAll<T>()
Expand All @@ -84,22 +84,20 @@ public bool HasBindingFor<T>()

public IEnumerable<object> GetAll(Type type)
{
List<object> list = ((IEnumerable) _container
.Resolve(typeof (IEnumerable<>)
.MakeGenericType(type)))
return ((IEnumerable)_container.Resolve(typeof(IEnumerable<>).MakeGenericType(type)))
.OfType<object>()
.ToList();

return list;
}

public IEnumerable<Type> GetBoundServices()
{
IEnumerable<Type> types = _container.ComponentRegistry.Registrations
.SelectMany(r => r.Services.OfType<IServiceWithType>(),
(r, s) => new {r, s})
.Select(rs => rs.r.Activator.LimitType).ToList();
return types;
return _container
.ComponentRegistry.Registrations
.SelectMany(
r => r.Services.OfType<IServiceWithType>(),
(r, s) => new { r, s })
.Select(rs => rs.r.Activator.LimitType)
.ToList();
}

public void Bind(Type type, Func<Type> resolveCallback)
Expand Down Expand Up @@ -174,8 +172,6 @@ public void Bind(Type service, Func<Type, object> resolveCallback, BindingLifecy
RegisterWithCallback(service, resolveCallback, DefaultLifecycle);
}

public BindingLifecycle DefaultLifecycle { get; set; }

#pragma warning restore 1591

void RegisterWithCallback<T>(Type service, Func<T> resolveCallback, BindingLifecycle lifecycle)
Expand Down Expand Up @@ -228,15 +224,21 @@ object ResolveUnregistered(Type type)
foreach (ParameterInfo parameter in parameters)
{
object service = _container.Resolve(parameter.ParameterType);
if (service == null) throw new Exception("Unkown service");
if (service == null)
{
throw new Exception("Unkown service");
}

parameterInstances.Add(service);
}

return Activator.CreateInstance(type, parameterInstances.ToArray());
}
catch (Exception)
{
}
}

throw new MissingDefaultConstructorException(type);
}
}
Expand Down
18 changes: 7 additions & 11 deletions Source/Bifrost.Ninject/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public Container(IKernel kernel)
_boundServices = new List<Type>();
}

public IKernel Kernel { get; private set; }
public virtual BindingLifecycle DefaultLifecycle => BindingLifecycle.Transient;

public IKernel Kernel { get; }

public T Get<T>()
{
Expand Down Expand Up @@ -91,14 +93,12 @@ public void Bind<T>(Func<Type> resolveCallback, BindingLifecycle lifecycle)

public void Bind<T>(Type type)
{
Kernel.Bind<T>().To(type);
_boundServices.Add(typeof(T));
Bind<T>(type, DefaultLifecycle);
}

public void Bind(Type service, Type type)
{
Kernel.Bind(service).To(type);
_boundServices.Add(service);
Bind(service, type, DefaultLifecycle);
}

public void Bind<T>(Type type, BindingLifecycle lifecycle)
Expand Down Expand Up @@ -128,14 +128,12 @@ public void Bind(Type service, object instance)

public void Bind<T>(Func<T> resolveCallback)
{
Kernel.Bind<T>().ToMethod(c => resolveCallback());
_boundServices.Add(typeof(T));
Bind(resolveCallback, DefaultLifecycle);
}

public void Bind(Type service, Func<Type, object> resolveCallback)
{
Kernel.Bind(service).ToMethod(c => resolveCallback(c.Request.Service));
_boundServices.Add(service);
Bind(service, resolveCallback, DefaultLifecycle);
}

public void Bind<T>(Func<T> resolveCallback, BindingLifecycle lifecycle)
Expand All @@ -149,7 +147,5 @@ public void Bind(Type service, Func<Type, object> resolveCallback, BindingLifecy
Kernel.Bind(service).ToMethod(c => resolveCallback(c.Request.Service)).WithLifecycle(lifecycle);
_boundServices.Add(service);
}

public BindingLifecycle DefaultLifecycle { get; set; }
}
}
40 changes: 19 additions & 21 deletions Source/Bifrost.SimpleInjector/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using Bifrost.Execution;
using SimpleInjector;
using SimpleInjector.Extensions;
using SimpleInjector.Extensions.Decorators;
using SimpleInjector.Advanced;
using SimpleInjector.Advanced.Internal;
using SimpleInjector.Diagnostics;
using SimpleInjector.Integration.Web;
using IContainer = Bifrost.Execution.IContainer;
using System;
using System.Collections.Generic;

namespace Bifrost.SimpleInjector
{
Expand All @@ -26,7 +17,8 @@ public Container(global::SimpleInjector.Container container)
{
_container = container;
}
public BindingLifecycle DefaultLifecycle { get; set; }

public virtual BindingLifecycle DefaultLifecycle => BindingLifecycle.Transient;

public T Get<T>()
{
Expand All @@ -41,7 +33,10 @@ public T Get<T>(bool optional)
}
catch
{
if (!optional) throw;
if (!optional)
{
throw;
}
}

return default(T);
Expand All @@ -52,15 +47,18 @@ public object Get(Type type)
return _container.GetInstance(type);
}

public object Get(Type type, bool optional = false)
public object Get(Type type, bool optional)
{
try
{
return _container.GetInstance(type);
}
catch
{
if (!optional) throw;
if (!optional)
{
throw;
}
}

return null;
Expand Down Expand Up @@ -95,12 +93,12 @@ public IEnumerable<Type> GetBoundServices()

public void Bind(Type service, Func<Type> resolveCallback)
{
_container.Register(service, resolveCallback);
_container.Register(service, resolveCallback, DefaultLifecycle);
}

public void Bind<T>(Func<Type> resolveCallback)
{
_container.Register(typeof(T), resolveCallback);
_container.Register(typeof(T), resolveCallback, DefaultLifecycle);
}

public void Bind(Type service, Func<Type> resolveCallback, BindingLifecycle lifecycle)
Expand All @@ -121,27 +119,27 @@ public void Bind<T>(Func<T> resolveCallback)

public void Bind(Type service, Func<Type, object> resolveCallback)
{
_container.Register(service, () => resolveCallback(service));
_container.Register(service, resolveCallback, DefaultLifecycle);
}

public void Bind<T>(Func<T> resolveCallback, BindingLifecycle lifecycle)
{
_container.Register<T>(resolveCallback, lifecycle);
_container.Register(resolveCallback, lifecycle);
}

public void Bind(Type service, Func<Type, object> resolveCallback, BindingLifecycle lifecycle)
{
throw new NotImplementedException();
_container.Register(service, resolveCallback, lifecycle);
}

public void Bind<T>(Type type)
{
_container.Register(typeof(T), () => type);
_container.Register(typeof(T), () => type, DefaultLifecycle);
}

public void Bind(Type service, Type type)
{
_container.Register(service, () => type);
_container.Register(service, () => type, DefaultLifecycle);
}

public void Bind<T>(Type type, BindingLifecycle lifecycle)
Expand Down
8 changes: 8 additions & 0 deletions Source/Bifrost.SimpleInjector/ContainerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,24 @@ public static void Register<T>(this global::SimpleInjector.Container container,
Func<Type> typeResolver = () => { return resolveCallback.Invoke().GetType(); };
container.Register(typeof(T), typeResolver, lifecycle);
}

public static void Register<T>(this global::SimpleInjector.Container container, Func<Type> resolveCallback, BindingLifecycle lifecycle)
{
container.Register(typeof(T), resolveCallback, lifecycle);
}

public static void Register(this global::SimpleInjector.Container container, Type service, Func<Type> resolveCallback, BindingLifecycle lifecycle)
{
var lifestyle = ResolveLifestyle(lifecycle);
container.Register(service, resolveCallback, lifestyle);
}

public static void Register(this global::SimpleInjector.Container container, Type service, Func<Type, object> resolveCallback, BindingLifecycle lifecycle)
{
var lifestyle = ResolveLifestyle(lifecycle);
container.Register(service, () => resolveCallback, lifestyle);
}

private static Lifestyle ResolveLifestyle(BindingLifecycle lifecycle)
{
var lifestyle = Lifestyle.Transient;
Expand Down
Loading