Hi guys, my problem here is that I need to read docs with different properties on the collection using an interface and right now it is giving me an error.
public interface IEntity {
public Guid Id { get; set; }
public Guid? TenantId { get; set; }
}
public interface IAccount : IEntity
{
public string Type { get; set; }
public string Name { get; set; }
public Currency Currency { get; set; }
public DateTime CreatedAt { get; set; }
public List<CurrencyBalance> CurrenciesBalance { get; set; }
public DateTime? UpdatedAt { get; set; }
}
public class CreditCard : IAccount
{
public Guid? AutomaticDebitAccountId { get; set; } = null;
public required decimal Limit { get; set; }
public override string Type { get; set; } = AccountType.CreditCard;
public DateTime ClosingDate { get; set; }
public DateTime ExpirationDate { get; set; }
}
public class DebitCard : IAccount
{
public required override string Type { get; set; } = AccountType.Bank;
}
On my repository I’m reading using the IAccount interface and on my code whenever I need I eventually cast to CreditCard or DebitCard , but it is giving me this error: Error during MainService.Domain.Interfaces.IAccount GetAsync: Unable to determine actual type of object to deserialize for interface type MainService.Domain.Interfaces.IAccount.
Welcome to the MongoDB Community Forums! What version of the .NET/C# driver are you using? Also how are you configuring the serializer to be used in this application? i.e Are you using Custom Serializers or Type Discriminators? Looking forward to more details, so we can point you in the right direction.
Hello Rashit, thanks for contacting, I’m using MongoDB.Driver version 3.1.0 and this is the only configuration lines that I have regarding MongoDb
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
ConventionRegistry.Register(
"Ignore",
new ConventionPack
{
new IgnoreIfNullConvention(true),
new IgnoreExtraElementsConvention(true),
},
t => true
);
I’m making a financial management system and I want to create accounts that represent the users bank accounts, credit cards, wallet, brokers, etc. So I made an IAccount interface which has the properties that all the accounts must have, but each account may have their own properties, just like the code I provided above, but I need something to represent all those classes of accounts.
Ah ok, sorry I did not understand your request, I’ve been able to find a solution, which is not using an interface at all and just a plain class, it can not even be abstract because abstract classes can not be instantiated. But the error I’m getting is this: Error during LogicaColossal.Mayom.MainService.Domain.Interfaces.IAccount GetByIdAsync: Serializer for LogicaColossal.Mayom.MainService.Domain.Interfaces.IAccount does not have a member named Id.
Which is not possible because IAccount inherits from IEntity which has an Id property. Here is my GetByIdAsync function (this is used for multi-tenants and is also a generic repository):
I think I’m still missing a little bit of context here, regarding exactly how your classes and collections are defined, but I think that the issue here is that you’ve created a parametrized collection using an interface, something like
You’re correct that in this case the best course of action is to use a base class, so in this case you could do something like:
[BsonKnownTypes(typeof(DebitCard), typeof(CreditCard))]
public class Card : IAccount
{
public virtual string Type { get; set; }
public Guid Id { get; set; }
public Guid? TenantId { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Balance { get; set; }
}
public class DebitCard : Card
{
public override string Type { get; set; } = "DebitCard";
}
public class CreditCard : Card
{
public override string Type { get; set; } = "CreditCard";
}
And then define your collection on the base class:
_collection = db.GetCollection<Card>("cards");
With this you should be able to filter the collection as you please. Remeber, when you’re working with polymorphic objects, you should always create the collection with the “most generic type” you have, and then eventually filter it (in this case Card). If you create your collection with a more specific type (like CreditCard in this case), you could get errors.
For instance: