Пример #1
0
        public static void RegisterType <IT, TT>(this IIocBuilder builder, LifeTimeScope scope = LifeTimeScope.Transient, string name = null, bool autoWire = false,
                                                 Func <IIocContext, IDictionary <string, object> > getConstructorParameters = null, Func <IIocContext, TT, TT> onActivating = null, Action <IIocContext, TT> onActivated = null)
            where TT : class, IT
        {
            Func <IIocContext, object, object> onObjActivaing = null;
            Action <IIocContext, object>       onObjActivated = null;

            if (onActivating != null)
            {
                onObjActivaing = (ctx, obj) =>
                {
                    var nobj = onActivating(ctx, (TT)obj);
                    return(nobj);
                };
            }


            if (onActivated != null)
            {
                onObjActivated = (ctx, obj) =>
                {
                    onActivated(ctx, (TT)obj);
                };
            }

            builder.RegisterType(typeof(IT), typeof(TT), scope, name, autoWire, getConstructorParameters, onObjActivaing, onObjActivated);
        }
Пример #2
0
        public void RegisterInstance(Type interfaceType, object instance, LifeTimeScope scope = LifeTimeScope.Transient, string name = null, bool autoWire = false)
        {
            var rb = builder.RegisterInstance(instance).As(interfaceType);

            if (!string.IsNullOrEmpty(name))
            {
                rb.Named(name, interfaceType);
            }
            switch (scope)
            {
            case LifeTimeScope.Singleton:
                rb.SingleInstance();
                break;

            case LifeTimeScope.WorkUnit:
                rb.InstancePerMatchingLifetimeScope("workunit");
                break;

            case LifeTimeScope.Transient:
            default:
                rb.InstancePerDependency();
                break;
            }

            if (autoWire)
            {
                module.AddAutoWiredType(interfaceType);
            }
        }
Пример #3
0
        public void RegisterType(Type interfaceType, Type serviceType, LifeTimeScope scope = LifeTimeScope.Transient, string name = null, bool autoWire = false,
                                 Func <IIocContext, IDictionary <string, object> > getConstructorParameters = null, Func <IIocContext, object, object> onActivating = null, Action <IIocContext, object> onActivated = null)
        {
            var rb = builder.RegisterType(serviceType).As(interfaceType);

            if (!string.IsNullOrEmpty(name))
            {
                rb.Named(name, interfaceType);
            }
            switch (scope)
            {
            case LifeTimeScope.Singleton:
                rb.SingleInstance();
                break;

            case LifeTimeScope.WorkUnit:
                rb.InstancePerMatchingLifetimeScope("workunit");
                break;

            case LifeTimeScope.Transient:
            default:
                rb.InstancePerDependency();
                break;
            }
            if (getConstructorParameters != null)
            {
                rb.OnPreparing(pe =>
                {
                    var paras = getConstructorParameters(new AutofacContext(pe.Context));
                    if (paras != null)
                    {
                        pe.Parameters = paras.Select(kv => new NamedParameter(kv.Key, kv.Value));
                    }
                });
            }
            if (onActivating != null)
            {
                rb.OnActivating(ae =>
                {
                    var obj = onActivating(new AutofacContext(ae.Context), ae.Instance);
                    ae.ReplaceInstance(obj);
                });
            }
            if (onActivated != null)
            {
                rb.OnActivated(ae =>
                {
                    onActivated(new AutofacContext(ae.Context), ae.Instance);
                });
            }

            if (autoWire)
            {
                module.AddAutoWiredType(interfaceType);
            }
        }
Пример #4
0
        public void Test_A_Add_B_Should_Be_C(int parameter1, int parameter2, int expected)
        {
            //arrange
            var target = new LifeTimeScope();

            //act
            var actual = target.A_Add_B_Should_Be_C(parameter1, parameter2);

            //assert
            Assert.Equal(expected, actual);
        }
