Esempio n. 1
0
        // public static ServiceContainer Ioc { get; internal set; }

        public static DataHelpers instanc()
        {
            if (_instance == null)
            {
                _instance = new DataHelpers();
            }
            return(_instance);
        }
Esempio n. 2
0
        //public Task<List<T>> GetMongoDataAll<T>() where T : IDocument
        //{
        //    return Task.Run(() => { return DS.db.GetAll<T>().ToList(); });
        //}

        //public async Task<IEnumerable<dynamic>> GetMongoDataRange(string type,int pageNumber)
        //{
        //    var generic = GetGenericData(type);

        //    var GetRange = (generic.GetType() as Type).GetMethod("GetRange");
        //    Task<IEnumerable<dynamic>> res = GetRange.Invoke(generic, new object[] { pageNumber });

        //    return ( (await res) as IEnumerable<dynamic>);

        //}

        //public async Task<List<T>> GetMongoDataPaged<T>(int pageNumber, int selectedCOunt) where T : IDocument
        //{
        //    var result = await DS.db.GetPaginatedAsync<T,Guid>(a => true, ((pageNumber - 1) * selectedCOunt), selectedCOunt);
        //    return result.OrderByDescending(a => a.AddedAtUtc).ToList();
        //    //return await DS.db.GetPaginatedAsyncA<T>(a => true, ((pageNumber - 1) * selectedCOunt), selectedCOunt);

        //}

        //public async Task<List<T>> GetMongoDataFilterPaged<T>(string lowerName, int pageNumber, int selectedCOunt) where T : IDocument
        //{

        //    return await DS.db.GetPaginatedAsyncA<T>(a => a.NameSearch.ToLower().Contains(lowerName), ((pageNumber - 1) * selectedCOunt), selectedCOunt);

        //}

        //public async Task<long> GetMongoDataCount<T>(Expression<Func<T, bool>> filter) where T : IDocument
        //{
        //    return await DS.db.CountAsync<T>(filter);

        //}

        //public async Task<long> GetMongoDataCount(string type,bool IsTypeFullName = false)
        //{

        //    //Type _type;
        //    //if (IsTypeFullName)
        //    //{
        //    //    _type = DataHelpers.GetTypes[type];// Type.GetType(type);
        //    //}
        //    //else
        //    //{
        //    //    _type = DataHelpers.GetTypes[type]; ;// Type.GetType($"Ovresko.Generix.Core.Modules.{type}");
        //    //}

        //    var generic = GetGenericData(type);

        //    var metGetCount = (generic.GetType() as Type).GetMethod("GetCount");
        //    Task<long> res = metGetCount.Invoke(generic, null);

        //    return await res;

        //}

        //public async Task<List<T>> GetData<T>(Expression<Func<T, bool>> filter) where T : IDocument
        //{
        //    return await DS.db.GetAllAsync<T>(filter);

        //}


        /// <summary>
        /// /////
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filter"></param>
        /// <returns></returns>

        //public static async Task<List<dynamic>> GetDataStatic(Type type, Expression<Func<dynamic, bool>> filter)
        //{

        //    MethodInfo method = typeof(DataHelpers).GetMethod("GetDataStaticGeneric");
        //    MethodInfo genericMethod = method.MakeGenericMethod(type);

        //    var s = (genericMethod.Invoke(null, new object[] { filter }));

        //    return await (s as Task<List<dynamic>>);

        //}

        //public static List<T> GetDataStaticGeneric<T>(Expression<Func<T, bool>> filter) where T : IDocument
        //{
        //    var s = DS.db.GetAllAsync<T>(filter).Result;
        //    return s;
        //}

        //public List<T> GetDataSync<T>(Expression<Func<T, bool>> filter) where T : IDocument
        //{
        //    return DS.db.GetAll<T>(filter);

        //}
        //public async Task<T> GetOne<T>(Expression<Func<T, bool>> filter) where T : IDocument
        //{
        //    return await DS.db.GetOneAsync<T>(filter);
        //}


        //public Task<long> GetMongoDataCount<T>() where T : IDocument
        //{
        //    return DS.db.CountAsync<T>(a => true);

        //}

        //public Task<List<T>> GetMongoDataSearch<T>(string nameSearch) where T : IDocument
        //{
        //    return DS.db.GetAllAsync<T>(a => a.Name.ToLower().Contains(nameSearch.ToLower()));
        //}

        //public static void MapPropertiesRef(ref IDocument selected, dynamic original,string fetchValue)
        //{
        //    selected = MapProperties(selected, original,true, fetchValue);
        //}

        public static IDocument MapProperties(IDocument toDocument, dynamic fromDocument, bool OnlyIfNull = false, string selectedProeprty = null)
        {
            try
            {
                var notAlloewd = DataHelpers.NotAllowedProperties;
                notAlloewd.Add("Series");

                List <PropertyInfo> propsSelected = new List <PropertyInfo>();
                if (!string.IsNullOrWhiteSpace(selectedProeprty))
                {
                    propsSelected.Add(toDocument.GetType().GetProperty(selectedProeprty));
                }
                else
                {
                    propsSelected = toDocument.GetType().GetProperties().Where(z => z.GetCustomAttribute(typeof(DisplayNameAttribute)) != null).ToList();
                }

                var propsModel = (fromDocument.GetType().GetProperties() as PropertyInfo[]).Select(z => z.Name);

                var commun = propsSelected.Where(a => propsModel.Contains(a.Name) &&
                                                 !notAlloewd.Contains(a.Name) &&
                                                 a.CanWrite);

                if (commun != null)
                {
                    foreach (var prop in commun)
                    {
                        try
                        {
                            var  propModel    = (fromDocument as IDocument).GetType().GetProperty(prop.Name);
                            Type typeModel    = propModel?.PropertyType;
                            Type typeSelected = prop.PropertyType;
                            if (typeSelected.Equals(typeModel) == true)
                            {
                                var     value   = propModel.GetValue(fromDocument);
                                dynamic meValue = prop.GetValue(toDocument);
                                if (OnlyIfNull)
                                {
                                    var type = prop.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;

                                    if ((type != null && type.FieldType == ModelFieldType.ReadOnly))
                                    {
                                        prop.SetValue(toDocument, value);
                                    }
                                }
                                else
                                {
                                    prop.SetValue(toDocument, value);
                                }
                            }
                        }
                        catch (Exception s)
                        {
                            DataHelpers.Logger.LogError(s);
                            DataHelpers.ShowMessage(s.Message);
                            continue;
                        }
                    }
                }

                //selected = original.MapRefsTo(selected);

                return(toDocument);
            }
            catch (Exception s)
            {
                DataHelpers.Logger.LogError(s);
                DataHelpers.ShowMessage(s.Message);
                return(null);
            }
        }