Example #1
0
        /// <summary>
        /// Saves an entity based on a provided type.
        /// </summary>
        /// <remarks>
        /// This version of Save() does not run Validation, or
        /// before and after save events since it's not tied to
        /// the current entity type. If you want the full featured
        /// save use the non-generic Save() operation.
        /// </remarks>
        /// <returns></returns>
        public bool Save <T>(T entity, string collectionName = null)
            where T : class, new()
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                collectionName = Pluralizer.Pluralize(typeof(T).Name);
            }

            if (entity == null)
            {
                SetError("No entity to save passed.");
                return(false);
            }

            try
            {
                var result = Database.GetCollection(collectionName).Save(entity);
                if (result.HasLastErrorMessage)
                {
                    SetError(result.LastErrorMessage);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                SetError(ex, true);
                return(false);
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Specialized CreateContext that accepts a connection string and provider.
        /// Creates a new context based on a Connection String name or
        /// connection string.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        /// <remarks>Important:
        /// This only works if you implement a DbContext contstructor on your custom context
        /// that accepts a connectionString parameter.
        /// </remarks>
        protected virtual MongoDatabase GetDatabase(string collection   = null,
                                                    string database     = null,
                                                    string serverString = null)
        {
            var db = Context.GetDatabase(serverString, database);

            if (string.IsNullOrEmpty(collection))
            {
                collection = Pluralizer.Pluralize(typeof(TEntity).Name);
            }

            CollectionName = collection;

            return(db);
        }
Example #3
0
        /// <summary>
        /// Base constructor using default behavior loading context by
        /// connectionstring name.
        /// </summary>
        /// <param name="connectionString">Connection string name</param>
        public MongoDbBusinessBase(string collection = null, string database = null, string connectionString = null)
        {
            InitializeInternal();

            Context  = new TMongoContext();
            Database = GetDatabase(collection, database, connectionString);

            if (!Database.CollectionExists(CollectionName))
            {
                if (string.IsNullOrEmpty(CollectionName))
                {
                    CollectionName = Pluralizer.Pluralize(EntityType.Name);
                }

                Database.CreateCollection(CollectionName);
            }

            Initialize();
        }