Marten:定义模式内容(如索引等),而不是在构造函数/工厂调用中创建DocumentStore

问题描述 投票:0回答:2

我刚刚开始测试Marten(2.9),到目前为止我很喜欢它。但是,我不确定我是否遵循DocumentStore.For方法。例如,在Marten的“dbhandler”中,我可以写:

    public MartenDbHandler()
    {
        store = DocumentStore.For(_ =>
        {
            _.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate;
            _.Connection("host=localhost;database=marten;password=root;username=postgres");
            _.Schema.For<Customer>().Index(x => x.PopulationRegistryNumber);
        });
    }

但很自然地,当我初始化数据库并提供连接字符串时,我不希望拥有所有模式代码。

所以我想,也许我可以传递store变量,并做同样的事情,但然后For事物不存在:

enter image description here

...而且我还没有找到以任何其他方式设置Schema的方法。

我真正想做的是拥有一个接口,当我启动我的应用程序时,动态加载和执行(通过反射),处理这些事情,如IMartenMetaData看起来像:

public interface IMartenMetaData
{
    SetMetaData(DocumentStore store);
}

然后在那个/那些类中实现架构,但这不起作用,因为我不能使用DocumentStore来设置元。

c# database-schema marten
2个回答
1
投票

把事情简单化。文档存储应该在您的应用程序中有一个实例,您可以在构造期间定义架构属性。无需抽象商店。

一种方法是您可以创建自己的DocumentStore实现。您可以在源代码中引用测试文档存储类。

更新:你可以在这里找到样本https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/TestingDocumentStore.cs


0
投票

我设法做了一个很好的方法来保持它更有活力,而不是所有的DocumentStore构建。

请参阅下面的代码。这个想法很简单:1)单独创建StoreOptions 2)在创建DocumentStore之前,运行方法,通过Reflection找到将添加表元数据的某个Type的所有类3)创建DocumentStore

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,我实际上使用基类)。但是,如果您没有使用它,则可以删除基类。

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);
    }
}

要么

internal class MartenTableMetaDataDriver : MartenTableMetaDataBase
{
    protected override void SetSpecificTableMetaData(StoreOptions storeOptions)
    {
        storeOptions.Schema.For<Driver>().Index(x => x.Username);
    }
}

等等

这将使Marten数据库处理程序保持干净,并将元数据分成特定类,以提高可读性,清晰度和所有这些东西=)

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.