/src/COFE3/app/COFE3.Core/Implements/PropertyBehaviorManager.cs
http://cofe.codeplex.com · C# · 235 lines · 178 code · 57 blank · 0 comment · 52 complexity · a9a1af58647a97cbb54bf4be8c45abc3 MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Composition;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Cofe.Core.Service;
- using Cofe.Core.Utils;
-
- namespace Cofe.Core.Property
- {
- public class PropertyBehaviorManager : IPropertyBehaviorManager
- {
- [Export(typeof(ICofeService))]
- [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
- public class Factory : IPropertyBehaviorManagerFactory
- {
-
- public IPropertyBehaviorManager ConstructBehavior(IPropertyHost propertyHost, IServiceLocater serviceLocater)
- {
- return new PropertyBehaviorManager(propertyHost, serviceLocater);
- }
- }
-
- #region Constructor
-
- private void init(IServiceLocater serviceLocater)
- {
- _propertyDictionary = serviceLocater.FindService<IPropertyDictionary>();
- }
-
- private PropertyBehaviorManager(IPropertyHost propertyHost, IServiceLocater serviceLocater)
- {
- _propertyHost = propertyHost;
- init(serviceLocater);
- }
-
- #endregion
-
- #region Methods
-
-
- private static Dictionary<PropertyRetrivalOptions, T> loadBehaviors<T>()
- where T : IPropertyProviderBehavior
- {
- var retVal = new Dictionary<PropertyRetrivalOptions, T>();
- var allListBehavior = CofeServices.ServiceLocater.FindAllServices<T>();
- foreach (var name in Enum.GetNames(typeof(PropertyRetrivalOptions)))
- {
- PropertyRetrivalOptions option;
- Enum.TryParse<PropertyRetrivalOptions>(name, out option);
- var behavior = allListBehavior.FirstOrDefault(b => b.SupportOption.HasFlag(option));
- retVal.Add(option, behavior);
- }
- return retVal;
- }
-
- private static void loadBehaviors()
- {
- if (_listBehavior == null) _listBehavior = loadBehaviors<IPropertyListSupportedBehavior>();
- if (_getBehavior == null) _getBehavior = loadBehaviors<IPropertyGetBehavior>();
- if (_setBehavior == null) _setBehavior = loadBehaviors<IPropertySetBehavior>();
- }
-
-
- public async Task<object[]> GetSupportedPropertyAsync(PropertyRetrivalOptions options = PropertyRetrivalOptions.NotApplied,
- MatchMode matchMode = MatchMode.All, params IPropertyFilter[] filters)
- {
- object[] supportedProperty;
-
- if (_propertyHost.PropertyCache != null && _propertyHost.PropertyCache.SupportedDefinitions.IsPropertySupported(CacheProperties.SupportedProperties))
- supportedProperty = (await _propertyHost.PropertyCache.GetOnePropertyAsync(CacheProperties.SupportedProperties))
- .ValueAs<object[]>();
- else
- {
- await _propertyHost.ConnectAsync();
- loadBehaviors();
- var behavior = _listBehavior[options];
- if (behavior == null) throw new NotSupportedException("Behavior");
- supportedProperty = behavior.GetSupportedProperty(_propertyHost.AllPropertyProviders).ToArray();
- }
-
- return PropertyUtils.FilterProperty(supportedProperty.ToList(), matchMode, filters).ToArray();
- }
-
-
- public bool IsPropertySupported(object property)
- {
- return GetSupportedPropertyAsync().Result.Contains(property);
- }
-
- public async Task<PropertyPair> GetPropertyAsync(object property)
- {
- if (property is string)
- {
- object propertyObject = _propertyHost.ServiceLocater.FindService<IPropertyDictionary>().GetProperty((string)property);
- return await GetPropertyAsync(propertyObject);
- }
-
- var options = PropertyUtils.GetPropertyRetrivalOptions(property);
-
- loadBehaviors();
- var behavior = _getBehavior[options];
- if (behavior == null) throw new NotSupportedException("Behavior");
-
- var retVal = await behavior.GetPropertyAsync(property, _propertyHost.AllPropertyProviders, options);
- if (retVal == null && _propertyHost.PrimaryPropertyProvider == null)
- {
- await _propertyHost.ConnectAsync();
- retVal = await behavior.GetPropertyAsync(property, _propertyHost.AllPropertyProviders, options);
- }
-
- if (retVal != null)
- return retVal;
-
- throw new NotSupportedException(property.ToString());
-
- }
-
- public async Task<bool> SetPropertyAsync(PropertyPair propertyPair)
- {
- var options = PropertyUtils.GetPropertyRetrivalOptions(propertyPair.Property);
-
- loadBehaviors();
- var behavior = _setBehavior[options];
- if (behavior == null) throw new NotSupportedException("Behavior");
-
- if (options != PropertyRetrivalOptions.First) //If First, then PropertyCache will be updated only.
- await _propertyHost.ConnectAsync(); //Have to write to main.
- return await behavior.SetPropertyAsync(_propertyHost.AllPropertyProviders, propertyPair, options);
- }
-
-
- public async Task<T> InvokeAsync<T>(object property, ParameterDic parameters = null)
- {
- if (parameters == null) parameters = new ParameterDic();
-
- if (!IsPropertySupported(property))
- {
- var relatedProperty = PropertyUtils.FindRelatedProperty(property);
- if (relatedProperty != null && IsPropertySupported(relatedProperty))
- return Invoke<T>(relatedProperty, parameters);
- else throw new NotSupportedException();
- }
-
- PropertyPair ppair = await GetPropertyAsync(property);
- return await _propertyDictionary.PropertyInvoker.InvokeAsync<T>(ppair, parameters);
- }
-
- public async Task InvokeAsync(object property, ParameterDic parameters = null)
- {
- if (parameters == null) parameters = new ParameterDic();
-
- if (!IsPropertySupported(property))
- {
- var relatedProperty = PropertyUtils.FindRelatedProperty(property);
- if (relatedProperty != null && IsPropertySupported(relatedProperty))
- {
- Invoke(relatedProperty, parameters);
- return;
- }
- else throw new NotSupportedException();
- }
-
- PropertyPair ppair = await GetPropertyAsync(property);
- await _propertyDictionary.PropertyInvoker.InvokeAsync(ppair, parameters);
- }
-
- public T Invoke<T>(object property, ParameterDic parameters = null)
- {
- if (parameters == null) parameters = new ParameterDic();
-
- if (!IsPropertySupported(property))
- {
- var relatedProperty = PropertyUtils.FindRelatedProperty(property);
- if (relatedProperty != null && IsPropertySupported(relatedProperty))
- {
- return Task.Run<T>(async () => await InvokeAsync<T>(relatedProperty, parameters)).Result;
- }
-
- else throw new NotSupportedException();
- }
-
- PropertyPair ppair = this.GetProperty(property);
- return _propertyDictionary.PropertyInvoker.Invoke<T>(ppair, parameters);
- }
-
- public void Invoke(object property, ParameterDic parameters = null)
- {
- if (parameters == null) parameters = new ParameterDic();
-
- if (!IsPropertySupported(property))
- {
- var relatedProperty = PropertyUtils.FindRelatedProperty(property);
- if (relatedProperty != null && IsPropertySupported(relatedProperty))
- {
- InvokeAsync(relatedProperty, parameters).RunSynchronously();
- return;
- }
- else throw new NotSupportedException();
- }
-
- PropertyPair ppair = this.GetProperty(property);
- _propertyDictionary.PropertyInvoker.Invoke(ppair, parameters);
- }
-
- #endregion
-
- #region Data
-
- private IPropertyDictionary _propertyDictionary;
- private IPropertyHost _propertyHost;
-
- static Dictionary<PropertyRetrivalOptions, IPropertyListSupportedBehavior> _listBehavior;
- static Dictionary<PropertyRetrivalOptions, IPropertyGetBehavior> _getBehavior;
- static Dictionary<PropertyRetrivalOptions, IPropertySetBehavior> _setBehavior;
-
- #endregion
-
- #region Public Properties
-
- public IPropertyHost PropertyHost { get { return _propertyHost; } }
-
- #endregion
-
-
-
-
-
-
-
-
-
- }
- }