/// <summary>
        /// This decodes the type and returns an array of types. If the type is based on a GenericDto
        /// then it returns the Data type and the
        /// </summary>
        /// <typeparam name="TD"></typeparam>
        /// <param name="whatItShouldBe"></param>
        /// <param name="syncAsync"></param>
        /// <returns></returns>
        private static Type[] GetTypesFromInitialType <TD>(WhatItShouldBe whatItShouldBe, SyncAsyncDefiner syncAsync)
            where TD : class
        {
            var classType = typeof(TD);

            Type[] dataTypes;
            if (classType.IsSubclassOf(typeof(EfGenericDtoBase)))
            {
                dataTypes = GetGenericTypesIfCorrectGeneric(classType, syncAsync.BaseGenericDtoType);
                if (dataTypes == null)
                {
                    throw new InvalidOperationException(string.Format("This service needs a class which inherited from {0}.",
                                                                      syncAsync.BaseGenericDtoType.Name));
                }
            }
            else if (!whatItShouldBe.HasFlag(WhatItShouldBe.DataClass))
            {
                throw new InvalidOperationException("This type of service only works with some form of EfGenericDto.");
            }
            else
            {
                //Its a data class
                dataTypes = new[] { typeof(TD) };
            }
            return(dataTypes);
        }
            public SyncAsyncDefiner(WhatItShouldBe whatItShouldBe)
            {
                if (whatItShouldBe.HasFlag(WhatItShouldBe.IsSync))
                {
                    BaseGenericDtoType            = typeof(EfGenericDto <,>);
                    _serviceReplaceString         = UpdateServiceReplaceString;
                    _serviceAssemblyQualifiedName = UpdateServiceAssemblyQualifiedName;
                }
                else if (whatItShouldBe.HasFlag(WhatItShouldBe.IsAsync))
                {
                    BaseGenericDtoType            = typeof(EfGenericDtoAsync <,>);
                    _serviceReplaceString         = UpdateServiceAsyncReplaceString;
                    _serviceAssemblyQualifiedName = UpdateServiceAsyncAssemblyQualifiedName;
                }
                else
                {
                    throw new InvalidOperationException("Neither the IsSync or the IsAsync flags were set.");
                }

                //If any type allowed we test against base
                if (!whatItShouldBe.HasFlag(WhatItShouldBe.SpecificDto))
                {
                    BaseGenericDtoType = typeof(EfGenericDtoBase <,>);
                }
            }
        //public static Type[] DecodeTypes<TD>(WhatItShouldBe whatItShouldBe) where TD : class
        //{
        //    return GetTypesFromInitialType<TD>(whatItShouldBe, new SyncAsyncDefiner(whatItShouldBe));
        //}

        private static dynamic CreateService <TD>(WhatItShouldBe whatItShouldBe, params object[] ctorParams) where TD : class
        {
            var syncAsync = new SyncAsyncDefiner(whatItShouldBe);

            var dataTypes = GetTypesFromInitialType <TD>(whatItShouldBe, syncAsync);

            var genericServiceString = syncAsync.BuildTypeString(dataTypes.Length);
            var serviceGenericType   = Type.GetType(genericServiceString);

            if (serviceGenericType == null)
            {
                throw new InvalidOperationException("Failed to create the type. Is the DTO of the correct type?");
            }
            var serviceType = serviceGenericType.MakeGenericType(dataTypes);

            return(Activator.CreateInstance(serviceType, ctorParams));
        }
 public static dynamic CreateCorrectService <TD>(WhatItShouldBe whatItShouldBe, params object[] ctorParams) where TD : class
 {
     return(CreateService <TD>(whatItShouldBe, ctorParams));
 }