Skip to content

Commit

Permalink
Do not expose DefaultLifecycle from IContainer. #16
Browse files Browse the repository at this point in the history
  • Loading branch information
bnordli committed Mar 2, 2016
1 parent ef64316 commit 7393a03
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 242 deletions.
69 changes: 36 additions & 33 deletions Source/Bifrost.Autofac/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//

#endregion

using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -32,11 +30,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 @@ -47,8 +45,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 @@ -62,7 +61,10 @@ public T Get<T>(bool optional)
}
catch
{
if (!optional) throw;
if (!optional)
{
throw;
}
}

return default(T);
Expand All @@ -77,11 +79,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 @@ -101,9 +99,7 @@ public bool HasBindingFor<T>()

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

Expand All @@ -112,10 +108,12 @@ public IEnumerable<object> GetAll(Type type)

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();
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;
}

Expand All @@ -126,7 +124,7 @@ public void Bind(Type type, Func<Type> resolveCallback)

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


Expand All @@ -137,12 +135,12 @@ public void Bind(Type type, Func<Type> resolveCallback, BindingLifecycle lifecyc

public void Bind<T>(Func<Type> resolveCallback, BindingLifecycle lifecycle)
{
RegisterWithCallback(typeof (T), resolveCallback, lifecycle);
RegisterWithCallback(typeof(T), resolveCallback, lifecycle);
}

public void Bind<T>(Type type)
{
RegisterService(typeof (T), type, DefaultLifecycle);
RegisterService(typeof(T), type, DefaultLifecycle);
}

public void Bind(Type service, Type type)
Expand All @@ -152,7 +150,7 @@ public void Bind(Type service, Type type)

public void Bind<T>(Type type, BindingLifecycle lifecycle)
{
RegisterService(typeof (T), type, lifecycle);
RegisterService(typeof(T), type, lifecycle);
}

public void Bind(Type service, Type type, BindingLifecycle lifecycle)
Expand All @@ -162,7 +160,7 @@ public void Bind(Type service, Type type, BindingLifecycle lifecycle)

public void Bind<T>(T instance)
{
RegisterInstance(typeof (T), instance);
RegisterInstance(typeof(T), instance);
}

public void Bind(Type service, object instance)
Expand All @@ -173,26 +171,24 @@ public void Bind(Type service, object instance)

public void Bind<T>(Func<T> resolveCallback)
{
RegisterWithCallback(typeof (T), resolveCallback, DefaultLifecycle);
RegisterWithCallback(typeof(T), resolveCallback, DefaultLifecycle);
}

public void Bind(Type service, Func<Type,object> resolveCallback)
public void Bind(Type service, Func<Type, object> resolveCallback)
{
RegisterWithCallback(service, resolveCallback, DefaultLifecycle);
}

public void Bind<T>(Func<T> resolveCallback, BindingLifecycle lifecycle)
{
RegisterWithCallback(typeof (T), resolveCallback, DefaultLifecycle);
RegisterWithCallback(typeof(T), resolveCallback, DefaultLifecycle);
}

public void Bind(Type service, Func<Type, object> resolveCallback, BindingLifecycle lifecycle)
{
RegisterWithCallback(service, resolveCallback, DefaultLifecycle);
}

public BindingLifecycle DefaultLifecycle { get; set; }

#pragma warning restore 1591

void RegisterWithCallback<T>(Type service, Func<T> resolveCallback, BindingLifecycle lifecycle)
Expand All @@ -207,7 +203,8 @@ void RegisterWithCallback(Type type, Func<Type, object> resolveCallback, Binding

void RegisterService(Type service, Type type, BindingLifecycle lifecycle)
{
Update(x =>
Update(
x =>
{
if (type.IsGenericType)
{
Expand Down Expand Up @@ -240,21 +237,27 @@ object ResolveUnregistered(Type type)
{
try
{
ParameterInfo[] parameters = constructor.GetParameters();
var parameters = constructor.GetParameters();
var parameterInstances = new List<object>();
foreach (ParameterInfo parameter in parameters)
foreach (var 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);
}
}
}
}
18 changes: 7 additions & 11 deletions Source/Bifrost.Ninject/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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 @@ -105,14 +107,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 @@ -142,14 +142,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 @@ -163,7 +161,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; }
}
}
47 changes: 22 additions & 25 deletions Source/Bifrost.SimpleInjector/Container.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
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 @@ -22,7 +13,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 @@ -37,7 +29,10 @@ public T Get<T>(bool optional)
}
catch
{
if (!optional) throw;
if (!optional)
{
throw;
}
}

return default(T);
Expand All @@ -48,15 +43,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 @@ -85,18 +83,18 @@ public IEnumerable<object> GetAll(Type type)
public IEnumerable<Type> GetBoundServices()
{
return _container.GetCurrentRegistrations()
.Select(r => r.ServiceType)
.AsEnumerable();
.Select(r => r.ServiceType)
.AsEnumerable();
}

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 @@ -117,27 +115,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 All @@ -152,13 +150,12 @@ public void Bind(Type service, Type type, BindingLifecycle lifecycle)

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

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

}
}
5 changes: 5 additions & 0 deletions Source/Bifrost.SimpleInjector/ContainerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static void Register(this global::SimpleInjector.Container container, Typ
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)
{
Expand Down
Loading

0 comments on commit 7393a03

Please sign in to comment.