Пример #5
0
        public static void RegisterConfig(this IIocBuilder builder, XElement root)
        {
            var compEles = root.Element("components").Elements("component");

            foreach (var ele in compEles)
            {
                var           serviceType   = Type.GetType(ele.Attribute("type").Value);
                var           interfaceType = Type.GetType(ele.Attribute("service").Value);
                LifeTimeScope scope         = LifeTimeScope.Transient;
                if (ele.Attribute("scope") != null)
                {
                    switch (ele.Attribute("scope").Value.ToLower())
                    {
                    case "singleton":
                        scope = LifeTimeScope.Singleton;
                        break;

                    case "workunit":
                        scope = LifeTimeScope.WorkUnit;
                        break;

                    case "transiant":
                    default:
                        scope = LifeTimeScope.Transient;
                        break;
                    }
                }
                bool   autoWire = ele.AttributeValue <bool>("autowire", false);
                string name     = ele.AttributeValue("name", null);
                Func <IIocContext, IDictionary <string, object> > getConstructorParameters = null;
                if (ele.HasElements)
                {
                    var paraEles = ele.Element("parameters").Elements("parameter");
                    Dictionary <string, object> paras = new Dictionary <string, object>();
                    foreach (var pele in paraEles)
                    {
                        paras.Add(pele.Attribute("name").Value, pele.Attribute("value").Value);
                    }
                    getConstructorParameters = (ctx) => { return(paras); };
                }

                builder.RegisterType(interfaceType, serviceType, scope, name, autoWire, getConstructorParameters);
            }

            var moduleEles = root.Element("modules").Elements("module");

            foreach (var ele in moduleEles)
            {
                var        moduleType = Type.GetType(ele.Attribute("type").Value);
                IIocModule module     = Activator.CreateInstance(moduleType) as IIocModule;
                builder.RegisterModule(module);
            }
        }
Пример #6
0
        public void Test_A_Add_B_Should_Be_C_Fact()
        {
            //arrange
            var target     = new LifeTimeScope();
            var parameter1 = 1;
            var parameter2 = 2;
            var expected   = 3;

            //act
            var actual = target.A_Add_B_Should_Be_C(parameter1, parameter2);

            //assert
            Assert.Equal(expected, actual);
        }
Пример #7
0
        public void RegisterGeneric(Type interfaceType, Type serviceType, LifeTimeScope scope = LifeTimeScope.Transient)
        {
            var rb = builder.RegisterGeneric(serviceType).As(interfaceType);

            switch (scope)
            {
            case LifeTimeScope.Singleton:
                rb.SingleInstance();
                break;

            case LifeTimeScope.WorkUnit:
                rb.InstancePerMatchingLifetimeScope("workunit");
                break;

            case LifeTimeScope.Transient:
            default:
                rb.InstancePerDependency();
                break;
            }
        }
Пример #8
0
        public void RegisterFactory <IT, TT>(LifeTimeScope scope = LifeTimeScope.Transient)
            where TT : class, IServiceFactory <IT>
        {
            builder.RegisterType <TT>().As <IServiceFactory <IT> >().SingleInstance();

            var rb = builder.Register((c, p) =>
            {
                if (p.Count() <= 0)
                {
                    throw new Exception("cannot get the parameters from the constructor");
                }

                Type containerType = p.TypedAs <Type>();

                if (containerType.IsGenericType)
                {
                    containerType = containerType.GetGenericArguments()[0];
                }
                var fac = c.Resolve <IServiceFactory <IT> >();
                return(fac.Create(containerType, new AutofacContext(c)));
            }).As <IT>();

            module.AddTypeBasedServiceType(typeof(IT));
        }
Пример #9
0
 public static void RegisterInstance <IT, TT>(this IIocBuilder builder, TT instance, LifeTimeScope scope = LifeTimeScope.Transient, string name = null, bool autoWire = false)
 {
     builder.RegisterInstance(typeof(IT), instance, scope, name, autoWire);
 }
Пример #10
0
 public static void EndLifeTimeScope()
 {
     LifeTimeScope.Dispose();
 }
Пример #11
0
 public ServiceAttribute(string contractName, Type exportType, LifeTimeScope scope)
 {
     this.ContractName = contractName;
     this.ExportType   = exportType;
     this.Scope        = scope;
 }
Пример #12
0
 public ServiceAttribute(Type exportType, LifeTimeScope scope)
     : this((string)null, exportType, scope)
 {
 }
Пример #13
0
 public ServiceAttribute(string contractName, LifeTimeScope scope)
     : this(contractName, (Type)null, scope)
 {
 }
Пример #14
0
 public ServiceAttribute(LifeTimeScope scope)
     : this((string)null, (Type)null, scope)
 {
 }