Ejemplo n.º 1
0
 /// <summary>
 /// Here we will using reflection and get instance using className + register in Instance table if instanceName != null
 /// </summary>
 /// <param name="classDefinition"></param>
 /// <returns></returns>
 public object GetInstance(ClassDefinition classDefinition, string instanceName)
 {
     return(null);
 }
Ejemplo n.º 2
0
        public DomainObject GetObjectByInstanceName(string instanceName)
        {
            if (this.NeedCaching(instanceName) && this.IsInCache(instanceName))
            {
                return(this.GetFromCache(instanceName));
            }
            Instance instance  = GetBySystemName(instanceName);
            string   className = null;
            int      fileId    = 0;

            using (ClassDefinitionFactory classFactory = ClassDefinitionFactory.GetInstance())
            {
                classFactory.Context = this.FactoryContext;
                ClassDefinition definition = classFactory.InternalLoadByPrimaryKey(instance.ClassDefinitionId);
                fileId    = definition.FileId;
                className = definition.ClassName;
            }
            string fileName       = null;
            int    fileLocationId = 0;

            using (PlugFileFactory fileFactory = PlugFileFactory.GetInstance())
            {
                fileFactory.Context = this.FactoryContext;
                PlugFile file = fileFactory.InternalLoadByPrimaryKey(fileId);
                fileName       = file.PlugFileName;
                fileLocationId = file.DestinationLocationId;
            }
            string path = null;

            using (PlugLocationFactory locationFactory = PlugLocationFactory.GetInstance())
            {
                locationFactory.Context = this.FactoryContext;
                PlugLocation location = locationFactory.InternalLoadByPrimaryKey(fileLocationId);
                path = location.PlugLocationPath;
            }
            string filePath     = Path.Combine(path, fileName);
            string assemblyName = Path.GetFileNameWithoutExtension(filePath);
            Type   requiredType = null;

            Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in loadedAssemblies)
            {
                if (assembly.GetName().Name == assemblyName)
                {
                    requiredType = assembly.GetType(className, false, false);
                    if (requiredType != null)
                    {
                        break;
                    }
                }
            }
            if (requiredType == null)
            {
                Assembly newDll = Assembly.LoadFrom(filePath);
                requiredType = newDll.GetType(className, false, false);
            }

            if (requiredType == null)
            {
                throw new ArgumentException(string.Format("Type '{0}' was not found.", className));
            }
            DomainObject    instanceObject = (DomainObject)Activator.CreateInstance(requiredType);
            IInstantiatable instantinable  = instanceObject as IInstantiatable;

            if (instantinable != null)
            {
                instantinable.Setup(instance, this.FactoryContext);
            }
            if (NeedCaching(instanceName))
            {
                SetToCache(instanceName, instanceObject);
            }
            return(instanceObject);
        }