Beispiel #1
0
        private void InjectDepedencyProperties(object instance)
        {
#if NET451
            PropertyInfo[] properties = objectType.GetProperties(BindingFlags.SetProperty | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
#elif DOTNET5_4
            PropertyInfo[] properties = objectType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
#endif
                                        .Where(p => p.GetCustomAttributes(false).Any(s => s.GetType() == typeof(DepedencyAtrribute)))
                                        .ToArray();
            foreach (PropertyInfo p in properties)
            {
#if NET451
                DepedencyAtrribute attr = (DepedencyAtrribute)p.GetCustomAttributes(typeof(DepedencyAtrribute), false)[0];
#elif DOTNET5_4
                DepedencyAtrribute attr = p.GetCustomAttributes <DepedencyAtrribute>().First();
#endif
                var v = this.GetService(attr.ServiceType, attr.ServiceName, attr.ServiceVersion);
                if (v == null)
                {
                    throw new KeyNotFoundException(
                              String.Format(
                                  "The sepecified depedency service with a key '{0}' could not be found. Type: {1}",
                                  ServiceKey.Create(attr.ServiceType, attr.ServiceName, attr.ServiceVersion), ObjectType.AssemblyQualifiedName));
                }
                p.SetValue(instance, v, null);
            }
        }
        /// <summary>
        /// Applys a service object type that used to create service objects.
        /// </summary>
        /// <param name="objectType">A <c>System.Type</c> that specifies the service object type to create service object.</param>
        /// <param name="parameters">A <c>IDictionary&lt;String, String^gt;</c> that specifies a dictionary of the dependency constructor of the service object type.</param>
        /// <param name="properties">A <c>IDictionary&lt;String, String^gt;</c> specifies a dictionary to set properties of the service object.</param>
        /// <param name="name">An <c>System.String</c> that specifies the name of service object to get.</param>
        /// <param name="version">An <c>System.String</c> that specifies the version of service object to get.</param>
        /// <returns>An IoC container that implements <c>Alioth.Framework.IAliothServiceContainer</c>.</returns>
        public IAliothServiceContainer Apply(Type objectType, IDictionary <String, String> parameters, IDictionary <String, String> properties, string name, string version)
        {
            #region precondition
            if (objectType == null)
            {
                throw new ArgumentNullException("objectType");
            }
#if NET451
            if (!objectType.IsClass)
            {
#elif DOTNET5_4
            if (!objectType.GetTypeInfo().IsClass)
            {
#endif
                throw new ArgumentOutOfRangeException("objectType", "The specified object type should be a concrete class.");
            }
            #endregion
#if NET451
            ServiceTypeAtrribute[] attributes = (ServiceTypeAtrribute[])objectType.GetCustomAttributes(typeof(ServiceTypeAtrribute), false);
#elif DOTNET5_4
            ServiceTypeAtrribute[] attributes = objectType.GetTypeInfo().GetCustomAttributes <ServiceTypeAtrribute>(false).ToArray();
#endif
            if (attributes.Length == 0)
            {
                throw new ArgumentException(String.Format("{0} should be to anotate with {1}", objectType.Name, attributes));
            }
            IObjectBuilder builder = GetBuilder(objectType, parameters, properties, attributes);
            foreach (ServiceTypeAtrribute attribute in attributes)
            {
                ServiceKey key = ServiceKey.Create(attribute.ServiceType, name, version);
                AddBuilder(key, builder);
            }
            return(this);
        }
 private void AddBuilder(ServiceKey key, IObjectBuilder builder)
 {
     if (!builderContainer.TryAdd(key, builder))
     {
         throw new ArgumentException(String.Format("An element with the same key:\"{0}\", already exists in the AliothServiceContainer:\"{1}\"", key, Description));
     }
 }
        /// <summary>
        /// Applys a singleton service object.
        /// </summary>
        /// <typeparam name="T">The service type of the service object.</typeparam>
        /// <typeparam name="O">The type of the service object.</typeparam>
        /// <param name="instance">The service object.</param>
        /// <param name="name">An <c>System.String</c> that specifies the name of service object to get.</param>
        /// <param name="version">An <c>System.String</c> that specifies the version of service object to get.</param>
        /// <returns>An IoC container that implements <c>Alioth.Framework.IAliothServiceContainer</c>.</returns>
        public IAliothServiceContainer Apply <T, O>(O instance, string name, string version) where O : T
        {
            var key     = new ServiceKey(typeof(T), name, version);
            var builder = SingletonBuilder.Create(instance); //TODO create object build with IoC container.

            AddBuilder(key, builder);
            return(this);
        }
Beispiel #5
0
 /// <summary>
 /// Determines whether the current <c>Alioth.Framework.ServiceKey</c> object represents the same service key as a specified <c>Alioth.Framework.ServiceKey</c> object.
 /// </summary>
 /// <param name="obj">An object to compare to the current <c>Alioth.Framework.ServiceKey</c> object.</param>
 /// <returns>true if both <c>Alioth.Framework.ServiceKey</c> objects have the same <c>Alioth.Framework.ServiceKey</c> value; otherwise, false.</returns>
 public override Boolean Equals(object obj)
 {
     if (Object.ReferenceEquals(obj, null))
     {
         return(false);
     }
     if (obj.GetType() == this.GetType())
     {
         ServiceKey key = (ServiceKey)obj;
         return(this.type.Equals(key.type) &&
                this.name.Equals(key.name, StringComparison.OrdinalIgnoreCase) &&
                this.version.Equals(key.version, StringComparison.OrdinalIgnoreCase));
     }
     return(false);
 }
        /// <summary>
        /// Gets the service object of the specified type.
        /// </summary>
        /// <param name="serviceType">An <c>System.Type</c> that specifies the type of service object to get.</param>
        /// <param name="name">An <c>System.String</c> that specifies the name of service object to get.</param>
        /// <param name="version">An <c>System.String</c> that specifies the version of service object to get.</param>
        /// <returns>A service object of type serviceType.-or- null if there is no service object of type serviceType.</returns>
        public object GetService(Type serviceType, string name, string version)
        {
            Object obj = null;

            var            key = ServiceKey.Create(serviceType, name, version);
            IObjectBuilder builder;

            if (builderContainer.TryGetValue(key, out builder))
            {
                obj = builder.Build();
            }
            else if (parent != null)
            {
                obj = parent.GetService(serviceType);
            }
            return(obj);
        }