问题描述
我想从模型中分离出EF层。
我需要一个EF Builder来将模型发送给它(我为mongodb找到了这段代码,但是我需要EF核心):
builder.AddMongo();
builder.AddMongoRepository< Cart>(购物车);
builder.AddMongoRepository< Customer>( Customers);
builder.AddMongoRepository< Product>(产品);
上面的代码在启动文件中。
我从 applicationsetting.json
文件传递参数,如您所见:
mongo:{
connectionString: mongodb:// localhost:27017,
database:客户服务,
seed:false
},
这是mongo示例代码:
公共静态类扩展
{
public static void AddMongo(此ContainerBuilder构建器)
{
builder.Register(context =>
{
var configuration = context.Resolve< IConfiguration>( );
var options = configuration.GetOptions< MongoDbOptions>( mongo);
返回选项;
})。SingleInstance();
builder.Register(context =>
{
var options = context.Resolve< MongoDbOptions>();
返回新的MongoClient(options .ConnectionString);
})。SingleInstance();
builder.Register(context =>
{
var options = context.Resolve< MongoDbOptions>();
var client = context.Resolve< MongoClient> ();
返回client.GetDatabase(options.Database);
})。InstancePerLifetimeScope();
builder.RegisterType< EFDbInitializer>()
.As< IEFDbInitializer>()
.InstancePerLifetimeScope();
builder.RegisterType< MongoDbSeeder>()
.As< IEFDbSeeder>()
.InstancePerLifetimeScope();
}
公共静态无效AddMongoRepository< TEntity>(此ContainerBuilder构建器,字符串collectionName)
其中TEntity:IIidentifiable
=> builder.Register(ctx => new EFRepository< TEntity>(ctx.Resolve< IMongoDatabase>(),collectionName))
.As< IMongoRepository< TEntity>>()
.InstancePerLifetimeScope();
}
我的问题是:是否有像mongo这样的EF解决方案?
mongoDb的所有代码都可用。
首先,我相信会有一些困惑:
- 是一个数据库
- 是一个ORM。它允许您访问存储中的数据
也就是说,您可以将EF Core与,例如MongoDB或SQL Server。
在实体(数据访问层)和业务模型之间希望有一个单独的层是一个好习惯,应该鼓励。
因为主题广泛,并且由于存在大量文档和教程以及该主题,所以我更愿意给您一些链接,而不是为您提供完整的体系结构。
您可以检查,以查看相关层及其应包含的内容。
此外,我建议您咨询,该版本提供了大量基于以下内容的干净架构: .net内核。
希望有帮助。
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.
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.
这篇关于EF核心中的持久性数据层,动态EF。将EF与模型分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!