简体   繁体   English

C# LinqToSql LoadOption 与模式存储库

[英]C# LinqToSql LoadOption with pattern repository

In the project i am maintaining, i have refactoring few logic using the repository pattern over the old ORM (LinqToSql).在我维护的项目中,我使用存储库模式重构了一些逻辑,而不是旧的 ORM (LinqToSql)。

My problem now is sometimes i have to share DataContext across repository (this is a desirable feature) but those repository try to "steal" each other the loading option.我现在的问题是有时我必须跨存储库共享 DataContext (这是一个理想的功能),但这些存储库试图“窃取”彼此的加载选项。

Here an example of my repository constructor, that receive the DataContext and build the proper loading option.这是我的存储库构造函数的示例,它接收 DataContext 并构建正确的加载选项。

    public ArticleRepository(DataContext datacontext) : base(datacontext)
    {
        this.Name = "ArticleRepository";

        lock ( _sync )
        {
            this.Datacontext = datacontext; // --> this is done in base class, assing the shared object to the current repository.
            this.Datacontext.DeferredLoadingEnabled = false;

            DataLoadOptions options = new DataLoadOptions();
            options.LoadWith<tbl_ana_Article>(article => article.UoM);
            options.LoadWith<tbl_ana_Article>(article => article.Brand);
            this.Datacontext.LoadOptions = options;
        }
    }

But i have also OrderRepository which it's own loading option.但我也有 OrderRepository 它自己的加载选项。

The problem arise when i use such repositories on the same DataContext:当我在同一个 DataContext 上使用此类存储库时会出现问题:

using ( var context = new MyDatacontex("...") )
{
    var articleRepo = new ArticleRepository(context);
    var orderRepo = new OrderRepository(context);// <-- here the loading option are overwritten

    articleRepo.DoStuff();
    orderRepo.DoOtherStuff();

    context.SubmitChanges();
}

Now in this specific case, i can reorder operation and avoid problems but it's a very specific and fragile solution.现在在这种特定情况下,我可以重新排序操作并避免出现问题,但这是一个非常具体且脆弱的解决方案。

I don't know if i have to specify a loding option in the constructor, save it in the object, and overwrite the shared datacontex property before each datacontext use (read).我不知道我是否必须在构造函数中指定加载选项,将其保存在 object 中,并在每次使用数据上下文之前覆盖共享数据上下文属性(读取)。 Is this a good solution or there is a better one?这是一个好的解决方案还是有更好的解决方案?

since Context is a same object, you have to prepare it before use.由于Context是同一个 object,所以使用前必须准备好。 It's a complex scenario so you have to be warned about state of "Context Options" before any usage.这是一个复杂的场景,因此在使用之前必须警告您“上下文选项”的 state。 If you change it before specific usage, the next usage must clear last options or set its own options.如果在特定用法之前更改它,则下一个用法必须清除最后一个选项或设置自己的选项。 Best approach could be that you set options before usage and return it to previous state after usage so it would be all clear.最好的方法可能是您在使用前设置选项并在使用后将其返回到以前的 state 以便一切都清楚。 But in async scenarios there is a chance that you see a behavior that you didn't expect.但在异步场景中,您可能会看到意想不到的行为。

An other note, some options are shared and some of them are not.另请注意,有些选项是共享的,而有些则不是。 For example DeferredLoadingEnabled is a shared setting and LoadWith is a specific option.例如DeferredLoadingEnabled是一个共享设置,而LoadWith是一个特定选项。 You can decide to make shared setting fixated and set all specific options once in Context constructor.您可以决定在Context构造函数中固定共享设置并设置所有特定选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM