C
C#•3y ago
RDasher

āœ… MongoDB Document Name

So I'm learning MongoDB.Driver in DotNet, and I'm figuring things out. But I got stuck on this little roadblock. So I've created dynamic methods inheriting from a base model. And somewhere here, I ended up having my documents insert under the document T. Now naturally, I fixing this is not hard, I simply create insert methods with the Model Type. But I do wanna know if this is possible with a generic Insert method. tl;dr: I want to use a generic insert method to insert all my documents, but currently they all insert under the name T. I know how this can be fixed, but am asking if this is possible with a generic insert method with the Type T The generic method itself:
public async Task InsertAsync(T Entity)
{

switch (Entity)
{
case LoggedEntity _LoggedEntity:
_LoggedEntity.CreatedBy = "";
_LoggedEntity.CreatedOnUtc = DateTime.UtcNow;
break;
default:
break;
}

await _DBSet.InsertOneAsync(Entity);
}
public async Task InsertAsync(T Entity)
{

switch (Entity)
{
case LoggedEntity _LoggedEntity:
_LoggedEntity.CreatedBy = "";
_LoggedEntity.CreatedOnUtc = DateTime.UtcNow;
break;
default:
break;
}

await _DBSet.InsertOneAsync(Entity);
}
Where it is called:
public async Task InsertCrewAsync(CrewModel crewModel)
{
await _CrewRepository.InsertAsync(crewModel);
}
public async Task InsertCrewAsync(CrewModel crewModel)
{
await _CrewRepository.InsertAsync(crewModel);
}
23 Replies
phaseshift
phaseshift•3y ago
ut currently they all insert under the name T.
show where you get the collection ie how is that T even made?!
RDasher
RDasherOP•3y ago
public class Repository<T> : IRepository<T> where T : BaseEntity
{
public async Task InsertAsync(T Entity)
{

switch (Entity)
{
case LoggedEntity _LoggedEntity:
_LoggedEntity.CreatedBy = "";
_LoggedEntity.CreatedOnUtc = DateTime.UtcNow;
break;
default:
break;
}

await _DBSet.InsertOneAsync(Entity);
}
}
public class Repository<T> : IRepository<T> where T : BaseEntity
{
public async Task InsertAsync(T Entity)
{

switch (Entity)
{
case LoggedEntity _LoggedEntity:
_LoggedEntity.CreatedBy = "";
_LoggedEntity.CreatedOnUtc = DateTime.UtcNow;
break;
default:
break;
}

await _DBSet.InsertOneAsync(Entity);
}
}
And the CrewModel Inherits from BaseEntity
phaseshift
phaseshift•3y ago
where/what is _DBSet
RDasher
RDasherOP•3y ago
public class Repository<T> : IRepository<T> where T : BaseEntity
{

public Repository()
{
MongoDbContext.EstablishMongoDbConnection();
_DBSet = MongoDbContext.Database.GetCollection<T>(nameof(T));
}

private IMongoCollection<T> _DBSet { get; set; }

public IMongoCollection<T> DBSet { get { return _DBSet; } }
}
public class Repository<T> : IRepository<T> where T : BaseEntity
{

public Repository()
{
MongoDbContext.EstablishMongoDbConnection();
_DBSet = MongoDbContext.Database.GetCollection<T>(nameof(T));
}

private IMongoCollection<T> _DBSet { get; set; }

public IMongoCollection<T> DBSet { get { return _DBSet; } }
}
Right above the insert method
phaseshift
phaseshift•3y ago
_DBSet = MongoDbContext.Database.GetCollection<T>(nameof(T));
of course... you want typeof(T).Name
RDasher
RDasherOP•3y ago
Ah, I see the issue Haha, thank you very much, let me try that real quick
nameof(T).GetType().Name
nameof(T).GetType().Name
I'm trying this I'll let you know if this works
phaseshift
phaseshift•3y ago
it wont work why arent you trying wht I gave
RDasher
RDasherOP•3y ago
I did, that gives me an error There is no Name in the results of nameof(T)
phaseshift
phaseshift•3y ago
please read
RDasher
RDasherOP•3y ago
phaseshift
phaseshift•3y ago
re-read
RDasher
RDasherOP•3y ago
I am Dumb @phaseshift Thank you very much, that was it, thanks! !close
phaseshift
phaseshift•3y ago
/close šŸ‘ np
RDasher
RDasherOP•3y ago
Thanks
phaseshift
phaseshift•3y ago
(you have to type it, though)
RDasher
RDasherOP•3y ago
/close It says application not responding pepeCEASE
phaseshift
phaseshift•3y ago
lol, nm. You tried!
RDasher
RDasherOP•3y ago
Neither text not command, guess I'll just leave it
Jana ā™”
Jana ♔•3y ago
guys my form contain label10 ! the text of the label : Username : (Login !)
string Usertxt = System.IO.File.ReadAllText(@"C:
TrexLauncher Minecraft Bedrock/LoginSystem/Username.TL"
if (File.Exists(Usertxt))
{
label10.Text = Usertxt;
}
string Usertxt = System.IO.File.ReadAllText(@"C:
TrexLauncher Minecraft Bedrock/LoginSystem/Username.TL"
if (File.Exists(Usertxt))
{
label10.Text = Usertxt;
}
after this code the label.text dont change ! please help me guys ! Can someone please help 😦
phaseshift
phaseshift•3y ago
@Jana ā™” dont cross post
Jana ā™”
Jana ♔•3y ago
okay .. still can help me plss ??
phaseshift
phaseshift•3y ago
stop talking about your issue in this thread
Accord
Accord•3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?