Пример #1
0
        public static Entity LocateEntity <Entity>(string key, bool isLocatorEnabled) where Entity : class, IEntity, new()
        {
            Entity local = default(Entity);

            if (((key != null) && isLocatorEnabled) && EntityLocator.Contains(key))
            {
                local = EntityLocator.Get(key) as Entity;
            }
            return(local);
        }
Пример #2
0
        public static Entity LocateOrCreate <Entity>(string key, string typeString, Type entityFactoryType, bool isLocatorEnabled) where Entity : class, IEntity, new()
        {
            if (string.IsNullOrEmpty(typeString))
            {
                throw new ArgumentException("typeString");
            }
            if (entityFactoryType == null)
            {
                throw new ArgumentException("entityFactoryType");
            }
            Entity entity      = default(Entity);
            Type   defaultType = typeof(Entity);

            if (defaultType.GetInterface("IEntityCacheItem") != null)
            {
                entity = EntityCache.GetItem <Entity>(key.ToString());
            }
            if (entity == null)
            {
                IEntityFactory factory = null;
                if (EntityFactories.ContainsKey(entityFactoryType.FullName))
                {
                    factory = EntityFactories[entityFactoryType.FullName];
                }
                else
                {
                    factory = TryAddEntityFactory(entityFactoryType);
                }
                if (((key != null) && isLocatorEnabled) && EntityLocator.Contains(key))
                {
                    entity = EntityLocator.Get(key) as Entity;
                }
                if (entity == null)
                {
                    entity = factory.CreateEntity(typeString, defaultType) as Entity;
                }
                if (!entity.IsEntityTracked)
                {
                    StartTracking(key, entity, isLocatorEnabled);
                }
                if (entity.GetType().GetInterface("IEntityCacheItem") != null)
                {
                    EntityCache.AddCache(key, entity);
                }
            }
            return(entity);
        }