Deleting fields from read models and events

Hello,

Is there any way to delete some old unnecessary fields from read model?
In past some fields were added to read models and also this fields are passed in some command/events, but now we dont need them (and dont want them) so we want get rid of them.
What is a way to do that without clearing database and keep system reply safe?

1 Like

If you’re using our normal IReadModelRepository backed by Mongo you will get errors when you delete properties on your read-models that exist on documents in the database. This is probably what you are experiencing.

To move beyond this I usually set up a convention for the MongoDB Serialization like this (Here I am using the Customer read-model in the Read -project and the CustomerID from Concepts to tell the convention which Assembly to ignore extra properties for):

using MongoDB.Bson.Serialization.Conventions;
using Read.Customers;
using Concepts.Customers;

namespace Core
{
    public static class ReadModelSerializationConventions
    {
        public static void IgnoreExtraElementsWhenDeserializingReadModels()
        {
            ConventionRegistry
            .Register(
                "ignore extra elements",
                new ConventionPack
                {
                    new IgnoreExtraElementsConvention(true),
                },
                type => type.Assembly == typeof(Customer).Assembly
                    || type.Assembly == typeof(CustomerId).Assembly
            );
        }
    }
}

You use this in your Startup.cs by calling the static method in the Configure -method. One example I have is this one:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                });
            }

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedProto
            });

            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseSession();
            app.UseMvc();
            app.UseDolittle();

            app.RunAsSinglePageApplicationWithoutCaching(); 

            SetStaticDependencies(app.ApplicationServices);

            ReadModelSerializationConventions
                .IgnoreExtraElementsWhenDeserializingReadModels();
        }

I hope that helps!

-Tomas

2 Likes