简体   繁体   中英

How to deserialize readonly fields using C# Driver for MongoDB?

I have a class which contains readonly private fields

public sealed class Company : AggregateRoot
{
    private readonly List<TimeSeriesImportInfo> _executedImportInfo = new(); 
    private readonly List<StockPrice> _stockPrices = new(); 

    public Company(
        Guid id,
        string name,
        string symbol)
            : base(id)
    {
        Name = name;
        Symbol = symbol;
    }

    public string Name { get; }
    public string Symbol { get; }
    public IReadOnlyCollection<TimeSeriesImportInfo> ExecutedImportInfo => _executedImportInfo.AsReadOnly();
    public IReadOnlyCollection<StockPrice> StockPrices => _stockPrices.AsReadOnly();

    public void AddExecutedImportInfo(DateTime month)
    {
        var timeSeriesImportInfo = new TimeSeriesImportInfo(month);
        var hasAlreadyBeenHandled = ExecutedImportInfo.Contains(timeSeriesImportInfo);

        if (hasAlreadyBeenHandled)
        {
            throw new ImportExecutedBeforeDomainException(Id, month);
        }

        _executedImportInfo.Add(timeSeriesImportInfo);

        var importExecutedEvent = new ImportExecutedDomainEvent(Id, timeSeriesImportInfo.Month);
        Enqueue(importExecutedEvent);
    }

    public void AddStockPrices(ICollection<StockPrice> stockPrices)
    {
        var hasAlreadyBeenImported = stockPrices.Any(newStockPrice =>
            _stockPrices.Contains(newStockPrice));

        if (hasAlreadyBeenImported)
        {
            throw new StockPriceAlreadyImportedDomainException(Id, stockPrices);
        }

        _stockPrices.AddRange(stockPrices);

        var stockPricesAddedEvent = new StockPricesAddedDomainEvent(Id, stockPrices);
        Enqueue(stockPricesAddedEvent);
    }
}

I have configured the mapping for the company using the following method:

public static class Mapping
{
    public static IServiceCollection AddMappings(this IServiceCollection services)
    {
        BsonClassMap.RegisterClassMap<Company>(classMap =>
        {
            classMap.AutoMap();
            classMap.MapField("_executedImportInfo").SetElementName(nameof(Company.ExecutedImportInfo));
            classMap.MapField("_stockPrices").SetElementName(nameof(Company.StockPrices));
        });

        return services;
    }
}

The values of those private readonly fields are correctly saved to the database. However when im trying to fetch company object - those private fields can't be deserialized properly, because after the fetch they have default values (although they are persisted in the db). If i removed the "readonly" keyword from them - everything works correctly. I would like to avoid removing the readonly key and would like to not modify the Company class. How to achieve that in C#? Thanks for any help, regards.

不支持,在调用 ctor 后反序列化期间将跳过只读字段。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM