Skip to content

Commit

Permalink
* Trunk->Dev. *CRAP*, working on wrong branch
Browse files Browse the repository at this point in the history
  • Loading branch information
RobThree committed Sep 11, 2013
1 parent 98795ec commit 307a319
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 94 deletions.
100 changes: 100 additions & 0 deletions MongoRepository/dev/MongoRepository/ClassDiagram.cd
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Class Name="MongoRepository.CollectionName" Collapsed="true">
<Position X="7.75" Y="0.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>CollectionName.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="MongoRepository.Entity" Collapsed="true" BaseTypeListCollapsed="true">
<Position X="9.5" Y="0.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>Entity.cs</FileName>
</TypeIdentifier>
<Lollipop Position="0.2" Collapsed="true" />
</Class>
<Class Name="MongoRepository.MongoRepository&lt;T, U&gt;" Collapsed="true">
<Position X="4.5" Y="0.5" Width="2.25" />
<TypeIdentifier>
<HashCode>ADYABAAAAAAEQBAgAEAAAAQAAAEAAAAQAAAAAAEIAoA=</HashCode>
<FileName>Repository\MongoRepository.cs</FileName>
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Class Name="MongoRepository.MongoRepository&lt;T&gt;" Collapsed="true">
<Position X="4.5" Y="1.75" Width="2.25" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>Repository\MongoRepository.cs</FileName>
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Class Name="MongoRepository.MongoRepositoryManager&lt;T, U&gt;" Collapsed="true">
<Position X="0.5" Y="0.5" Width="2.25" />
<TypeIdentifier>
<HashCode>AAAAAAAAYACQACQBAECEAAQAAAAQAAAAAAgEAgUBABA=</HashCode>
<FileName>RepositoryManager\MongoRepositoryManager.cs</FileName>
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Class Name="MongoRepository.MongoRepositoryManager&lt;T&gt;" Collapsed="true">
<Position X="0.5" Y="1.75" Width="2.25" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>RepositoryManager\MongoRepositoryManager.cs</FileName>
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Class Name="MongoRepository.Util&lt;U&gt;" Collapsed="true">
<Position X="7.75" Y="1.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAGAAAAAAAAJBAAAAAACAAAAAAAIABAAAAAAAAAA=</HashCode>
<FileName>Util.cs</FileName>
</TypeIdentifier>
</Class>
<Interface Name="MongoRepository.IEntity" Collapsed="true">
<Position X="0.5" Y="7.75" Width="2.25" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>IEntity.cs</FileName>
</TypeIdentifier>
</Interface>
<Interface Name="MongoRepository.IEntity&lt;T&gt;" Collapsed="true">
<Position X="0.5" Y="6.5" Width="2.25" />
<TypeIdentifier>
<HashCode>AAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>IEntity.cs</FileName>
</TypeIdentifier>
</Interface>
<Interface Name="MongoRepository.IRepository&lt;T, U&gt;" Collapsed="true">
<Position X="4.5" Y="3.5" Width="2.25" />
<TypeIdentifier>
<HashCode>ABYAAAAAAAAAQAAgAAAAAAQAAAEAAAAAAAAAAAEIAoA=</HashCode>
<FileName>Repository\IRepository.cs</FileName>
</TypeIdentifier>
</Interface>
<Interface Name="MongoRepository.IRepository&lt;T&gt;" Collapsed="true">
<Position X="4.5" Y="5" Width="2.25" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>Repository\IRepository.cs</FileName>
</TypeIdentifier>
</Interface>
<Interface Name="MongoRepository.IRepositoryManager&lt;T, U&gt;" Collapsed="true">
<Position X="0.5" Y="3.5" Width="2.25" />
<TypeIdentifier>
<HashCode>AAAAAAAAYACQACQBAACEAAQAAAAQAAAAAAgEAgUBABA=</HashCode>
<FileName>RepositoryManager\IRepositoryManager.cs</FileName>
</TypeIdentifier>
</Interface>
<Interface Name="MongoRepository.IRepositoryManager&lt;T&gt;" Collapsed="true">
<Position X="0.5" Y="4.75" Width="2.25" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>RepositoryManager\IRepositoryManager.cs</FileName>
</TypeIdentifier>
</Interface>
<Font Name="Segoe UI" Size="9" />
</ClassDiagram>
2 changes: 1 addition & 1 deletion MongoRepository/dev/MongoRepository/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[DataContract]
[Serializable]
[BsonIgnoreExtraElements(Inherited = true)]
public abstract class Entity : IEntity
public abstract class Entity : IEntity<string>
{
/// <summary>
/// Gets or sets the id for this object (the primary record for an entity).
Expand Down
13 changes: 10 additions & 3 deletions MongoRepository/dev/MongoRepository/IEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ namespace MongoRepository
using MongoDB.Bson.Serialization.Attributes;

/// <summary>
/// Entity interface.
/// Generic Entity interface.
/// </summary>
public interface IEntity
public interface IEntity<T>
{
/// <summary>
/// Gets or sets the Id of the Entity.
/// </summary>
/// <value>Id of the Entity.</value>
[BsonId]
string Id { get; set; }
T Id { get; set; }
}

/// <summary>
/// "Default" Entity interface.
/// </summary>
public interface IEntity : IEntity<string>
{
}
}
6 changes: 4 additions & 2 deletions MongoRepository/dev/MongoRepository/MongoRepository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="MongoDB.Bson, Version=1.8.2.34, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\mongocsharpdriver.1.8.2\lib\net35\MongoDB.Bson.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="MongoDB.Driver, Version=1.8.2.34, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\mongocsharpdriver.1.8.2\lib\net35\MongoDB.Driver.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand All @@ -68,6 +68,8 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config.transform" />
<None Include="ClassDiagram.cd" />
<None Include="packages.config" />
<None Include="web.config.transform" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
25 changes: 13 additions & 12 deletions MongoRepository/dev/MongoRepository/Repository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
/// IRepository definition.
/// </summary>
/// <typeparam name="T">The type contained in the repository.</typeparam>
public interface IRepository<T> : IQueryable<T>
where T : IEntity
//TODO: Update documentation
public interface IRepository<T, U> : IQueryable<T>
where T : IEntity<U>
{
/// <summary>
/// Gets the Mongo collection (to perform advanced operations).
Expand All @@ -30,15 +31,8 @@ public interface IRepository<T> : IQueryable<T>
/// </summary>
/// <param name="id">The string representing the ObjectId of the entity to retrieve.</param>
/// <returns>The Entity T.</returns>
T GetById(string id);

/// <summary>
/// Returns a single T by the given criteria.
/// </summary>
/// <param name="criteria">The expression.</param>
/// <returns>A single T matching the criteria.</returns>
[Obsolete("The repository itself now implements IQueryable<T>; use .Where(criteria).FirstOrDefault() or .Where(criteria).Single() etc.")]
T GetSingle(Expression<Func<T, bool>> criteria);
//TODO: Update documentation
T GetById(U id);

/// <summary>
/// Adds the new entity in the repository.
Expand Down Expand Up @@ -70,7 +64,8 @@ public interface IRepository<T> : IQueryable<T>
/// Deletes an entity from the repository by its id.
/// </summary>
/// <param name="id">The string representation of the entity's id.</param>
void Delete(string id);
//TODO: Update documentation
void Delete(U id);

/// <summary>
/// Deletes the given entity.
Expand Down Expand Up @@ -132,4 +127,10 @@ public interface IRepository<T> : IQueryable<T>
/// </remarks>
void RequestDone();
}

