C
C#ā€¢2y 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ā€¢2y 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ā€¢2y 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ā€¢2y ago
where/what is _DBSet
RDasher
RDasherOPā€¢2y 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ā€¢2y ago
_DBSet = MongoDbContext.Database.GetCollection<T>(nameof(T));
of course... you want typeof(T).Name
RDasher
RDasherOPā€¢2y 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ā€¢2y ago
it wont work why arent you trying wht I gave
RDasher
RDasherOPā€¢2y ago
I did, that gives me an error There is no Name in the results of nameof(T)
phaseshift
phaseshiftā€¢2y ago
please read
RDasher
RDasherOPā€¢2y ago
phaseshift
phaseshiftā€¢2y ago
re-read
RDasher
RDasherOPā€¢2y ago
I am Dumb @phaseshift Thank you very much, that was it, thanks! !close
phaseshift
phaseshiftā€¢2y ago
/close šŸ‘ np
RDasher
RDasherOPā€¢2y ago
Thanks
phaseshift
phaseshiftā€¢2y ago
(you have to type it, though)
RDasher
RDasherOPā€¢2y ago
/close It says application not responding pepeCEASE
phaseshift
phaseshiftā€¢2y ago
lol, nm. You tried!
RDasher
RDasherOPā€¢2y ago
Neither text not command, guess I'll just leave it
Jana ā™”
Jana ā™”ā€¢2y 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ā€¢2y ago
@Jana ā™” dont cross post
Jana ā™”
Jana ā™”ā€¢2y ago
okay .. still can help me plss ??
phaseshift
phaseshiftā€¢2y ago
stop talking about your issue in this thread
Accord
Accordā€¢2y 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.
Want results from more Discord servers?
Add your server