예제 #1
0
        /// <summary>
        /// Captura a DAO simples relacionada com a Model do tipo submetido.
        /// </summary>
        /// <typeparam name="T">Model na qual a DAO est� relacionada.</typeparam>
        /// <returns>DAO.</returns>
        /// <exception cref="GDAException"></exception>
        public static ISimpleBaseDAO <T> GetSimpleDAO <T>()
        {
            Type persistenceType = typeof(T);

            if (MappingManager.MembersDAO.ContainsKey(persistenceType))
            {
                return((ISimpleBaseDAO <T>)MappingManager.MembersDAO[persistenceType]);
            }
            else
            {
                PersistenceBaseDAOAttribute info = MappingManager.GetPersistenceBaseDAOAttribute(persistenceType);
                if (info != null)
                {
                    ISimpleBaseDAO <T> dao;
                    try
                    {
                        if (info.BaseDAOType.IsGenericType)
                        {
                            Type t = info.BaseDAOType.MakeGenericType(info.BaseDAOGenericTypes);
                            dao = (ISimpleBaseDAO <T>)Activator.CreateInstance(t);
                        }
                        else
                        {
                            dao = (ISimpleBaseDAO <T>)Activator.CreateInstance(info.BaseDAOType);
                        }
                    }
                    catch (InvalidCastException)
                    {
                        throw new GDAException(String.Format("Invalid cast, type {0} not inherit interface ISimpleBaseDAO.", info.BaseDAOType.FullName));
                        ;
                    }
                    catch (Exception ex)
                    {
                        if (ex is TargetInvocationException)
                        {
                            throw new GDAException(ex.InnerException);
                        }
                        else
                        {
                            throw new GDAException(ex);
                        }
                    }
                    return(dao);
                }
                else
                {
                    try
                    {
                        return(new SimpleBaseDAO <T>());
                    }
                    catch (Exception ex)
                    {
                        throw new GDAException("Error to create instance SimpleBaseDAO<> for type " + persistenceType.FullName + ".\r\n" + ex.Message, ex);
                    }
                }
            }
        }