//TODO: Add documentation
public interface IRepository<T> : IRepository<T, string>
where T : IEntity<string>
{
}
}
86 changes: 54 additions & 32 deletions MongoRepository/dev/MongoRepository/Repository/MongoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
/// Deals with entities in MongoDb.
/// </summary>
/// <typeparam name="T">The type contained in the repository.</typeparam>
public class MongoRepository<T> : IRepository<T>
where T : IEntity
//TODO: Update documentation
public class MongoRepository<T, U> : IRepository<T, U>
where T : IEntity<U>
{
/// <summary>
/// MongoCollection field.
Expand All @@ -27,7 +28,7 @@ public class MongoRepository<T> : IRepository<T>
/// </summary>
/// <remarks>Default constructor defaults to "MongoServerSettings" key for connectionstring.</remarks>
public MongoRepository()
: this(Util.GetDefaultConnectionString())
: this(Util<U>.GetDefaultConnectionString())
{
}

Expand All @@ -37,7 +38,17 @@ public MongoRepository()
/// <param name="connectionString">Connectionstring to use for connecting to MongoDB.</param>
public MongoRepository(string connectionString)
{
this.collection = Util.GetCollectionFromConnectionString<T>(connectionString);
this.collection = Util<U>.GetCollectionFromConnectionString<T>(connectionString);
}

/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="connectionString">Connectionstring to use for connecting to MongoDB.</param>
/// <param name="collectionName">The name of the collection to use.</param>
public MongoRepository(string connectionString, string collectionName)
{
this.collection = Util<U>.GetCollectionFromConnectionString<T>(connectionString, collectionName);
}

/// <summary>
Expand All @@ -46,7 +57,17 @@ public MongoRepository(string connectionString)
/// <param name="url">Url to use for connecting to MongoDB.</param>
public MongoRepository(MongoUrl url)
{
this.collection = Util.GetCollectionFromUrl<T>(url);
this.collection = Util<U>.GetCollectionFromUrl<T>(url);
}

/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="url">Url to use for connecting to MongoDB.</param>
/// <param name="collectionName">The name of the collection to use.</param>
public MongoRepository(MongoUrl url, string collectionName)
{
this.collection = Util<U>.GetCollectionFromUrl<T>(url, collectionName);
}

/// <summary>
Expand All @@ -71,35 +92,15 @@ public MongoCollection<T> Collection
/// </summary>
/// <param name="id">The string representing the ObjectId of the entity to retrieve.</param>
/// <returns>The Entity T.</returns>
public T GetById(string id)
//TODO: Update documentation
public T GetById(U id)
{
if (typeof(T).IsSubclassOf(typeof(Entity)))
{
return this.GetById(new ObjectId(id));
return this.collection.FindOneByIdAs<T>(new ObjectId(id as string));
}

return this.collection.FindOneByIdAs<T>(id);
}

/// <summary>
/// Returns the T by its given ObjectId.
/// </summary>
/// <param name="id">The ObjectId of the entity to retrieve.</param>
/// <returns>The Entity T.</returns>
public T GetById(ObjectId id)
{
return this.collection.FindOneByIdAs<T>(id);
}

/// <summary>
/// Returns a single T by the given criteria.
/// </summary>
/// <param name="criteria">The expression.</param>
/// <returns>A single T matching the criteria.</returns>
[Obsolete("The repository itself now implements IQueryable<T>; use .Where(criteria).FirstOrDefault() or .Where(criteria).Single() etc.")]
public T GetSingle(Expression<Func<T, bool>> criteria)
{
return this.collection.AsQueryable<T>().Where(criteria).FirstOrDefault();
return this.collection.FindOneByIdAs<T>(BsonValue.Create(id));
}

/// <summary>
Expand Down Expand Up @@ -151,15 +152,16 @@ public void Update(IEnumerable<T> entities)
/// Deletes an entity from the repository by its id.
/// </summary>
/// <param name="id">The string representation of the entity's id.</param>
public void Delete(string id)
//TODO: Update documentation
public void Delete(U id)
{
if (typeof(T).IsSubclassOf(typeof(Entity)))
{
this.Delete(new ObjectId(id));
this.collection.Remove(Query.EQ("_id", new ObjectId(id as string)));
}
else
{
this.collection.Remove(Query.EQ("_id", id));
this.collection.Remove(Query.EQ("_id", BsonValue.Create(id)));
}
}

Expand Down Expand Up @@ -309,4 +311,24 @@ public IQueryProvider Provider
}
#endregion
}

