// TODO Providers and configured adapters

        public object GetAdapter(object adaptee, string adapterRoleName)
        {
            if (adaptee == null)
            {
                throw new ArgumentNullException("adaptee");
            }
            if (string.IsNullOrEmpty(adapterRoleName))
            {
                throw Failure.NullOrEmptyString(nameof(adapterRoleName));
            }
            object result;

            if (TryBasicFactories(adaptee.GetType(), adapterRoleName, t => t.GetAdapter(adaptee, adapterRoleName), out result))
            {
                return(result);
            }

            var convo = GetConventionAdapterType(adaptee.GetType(), adapterRoleName);

            if (convo == null)
            {
                return(null);
            }
            return(AdapterFactory.NewInstance(adaptee, convo));
        }
        private bool TryBasicFactories <T>(Type type, string adapterRoleName, Func <IAdapterFactory, T> func, out T result)
        {
            var roleAsm = AdapterFactory.GetAssemblyThatDefines(adapterRoleName);

            if (roleAsm == null)
            {
                throw RuntimeFailure.AdapterRoleNotDefined("adapterRoleName", adapterRoleName);
            }
            // Try assembly factory, factory where the role is defined, then provider factories
            var asm = AdapterFactory.FromAssembly(type.GetTypeInfo().Assembly) ?? AdapterFactory.Null;

            result = func(asm);
            if (result != null)
            {
                return(true);
            }

            result = func(AdapterFactory.FromAssembly(roleAsm));
            if (result != null)
            {
                return(true);
            }

            // Exclude self, otherwise would be recursive
            var right = App.GetProviders <IAdapterFactory>().Where(t => t != this);

            foreach (var m in right)
            {
                result = func(m);
                if (result != null)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #3
0
 public static StreamingSourceFactory FromAssembly(Assembly assembly)
 {
     return(new StreamingSourceFactory(AdapterFactory.FromAssembly(assembly)));
 }
Example #4
0
 public static TemplateFactory FromAssembly(Assembly assembly)
 {
     return(new TemplateFactory(AdapterFactory.FromAssembly(assembly)));
 }