Skip to content

Instantly share code, notes, and snippets.

@jonnii
Created October 10, 2012 19:35
Show Gist options
  • Save jonnii/3867894 to your computer and use it in GitHub Desktop.
Save jonnii/3867894 to your computer and use it in GitHub Desktop.
mongo repo
/// <summary>
/// Repository interface
/// </summary>
public interface IMongoRepository
{
/// <summary>
/// Indicates whether or not the repository can connect to the underlying
/// mongo instance
/// </summary>
bool CanConnect { get; }
/// <summary>
/// Allows you to manage a mongo repository
/// </summary>
IMongoRepositoryManager Management { get; }
/// <summary>
/// Allows you to manage mongo migration
/// </summary>
IMigrationManager Migration { get; }
/// <summary>
/// Gets the underlying mongo adapter
/// </summary>
/// <returns>The underlying mongo adapter</returns>
IMongoAdapter GetMongoAdapter();
/// <summary>
/// Counts the number of items in this repository for the given type
/// </summary>
/// <typeparam name="T">The type to count</typeparam>
/// <returns>The number of items</returns>
long Count<T>();
/// <summary>
/// Finds a specific item
/// </summary>
/// <typeparam name="T">The type of item to find</typeparam>
/// <param name="id">The id of the item</param>
/// <returns>The item</returns>
T FindOne<T>(string id)
where T : IIdentifiable;
/// <summary>
/// Finds a specific item, with an objectid
/// </summary>
/// <typeparam name="T">The type of object to find</typeparam>
/// <param name="objectId">The object id</param>
/// <returns>The item</returns>
T FindOne<T>(ObjectId objectId)
where T : IIdentifiable;
/// <summary>
/// Find a specific item with a query.
/// </summary>
/// <typeparam name="T">The type of item to find</typeparam>
/// <param name="query">The query to use to find the item</param>
/// <returns>The query result or null.</returns>
T FindOne<T>(IQuery<T> query);
/// <summary>
/// Finds a specific item
/// </summary>
/// <typeparam name="T">The type of item to find</typeparam>
/// <typeparam name="TAs">The type to present the item as once it is retrieved</typeparam>
/// <param name="id">The id of the item</param>
/// <returns>The item</returns>
TAs FindOne<T, TAs>(string id)
where T : TAs, IIdentifiable;
/// <summary>
/// Finds a specific item, with an objectid
/// </summary>
/// <typeparam name="T">The type of object to find</typeparam>
/// <typeparam name="TAs">The type to present the item as once it is retrieved</typeparam>
/// <param name="objectId">The object id</param>
/// <returns>The item</returns>
TAs FindOne<T, TAs>(ObjectId objectId)
where T : TAs, IIdentifiable;
/// <summary>
/// Find a specific item with a query.
/// </summary>
/// <typeparam name="T">The type of item to find</typeparam>
/// <typeparam name="TAs">The type to present the item as once it is retrieved</typeparam>
/// <param name="query">The query to use to find the item</param>
/// <returns>The query result or null.</returns>
TAs FindOne<T, TAs>(IQuery<T> query)
where T : TAs;
/// <summary>
/// Finds everything for a given type
/// </summary>
/// <typeparam name="T">The type of object to find</typeparam>
/// <returns>A queryable of all the objects</returns>
IQueryable<T> FindAll<T>();
/// <summary>
/// Executes a query resulting in a list of items
/// </summary>
/// <typeparam name="T">The underlying type of the query</typeparam>
/// <param name="query">The query to execute</param>
/// <returns>All the items that match the given query</returns>
IQueryable<T> Execute<T>(IQuery<T> query);
/// <summary>
/// Executes a projection resulting in a list of items
/// </summary>
/// <typeparam name="TTo">The target type to project to</typeparam>
/// <param name="projection">The projection</param>
/// <returns>The projected items</returns>
IEnumerable<TTo> Execute<TTo>(IProjection<TTo> projection);
/// <summary>
/// Executes a query resulting in a single item
/// </summary>
/// <typeparam name="T">The underlying type of the query</typeparam>
/// <param name="query">The query to execute</param>
/// <returns>The item that matches the given query</returns>
T ExecuteOne<T>(IQuery<T> query);
/// <summary>
/// Executes a projection resulting in a single item
/// </summary>
/// <typeparam name="TTo">The target type to project to</typeparam>
/// <param name="projection">The projection to execute</param>
/// <returns>The item that matches the given query</returns>
TTo ExecuteOne<TTo>(IProjection<TTo> projection);
/// <summary>
/// Executes an update over a collection
/// </summary>
/// <typeparam name="T">The type of the collection</typeparam>
/// <typeparam name="TR">The return value of the update</typeparam>
/// <param name="update">The update to run</param>
/// <returns>The result of the update</returns>
TR Execute<T, TR>(IUpdate<T, TR> update);
/// <summary>
/// Executes an update over a collection that has no return value
/// </summary>
/// <typeparam name="T">The type of the collection</typeparam>
/// <param name="update">The update to run</param>
void Execute<T>(IUpdate<T> update);
/// <summary>
/// Saves an item
/// </summary>
/// <typeparam name="T">The type of the item to save</typeparam>
/// <param name="item">The item to save</param>
void Save<T>(T item)
where T : IIdentifiable;
/// <summary>
/// Deletes an item
/// </summary>
/// <typeparam name="T">The type of item to delete</typeparam>
/// <param name="item">The item to delete</param>
void Delete<T>(T item)
where T : IIdentifiable;
/// <summary>
/// Deletes an item from a collection
/// </summary>
/// <typeparam name="T">The type of the collection</typeparam>
/// <param name="id">The id of the item to delete</param>
void Delete<T>(string id)
where T : IIdentifiable;
/// <summary>
/// Deletes an item from a collection
/// </summary>
/// <typeparam name="T">The type of the collection</typeparam>
/// <param name="id">The id of the item to delete</param>
void Delete<T>(ObjectId id)
where T : IIdentifiable;
/// <summary>
/// Registers an observer with the mongo repository to enable lifecycle
/// events on objects that also require a reference to the mongo repository
/// </summary>
/// <param name="observer">The observer to register</param>
void RegisterObserver<TItem>(IObserver<TItem> observer)
where TItem : IIdentifiable;
/// <summary>
/// Uploads a stream with a file name into the mongo repository
/// </summary>
IMongoFileInfo UploadFile(string fileName, Stream contents);
/// <summary>
/// Uploads a stream with a file name with meta data into the mongo repository
/// </summary>
IMongoFileInfo UploadFile(string fileName, Stream contents, Dictionary<string, object> metaData);
/// <summary>
/// Downloads a file by id
/// </summary>
IMongoFileInfo FindFile(string id);
/// <summary>
/// Downloads a file by name
/// </summary>
IMongoFileInfo FindFileByName(string name);
/// <summary>
/// Deletes a file by id
/// </summary>
void DeleteFile(string id);
/// <summary>
/// Deletes a file by name
/// </summary>
void DeleteFileByName(string name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment