public Genericattributetype GetAttributeType(string type, object typeObject = null, int?categoryId = null)
        {
            Check.Require(!string.IsNullOrWhiteSpace(type), "type must be valid.");

            var encodedLabel = EncodeLabel(type);

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.All,
            };

            settings.Converters.Insert(0, new PrimitiveJsonConverter());

            Genericattributetype foundEntity = null;

            if (typeObject != null)
            {
                var jsonObj = JsonConvert.SerializeObject(typeObject, settings);
                var found   = _attributeTypeRepository.DbSet.FirstOrDefault(x =>
                                                                            string.Compare(x.Name, type, true) == 0 &&
                                                                            string.Compare(x.Valuestring, jsonObj, true) == 0 &&
                                                                            x.Categoryid == categoryId);
                foundEntity = found;
            }
            else
            {
                var found2 = _attributeTypeRepository.DbSet.FirstOrDefault(x =>
                                                                           string.Compare(x.Name, type, true) == 0 &&
                                                                           x.Categoryid == categoryId);
                foundEntity = found2;
            }

            return(foundEntity);
        }
        private async Task <Tuple <Genericattributetype, Genericattributevalue> > CreateOrGetTupleTypeValue(
            string type,
            string value,
            object typeObject  = null,
            object valueObject = null,
            int typeSort       = 0,
            int valueSort      = 0,
            int?categoryId     = null)
        {
            //var res = "";

            //TypeSwitch.Do(
            //    value,
            //    TypeSwitch.Case<sbyte>(() => res = "It's a int"),
            //    TypeSwitch.Case<byte>(x => res = "It's a long"),
            //    TypeSwitch.Case<short>(x => res = "It's a long"),
            //    TypeSwitch.Case<ushort>(x => res = "It's a long"),
            //    TypeSwitch.Case<int>(x => res = "It's a long"),
            //    TypeSwitch.Case<uint>(x => res = "It's a long"),
            //    TypeSwitch.Case<long>(x => res = "It's a long"),
            //    TypeSwitch.Case<ulong>(x => res = "It's a long"),
            //    TypeSwitch.Case<char>(x => res = "It's a long"),
            //    TypeSwitch.Case<float>(x => res = "It's a long"),
            //    TypeSwitch.Case<double>(x => res = "It's a long"),
            //    TypeSwitch.Case<bool>(x => res = "It's a long"),
            //    TypeSwitch.Case<decimal>(x => res = "It's a long"),
            //    TypeSwitch.Case<string>(x => res = "It's a long"),
            //    TypeSwitch.Case<DateTime>(x => res = "It's a long"),
            //    TypeSwitch.Default(() => res = "It's default"));

            var utcNow = DateTime.UtcNow;

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.All,
            };

            settings.Converters.Insert(0, new PrimitiveJsonConverter());


            var typeEntity  = new Genericattributetype();
            var valueEntity = new Genericattributevalue();

            typeEntity.Createdon  = utcNow;
            typeEntity.Modifiedon = utcNow;
            typeEntity.Createdby  = "System";
            typeEntity.Modifiedby = "System";

            valueEntity.Createdon  = utcNow;
            valueEntity.Modifiedon = utcNow;
            valueEntity.Createdby  = "System";
            valueEntity.Modifiedby = "System";

            typeEntity.Sort  = typeSort;
            valueEntity.Sort = valueSort;

            typeEntity.Name = type;

            if (categoryId.HasValue)
            {
                typeEntity.Categoryid = categoryId.Value;
            }

            if (typeObject != null)
            {
                var typeObjectJson = JsonConvert.SerializeObject(typeObject, settings);
                typeEntity.Valuestring = typeObjectJson;

                typeEntity.Metatypestring = typeObject.GetType().Name;
                typeEntity.Metatypelabel  = typeObject.GetType().AssemblyQualifiedName;
            }

            valueEntity.Name = value;

            if (valueObject != null)
            {
                var valueObjectJson = JsonConvert.SerializeObject(valueObject, settings);
                valueEntity.Valuestring = valueObjectJson;

                valueEntity.Metatypestring = valueObject.GetType().Name;
                valueEntity.Metatypelabel  = valueObject.GetType().AssemblyQualifiedName;
            }

            Genericattributetype  finalType;
            Genericattributevalue finalValue;

            var findType = _attributeTypeRepository.DbSet.FirstOrDefault(
                x => x.Categoryid == typeEntity.Categoryid &&
                x.Name == typeEntity.Name &&
                x.Valuestring == typeEntity.Valuestring &&
                x.Metatypestring == typeEntity.Metatypestring &&
                x.Metatypelabel == typeEntity.Metatypelabel);

            if (findType != null)
            {
                finalType = findType;
            }
            else
            {
                await _attributeTypeRepository.AddAsync(typeEntity);

                var errors1 = await _unitOfWork.CommitHandledAsync();

                if (!errors1)
                {
                    _logger.LogError($"Can't Attribute Type ! {JsonConvert.SerializeObject(typeEntity, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })} ");
                }

                finalType = typeEntity;
            }

            var findValue = _attributeValueRepository.DbSet.FirstOrDefault(
                x => x.Name == valueEntity.Name &&
                x.Valuestring == valueEntity.Valuestring &&
                x.Metatypestring == valueEntity.Metatypestring &&
                x.Metatypelabel == valueEntity.Metatypelabel);

            if (findValue != null)
            {
                finalValue = findValue;
            }
            else
            {
                await _attributeValueRepository.AddAsync(valueEntity);

                var errors2 = await _unitOfWork.CommitHandledAsync();

                if (!errors2)
                {
                    _logger.LogError($"Can't Attribute Value ! {JsonConvert.SerializeObject(valueEntity, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })} ");
                }

                finalValue = valueEntity;
            }

            var resTuple = new Tuple <Genericattributetype, Genericattributevalue>(finalType, finalValue);

            return(resTuple);
        }
        private Tuple <Genericattributetype, Genericattributevalue> CreateBulkTupleTypeValue(
            AttributeBulk bulkInfo)
        {
            var utcNow = DateTime.UtcNow;

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.All,
            };

            settings.Converters.Insert(0, new PrimitiveJsonConverter());


            var typeEntity  = new Genericattributetype();
            var valueEntity = new Genericattributevalue();

            typeEntity.Createdon  = utcNow;
            typeEntity.Modifiedon = utcNow;
            typeEntity.Createdby  = "System";
            typeEntity.Modifiedby = "System";

            valueEntity.Createdon  = utcNow;
            valueEntity.Modifiedon = utcNow;
            valueEntity.Createdby  = "System";
            valueEntity.Modifiedby = "System";

            typeEntity.Sort  = bulkInfo.TypeSort;
            valueEntity.Sort = bulkInfo.ValueSort;

            typeEntity.Name = bulkInfo.Type;

            if (bulkInfo.CategoryId.HasValue)
            {
                typeEntity.Categoryid = bulkInfo.CategoryId.Value;
            }

            if (bulkInfo.TypeObject != null)
            {
                var typeObjectJson = JsonConvert.SerializeObject(bulkInfo.TypeObject, settings);
                typeEntity.Valuestring = typeObjectJson;

                typeEntity.Metatypestring = bulkInfo.TypeObject.GetType().Name;
                typeEntity.Metatypelabel  = bulkInfo.TypeObject.GetType().AssemblyQualifiedName;
            }

            valueEntity.Name = bulkInfo.Value;

            if (bulkInfo.ValueObject != null)
            {
                var valueObjectJson = JsonConvert.SerializeObject(bulkInfo.ValueObject, settings);
                valueEntity.Valuestring = valueObjectJson;

                valueEntity.Metatypestring = bulkInfo.ValueObject.GetType().Name;
                valueEntity.Metatypelabel  = bulkInfo.ValueObject.GetType().AssemblyQualifiedName;
            }

            Genericattributetype  finalType;
            Genericattributevalue finalValue;

            finalType = typeEntity;

            finalValue = valueEntity;

            var resTuple = new Tuple <Genericattributetype, Genericattributevalue>(finalType, finalValue);

            return(resTuple);
        }