Luis Valencia
Luis Valencia

Reputation: 33998

How to implement transient fault handling pattern with entity framework

I need to implement the following pattern: https://msdn.microsoft.com/en-us/library/dn589799.aspx

I read on a book that following:

If you are using Entity Framework 6 (EF 6), the retry logic for transient faults is built in to the framework. When your EF 6 model is in your project, you need to create a new class that derives from DbConfiguration and customizes the execution strategy in the constructor. EF 6 will look for classes that derive from DbConfiguration in your project and use them to provide resiliency. To set this, add a new Class file to your project and add using statements for System.Data.Entity and System.Data.Entity.SqlServer.

The code is as follows:

  public class EFConfiguration : DbConfiguration
    {
        public EFConfiguration()
        {
            this.SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
        }
    }

However I am not sure how to implement it on my code:

public class AppDataContext : DbContext
    {

        public AppDataContext() : base("AppDataContext")
        {
        }

        public DbSet<Module> Modulos { get; set; }

        public DbSet<Empresa> Empresas { get; set; }

        public DbSet<Entidad> Entidades { get; set; }

        public DbSet<Propiedad> Propiedades { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

    }

 public class AppDataContextInitializer : System.Data.Entity.DropCreateDatabaseAlways<AppDataContext>
    {
        protected override void Seed(AppDataContext context)
        {
            #region Seed Modules
                context.Modulos.Add(new Module() { Id = 1, ModuleName = "Contabilidad", FontAwesomeClass = "fa-ambulance" });
                context.Modulos.Add(new Module() { Id = 2, ModuleName = "Recursos Humanos", FontAwesomeClass = "fa-heartbeat" });
                context.Modulos.Add(new Module() { Id = 3, ModuleName = "Inventario", FontAwesomeClass = "fa-anchor" });
                context.Modulos.Add(new Module() { Id = 3, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" });
                context.Modulos.Add(new Module() { Id = 3, ModuleName = "Ventas", FontAwesomeClass = "fa-coffee" });
                context.Modulos.Add(new Module() { Id = 3, ModuleName = "Compras", FontAwesomeClass = "fa-calendar-o" });
                context.Modulos.Add(new Module() { Id = 3, ModuleName = "Cotizaciones", FontAwesomeClass = "fa-building" });
            #endregion

            #region Seed Empresas
            context.Empresas.Add(new Empresa() { Id = 1,
                Nombre = "XYA",
                NIT = "900854343",
                NombreRepresentanteLegal = "Carla Peresz",
                TelefonoRepresentanteLegal = "123",
                NombreContacto = " Esteban Andres",
                TelefonoContacto = "123"
                });
            #endregion

            #region Seed Entidades
            context.Entidades.Add(new Entidad()
            {
                Id = 1,
                Nombre = "Empresa",
                Propiedades = new List<Propiedad>()
                    {
                        new Propiedad()
                        {
                            Codigo="01",
                            Nombre="Twitter",
                            TipoDeDatos="Texto"
                        }
                    }
                });
            #endregion

            #region Seed Propiedad


            #endregion

            base.Seed(context);
        }
    }

Upvotes: 2

Views: 713

Answers (1)

vendettamit
vendettamit

Reputation: 14677

Here you're bit confused with resiliency and retry for consistency.

Connection Resiliency refers to the ability for EF to automatically retry any commands that fail due to these connection(Network unavailability) breaks.

I wish you may not want to interfere with this as EF does it very well. From the MSDN article that you mentioned, it seems like you want to use a caching pattern for your entities.

Well I can suggest you can try implementing UnitOfWork to keep your context CRUD operations at one place.

Then you can implement your cache in the UnitOfWork class and fetch/create/invalidate cache items based on type of operation request.

Make sure that cache-aside pattern doesn't give you 100% consistency. E.g. if an external operation changes the data in the DB then Cache would not be aware about that change.

Upvotes: 1

Related Questions