C# Marten:不在构造函数/工厂调用中定义模式内容(如索引等)来创建DocumentStore
我刚刚开始测试2.9,到目前为止我很喜欢它。但是,我不确定是否遵循DocumentStore.For方法。例如,在我的Marten数据库处理程序中,我可以编写:C# Marten:不在构造函数/工厂调用中定义模式内容(如索引等)来创建DocumentStore,c#,database-schema,marten,C#,Database Schema,Marten,我刚刚开始测试2.9,到目前为止我很喜欢它。但是,我不确定是否遵循DocumentStore.For方法。例如,在我的Marten数据库处理程序中,我可以编写: public MartenDbHandler() { store = DocumentStore.For(_ => { _.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate; _.C
public MartenDbHandler()
{
store = DocumentStore.For(_ =>
{
_.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate;
_.Connection("host=localhost;database=marten;password=root;username=postgres");
_.Schema.For<Customer>().Index(x => x.PopulationRegistryNumber);
});
}
然后在那些类中实现模式,但这不起作用,因为我不能使用DocumentStore设置元。保持简单。文档存储应该在应用程序中有一个实例,您可以在构建过程中定义模式属性。不需要抽象商店 一种方法是您可以创建自己的DocumentStore实现。您可以参考源代码中的测试文档存储类 更新:
您可以在这里找到示例我设法用了一种非常好的方法使其更具动态性,而不是全部用于DocumentStore的构建 请参阅下面的代码。想法很简单: 1单独创建StoreOptions 2在创建DocumentStore之前,运行方法,通过反射查找将添加表元数据的特定类型的所有类 3创建文档库
public MartenDbHandler()
{
StoreOptions so = new StoreOptions();
so.Connection("host=localhost;database=marten;password=root;username=postgres");
so.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate;
SetTableMeta(so);
store = new DocumentStore(so);
}
private void SetTableMeta(StoreOptions storeOptions)
{
// We get the current assembly through the current class
var currentAssembly = Assembly.GetExecutingAssembly();
// we filter the defined classes according to the interfaces they implement
var stuff = currentAssembly.DefinedTypes.Where(type => type.IsSubclassOf(typeof(MartenTableMetaDataBase))).ToList();
foreach (Type type in stuff)
{
IMartenTableMetaData temp = (IMartenTableMetaData)Activator.CreateInstance(type);
temp.SetTableMetaData(storeOptions);
}
OnLogEvent?.Invoke(this, $"{stuff.Count} table meta data initialized");
}
IMartenTableMetaData是IMartenTableMetaData接口的基类。在下面的示例中,没有使用基类,但我通常认为有一个基类是很好的,我使用了与另一个ORM类似的方法,在这里我实际使用基类。但是,如果您不使用基类ofc,则可以删除它
internal abstract class MartenTableMetaDataBase : IMartenTableMetaData
{
public void SetTableMetaData(StoreOptions storeOptions)
{
SetSpecificTableMetaData(storeOptions);
}
protected abstract void SetSpecificTableMetaData(StoreOptions storeOptions);
}
以及界面:
public interface IMartenTableMetaData
{
void SetTableMetaData(StoreOptions storeOptions);
}
因此,我现在可以为我想要添加元数据的每种类型创建一个类,如下所示:
internal class MartenTableMetaDataCustomer : MartenTableMetaDataBase
{
protected override void SetSpecificTableMetaData(StoreOptions storeOptions)
{
storeOptions.Schema.For<Customer>().Index(x => x.Muni);
}
}
或
等等
这将使Marten DB处理程序保持干净,并将元数据划分为特定的类,以确保可读性、清晰性和所有这些=
希望能有所帮助。谢谢您的帮助。我来看看测试文件。然而,我不得不说,我不同意这是保持它的简单,有它的所有建设。那绝对是个错误的地方。它使一个不应该真正保留一个冗长清单的班级变得臃肿不堪;最好有一个接口和实现它们的类来确定meta。但要明确的是:除了创建我自己的DocumentStore之外,没有其他方法了?在子类上,您可以看看PR 1066,它有关于此的示例测试。对不起,PR 1066是什么意思?PR=pull请求。你可以在Marten的github网站上搜索它
internal class MartenTableMetaDataCustomer : MartenTableMetaDataBase
{
protected override void SetSpecificTableMetaData(StoreOptions storeOptions)
{
storeOptions.Schema.For<Customer>().Index(x => x.Muni);
}
}
internal class MartenTableMetaDataDriver : MartenTableMetaDataBase
{
protected override void SetSpecificTableMetaData(StoreOptions storeOptions)
{
storeOptions.Schema.For<Driver>().Index(x => x.Username);
}
}