//TODO: Update documentation
public class MongoRepository<T> : MongoRepository<T, string>, IRepository<T, string>
where T : IEntity<string>
{
public MongoRepository()
: base() { }

public MongoRepository(MongoUrl url)
: base(url) { }

public MongoRepository(MongoUrl url, string collectionName)
: base(url, collectionName) { }

public MongoRepository(string connectionString)
: base(connectionString) { }

public MongoRepository(string connectionString, string collectionName)
: base(connectionString, collectionName) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
/// IRepositoryManager definition.
/// </summary>
/// <typeparam name="T">The type contained in the repository to manage.</typeparam>
public interface IRepositoryManager<T> where T : IEntity
//TODO: Update documentation
public interface IRepositoryManager<T, U>
where T : IEntity<U>
{
/// <summary>
/// Gets a value indicating whether the collection already exists.
Expand Down Expand Up @@ -124,16 +126,6 @@ public interface IRepositoryManager<T> where T : IEntity
/// </summary>
void ReIndex();

/// <summary>
/// Removes all entries for this repository in the index cache used by EnsureIndex.
/// </summary>
/// <remarks>
/// Call this method when you know (or suspect) that a process other than this one may
/// have dropped one or more indexes.
/// </remarks>
[Obsolete("mongo-csharp-driver 1.8.2.34 doesn't use index caches anymore (see https://jira.mongodb.org/browse/CSHARP-736)")]
void ResetIndexCache();

/// <summary>
/// Gets the total size for the repository (data + indexes).
/// </summary>
Expand Down Expand Up @@ -166,4 +158,10 @@ public interface IRepositoryManager<T> where T : IEntity
/// <returns>Returns the indexes for this repository.</returns>
GetIndexesResult GetIndexes();
}

//TODO: Update documentation
public interface IRepositoryManager<T> : IRepositoryManager<T, string>
where T : IEntity<string>
{
}
}
Loading

0 comments on commit 307a319

Please sign in to comment.