Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Entity : IEntity
- {
- [BsonId]
- [BsonRepresentation(BsonType.ObjectId)]
- public string Id { get; set; }
- [BsonIgnore]
- public abstract string CollectionName { get; }
- }
- public class Car
- {
- public string Name {get; set;}
- public override string CollectionName { get; } = "Cars";
- }
- public class MongoDbContext : IMongoDbContext
- {
- private readonly IMongoDatabase _database;
- public MongoDbContext(IMongoDatabase database)
- {
- _database = database;
- }
- public IMongoCollection<T> GetCollection<T>() where T : Entity, new()
- {
- return _database.GetCollection<T>(new T().CollectionName);
- }
- }
- mongoDbContext.GetCollection<Car>();
- [BsonKnownTypes(...)]
- public class MainClass: Entity, IMainClass
- {
- public override string CollectionName { get; } = "SomeCustom";
- }
- [BsonDiscriminator("Sub1")]
- public class Sub1: MainClass, ICommonForSeveralEntities
- {}
- [BsonDiscriminator("Sub2")]
- public class Sub2: MainClass, ICommonForSeveralEntities
- {}
- mongoDbContext.GetCollection<ICommonForSeveralEntities>();
Add Comment
Please, Sign In to add comment