Persistance datalayer in EF core ,dynamic EF. Separate EF from models

泪湿孤枕 提交于 2019-12-19 04:57:45

问题


I want to separate EF layer from my model .

I need a EF Builder to send my model to it like this(I found this code for mongodb but i need for EF core) :

        builder.AddMongo();
        builder.AddMongoRepository<Cart>("Carts");
        builder.AddMongoRepository<Customer>("Customers");
        builder.AddMongoRepository<Product>("Products");

The above code is inside startup file .

I pass the parameters from applicationsetting.json file as you can see :

 "mongo": {
    "connectionString": "mongodb://localhost:27017",
    "database": "customers-service",
    "seed": false
  },

Here is the mongo sample code :

public static class Extensions
    {
        public static void AddMongo(this ContainerBuilder builder)
        {
            builder.Register(context =>
            {
                var configuration = context.Resolve<IConfiguration>();
                var options = configuration.GetOptions<MongoDbOptions>("mongo");

                return options;
            }).SingleInstance();

            builder.Register(context =>
            {
                var options = context.Resolve<MongoDbOptions>();

                return new MongoClient(options.ConnectionString);
            }).SingleInstance();

            builder.Register(context =>
            {
                var options = context.Resolve<MongoDbOptions>();
                var client = context.Resolve<MongoClient>();
                return client.GetDatabase(options.Database);

            }).InstancePerLifetimeScope();

            builder.RegisterType<EFDbInitializer>()
                .As<IEFDbInitializer>()
                .InstancePerLifetimeScope();

            builder.RegisterType<MongoDbSeeder>()
                .As<IEFDbSeeder>()
                .InstancePerLifetimeScope();
        }

        public static void AddMongoRepository<TEntity>(this ContainerBuilder builder, string collectionName)
            where TEntity : IIdentifiable
            => builder.Register(ctx => new EFRepository<TEntity>(ctx.Resolve<IMongoDatabase>(), collectionName))
                .As<IMongoRepository<TEntity>>()
                .InstancePerLifetimeScope();
    }

My question is :is there any solution for EF like mongo ?

Every part of code for mongoDb is available.


回答1:


First of all, I believe there is a little confusion :

  • MongoDB is a database
  • Entity Core is an ORM. It allows you to access to data in a storage

That said, you can use EF Core with several data storages, such as MongoDB or SQL Server for instance.

To want a separated layer between your entities (Data Access Layer) and your business model is a good practice, and should be encouraged.

Because the topic is wide, and because lot of documentations and tutorials exists and the subject, I prefer to give you some links rather than to give you a complete architecture.

You can check this documentation from Microsoft to see the related layers, and what they should contains.

Also, I advise you to consult this github repo, which provide tons of clean architectures based on .net core.

Feel free to dig a bit into those links, they are providing a lot of precious informations.

Hope it helps.



来源:https://stackoverflow.com/questions/56273847/persistance-datalayer-in-ef-core-dynamic-ef-separate-ef-from-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!