Giter Site home page Giter Site logo

modular-ef-core's Introduction

Modular EF

An attempt at creating a way to ensure that we can re use our domain modules while at the same time ensuring Ef Core functionalities such as transaction, relation, and migrations, to name a few, work normally.

Detail

This solution contains two projects:

  1. Inventory [Class Libarry]
  2. ModularEf [Asp.Net Web App]

The inventory module is added to act as a proof of concept that we can modularize our domain with Ef.

The web app uses Asp.Net Core Identity for migration, just to ensure that the approach works with existing technologies.

Approach

Each of the modules that wants to use Ef Core has to have a class in the following format.

public static class EntityRegisterer
{
    public static void UseInventory(this ModelBuilder builder)
    {
        builder.Entity<Item>().ToTable("tbl_item");
    }
}

The name of the class and the method can be anything. Pick a name that makes sense.

This class adds an extension method to the ModelBuilder class.

So, any entity registration and entity configuration we need to do for this particular module, we can do here.

In the example above, we are adding an entity Item and then mapping it to Table tbl_item.

In our actual context class, which usually resides in its own project or in the web app (Recommended for this approach), we simply call the extensiton method in the OnModelCreating method to use configurations from a certain module.

public class BaseContext : IdentityDbContext<IdentityUser>
{
    public BaseContext(DbContextOptions<BaseContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.UseInventory();
    }
}

modelBuilder.UseInventory() adds the inventory module configuration. This will be picked up by our application as well as the tools such as Ef Core Migrations.

If we want to add Account, we simply call the extension method of Account module.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.UseInventory();
+   modelBuilder.UseAccount();
}

If two of your modules use the same extension method name, we have a couple of ways that can be resolved.

  1. Rename one of the method name

    This is the simplest and straight forward approach. We simply rename one of the methods so that there is not name clashing. However, this approach might not be a viable option if the module is used by multiple other projects.

  2. Call the extension method directly

    Since extension methods are simply static methods, we can call them directly by providing the model builder instance.

    modelBuilder.UseReport(); // Inventory Report
    - modelBuilder.UseReport(); // Account Report
    + Account.EntityRegisterer.UseReport(modelBuilder);

    This avoids the name clashing.

    Happy coding

modular-ef-core's People

Contributors

code-sujan avatar rehmatfalcon avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

code-sujan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.