Exemplo n.º 1
0
        protected IT GetNavigationPropertyDataItemFromCache <T, IT>([CallerMemberName] string navigationPropertyName = null) where T : ObjectBase
        {
            IT result = default(IT);

            if (IsEnableFillNavigationProperyByCache)
            {
                if (!NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                {
                    object          resultFromSecondLevelCache = null;
                    IEnumerable <T> result1 = GetNavigationPropertyDataAtFirstTime <T>(navigationPropertyName, out resultFromSecondLevelCache);
                    if (NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                    {
                        result = (IT)NavigationPropertyDataDic[navigationPropertyName];
                    }
                }
                else
                {
                    if (NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                    {
                        result = (IT)NavigationPropertyDataDic[navigationPropertyName];
                    }
                }
            }
            else
            {
                object value;
                if (NavigationPropertyDataDic.TryGetValue(string.Concat(navigationPropertyName, "1"), out value))
                {
                    result = (IT)value;
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        protected T GetNavigationPropertyDataItemFromCache <T>([System.Runtime.CompilerServices.CallerMemberName] string navigationPropertyName = null) where T : ObjectBase
        {
            T result = default(T);

            if (IsEnableFillNavigationProperyByCache)
            {
                if (!NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                {
                    object          resultFromSecondLevelCache = null;
                    IEnumerable <T> result1 = GetNavigationPropertyDataAtFirstTime <T>(navigationPropertyName, out resultFromSecondLevelCache);
                    if (NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                    {
                        result = (T)NavigationPropertyDataDic[navigationPropertyName];
                    }
                }
                else
                {
                    //System.Diagnostics.Debug.WriteLine(NavigationPropertyDataDic[navigationPropertyName]);
                    result = (T)NavigationPropertyDataDic[navigationPropertyName];
                }
            }
            else
            {
                object value;
                if (NavigationPropertyDataDic.TryGetValue(string.Concat(navigationPropertyName, "1"), out value))
                {
                    result = (T)value;
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        //public override bool Equals(object obj)
        //{
        //    if (ReferenceEquals(null, obj)) return false;
        //    if (ReferenceEquals(this, obj)) return true;
        //    if (obj.GetType() != typeof(_EntityBase)) return false;
        //    return Equals((_EntityBase)obj);
        //}

        protected ICollection <T> GetNavigationPropertyDataListFromCache <T>([CallerMemberName] string navigationPropertyName = null) where T : ObjectBase
        {
            ICollection <T> result = null;

            if (IsEnableFillNavigationProperyByCache)
            {
                if (!NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                {
                    object          tmp     = null;
                    IEnumerable <T> result1 = GetNavigationPropertyDataAtFirstTime <T>(navigationPropertyName, out tmp);
                    if (NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                    {
                        result = NavigationPropertyDataDic[navigationPropertyName] as List <T>;
                    }
                }
                else
                {
                    result = NavigationPropertyDataDic[navigationPropertyName] as List <T>;
                }
            }
            else
            {
                object value;
                if (NavigationPropertyDataDic.TryGetValue(string.Concat(navigationPropertyName, "1"), out value))
                {
                    result = (ICollection <T>)value;
                }
            }

            return(result);
        }
Exemplo n.º 4
0
 public virtual void OnColumnChanging(string columnName, ref object value)
 {
     if (NavigationPropertyDataDic.Count > 0 && EntityInfo().InfoForFillingNavigationPropertyDic.Count > 0)
     {
         var infoForFillingNavigationPropertyList = EntityInfo().InfoForFillingNavigationPropertyDic.Where(item => item.Value.ThisEntityRefrencePropertyName == columnName).ToList();
         foreach (var infoForFillingNavigationProperty in infoForFillingNavigationPropertyList)
         {
             object tmp;
             NavigationPropertyDataDic.TryRemove(infoForFillingNavigationProperty.Key, out tmp);
         }
     }
 }
Exemplo n.º 5
0
        private IEnumerable <T> GetNavigationPropertyDataAtFirstTime <T>(string navigationPropertyName, out object resultForSecondLevelDataSource) where T : ObjectBase
        {
            resultForSecondLevelDataSource = null;
            var infoForFillingNavigationProperty = EntityInfo().InfoForFillingNavigationPropertyDic[navigationPropertyName];

            InitForFillingNavigationProperty_JustOneTime(infoForFillingNavigationProperty);

            IEnumerable <T> result = null;

            if (!NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
            {
                var thisPropertyValue = this[infoForFillingNavigationProperty.ThisEntityRefrencePropertyName];
                var propertyType      = EntityInfo().Properties[infoForFillingNavigationProperty.ThisEntityRefrencePropertyName].PropertyType;
                var cacheInfo         = CacheConfig.CacheInfoDic.First(ci => ci.Value.Name == infoForFillingNavigationProperty.CacheName).Value;
                if (thisPropertyValue != Activator.CreateInstance(propertyType))
                {
                    lock (cacheInfo.InfoAndEntityListForFillingNavigationPropertyDic)
                    {
                        if (!NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                        {
                            if (thisPropertyValue != Activator.CreateInstance(propertyType))
                            {
                                if (cacheInfo.DisableCache)
                                {
                                    ObjectBase resultItem;
                                    result = (cacheInfo.MethodInfo.Invoke(null, new object[] { cacheInfo.Repository.GetQueryableForCahce(AppBase.DependencyInjectionFactory.CreateContextInstance()) }) as IQueryable <T>)
                                             .Where(string.Format("{0} == {1}", infoForFillingNavigationProperty.OtherEntityRefrencePropertyName, thisPropertyValue));
                                    resultItem = result.FirstOrDefault() as ObjectBase;
                                    if (resultItem != null)
                                    {
                                        resultItem.EnableFillNavigationProperyByCache();
                                    }
                                }
                                else
                                {
                                    var queryableCacheExecution = new QueryableCacheDataProvider <T>(cacheInfo);
                                    //  _stopwatch.Restart();
                                    result = queryableCacheExecution.Cache <List <T> >(cacheInfo, cacheInfo.AutoRefreshInterval, cacheInfo.BasicKey.ToString(), true).
                                             Where(item =>
                                    {
                                        var otherPropertyValue = item[infoForFillingNavigationProperty.OtherEntityRefrencePropertyName];
                                        return(otherPropertyValue != null && otherPropertyValue.Equals(thisPropertyValue));
                                    });
                                    // result.ToList().ForEach(item => (item as _EntityBase).EnableFillNavigationProperyByCache = true);
                                    // _stopwatch.Stop();
                                    // cacheInfo.UsingTime += TimeSpan.FromTicks(_stopwatch.ElapsedTicks);
                                }

                                if (!infoForFillingNavigationProperty.IsEnumerable)
                                {
                                    if (result.Count() == 0 && !string.IsNullOrEmpty(infoForFillingNavigationProperty.SecondLevelDataSourceName))
                                    {
                                        var dataSourceInfo = CacheConfig.CacheInfoDic.First(ci => ci.Value.Name == infoForFillingNavigationProperty.SecondLevelDataSourceName).Value;
                                        resultForSecondLevelDataSource = ((dataSourceInfo.MethodInfo.Invoke(null, new object[] { dataSourceInfo.Repository.GetQueryableForCahce(AppBase.DependencyInjectionFactory.CreateContextInstance()) }) as IQueryable)
                                                                          .Where(string.Format("{0} == {1}", infoForFillingNavigationProperty.OtherEntityRefrencePropertyName, thisPropertyValue)) as IEnumerable).Cast <IEntity>().FirstOrDefault();
                                        if (resultForSecondLevelDataSource != null)
                                        {
                                            ((ObjectBase)resultForSecondLevelDataSource).EnableFillNavigationProperyByCache();
                                        }
                                    }

                                    ObjectBase resultItem;
                                    if (!cacheInfo.DisableCache && result.Count() == 0)
                                    {
                                        //ToDo: Change invoke method by excact delegate for better performance.
                                        result = (cacheInfo.MethodInfo.Invoke(null, new object[] { cacheInfo.Repository.GetQueryableForCahce(AppBase.DependencyInjectionFactory.CreateContextInstance()) }) as IQueryable <T>)
                                                 .Where(string.Format("{0} == {1}", infoForFillingNavigationProperty.OtherEntityRefrencePropertyName, thisPropertyValue));
                                        resultItem = result.FirstOrDefault() as ObjectBase;
                                        if (resultItem != null)
                                        {
                                            resultItem.EnableFillNavigationProperyByCache();
                                        }
                                        //item => item[infoForFillingNavigationProperty.OtherEntityRefrencePropertyName].Equals(thisPropertyValue));
                                    }
                                }
                            }
                            else
                            {
                                result = new List <T>();
                            }

                            if (infoForFillingNavigationProperty.IsEnumerable)
                            {
                                NavigationPropertyDataDic[navigationPropertyName] = result.ToList <T>();
                            }
                            else
                            {
                                if (resultForSecondLevelDataSource == null)
                                {
                                    if (result != null)
                                    {
                                        NavigationPropertyDataDic[navigationPropertyName] = result.FirstOrDefault();
                                    }
                                }
                                else
                                {
                                    NavigationPropertyDataDic[navigationPropertyName] = resultForSecondLevelDataSource;
                                }
                            }
                        }
                        else
                        {
                            result = NavigationPropertyDataDic[navigationPropertyName] as List <T>;
                        }

                        var entities = cacheInfo.InfoAndEntityListForFillingNavigationPropertyDic[infoForFillingNavigationProperty];
                        lock (entities)
                        {
                            cacheInfo.InfoAndEntityListForFillingNavigationPropertyDic[infoForFillingNavigationProperty].Add(this);
                        }
                    }
                }
                else
                {
                    result = new List <T>();
                }
            }
            else
            {
                result = NavigationPropertyDataDic[navigationPropertyName] as List <T>;
            }

            return(result);
        }