LINQ、多态性、元数据映射、继承映射器

发布于 2024-07-07 04:27:27 字数 404 浏览 5 评论 0原文

我正在写一个小程序。 我正在编写的用于控制每个存储库的接口定义了 Save(IPublicObject) 方法。 我正在使用 LINQ 作为存储库 CRUD 的 SQL 版本。 我的问题是这样的。 我只想只有一种接受接口类型的方法。 我想考虑如何最好地找到我随后传入的继承类型的保存操作。

在书中,我正在阅读企业应用程序架构的模式。 我倾向于继承映射。 因此,我创建了一个派生对象

public class ConcretePublicObjectOne : IPublicObject{}

,然后将其传递到存储库的保存函数中。 正是在这一点上,我试图思考如何最好地说,好吧,我们需要使用“什么?” 保存方法等...

我应该使用映射类型的注册表、配置设置吗?

I am writing a small program. The interface I am writing to control each repository that is made defines a method of Save(IPublicObject). I am using LINQ for the SQL Version of the repository CRUD. My question is this. I would like to have only the one method which accepts the interface type. I want to think how I can best locate the Save action for the inherited type I then pass in.

In the book I am reading Patterns of Enterprise Application Architecture. I am leaning on the Inheritance Maping. So I create a derived object of

public class ConcretePublicObjectOne : IPublicObject{}

I want to then pass this into the Save Function of the respository. It is at this point where I am trying to think how best to say, ok we need to use "WHAT?" Save Method etc...

Should I use a registry, configuration setting mapping the types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2024-07-14 04:27:27

对于 LINQ-to-SQL,数据上下文已经为您完成了大量映射。 因此,我认为泛型可能是实现保存的最佳方法,同时仍然考虑您的界面(尽管我不太确定界面在这种情况下为您提供了什么......)。

您可以通过 GetTable() 访问数据上下文的通用方面:

    static void Save<T>(T item)
        where T : class, IPublicObject
    {
        using (DataContext ctx = GetDataContext())
        {
            Table<T> table = ctx.GetTable<T>();
            // for insert...
            table.InsertOnSubmit(item);
            // for update...
            table.Attach(item, true);
            // for delete...
            table.DeleteOnSubmit(item);

            ctx.SubmitChanges();

        }
    }

请注意,类型推断意味着您不需要指定 T保存时:

    Foo foo = new Foo();
    Save(foo); // <Foo> is inferred

这有什么用吗?

For LINQ-to-SQL, the data-context already does a lot of the mapping for you. As such, I think generics might be the best way to achieve a save while still having some consideration of your interface (although I'm not quite sure what the interface is giving you in this scenario...).

You can access the generic aspect of the data-context via GetTable<T>():

    static void Save<T>(T item)
        where T : class, IPublicObject
    {
        using (DataContext ctx = GetDataContext())
        {
            Table<T> table = ctx.GetTable<T>();
            // for insert...
            table.InsertOnSubmit(item);
            // for update...
            table.Attach(item, true);
            // for delete...
            table.DeleteOnSubmit(item);

            ctx.SubmitChanges();

        }
    }

Note that type-inference should mean that you don't need to specify the T when saving:

    Foo foo = new Foo();
    Save(foo); // <Foo> is inferred

Is that any use?

最近可好 2024-07-14 04:27:27

您想要做的是拥有一个:

Repository.Save(publicObject)

和存储库来从 publicObject 调用方法?

如果这是要求,那么您可以将存储库中的 Save 方法定义为:

public class Repository {
    public void Save(IPublicObject publicObject) {
        publicObject.Save();
    }
}

其中 IPublicObject 定义为:

public interface IPublicObject {
    void Save();
}

但使用这种方法,您必须为每个实体定义一个保存方法(如示例中的 ConcretePublicObjectOne)

What you wish to do is to have a:

Repository.Save(publicObject)

and the repository to call a method from publicObject?

If this is the requirement then you can define the Save method in repository as:

public class Repository {
    public void Save(IPublicObject publicObject) {
        publicObject.Save();
    }
}

where IPublicObject is defined as:

public interface IPublicObject {
    void Save();
}

But with this approach you will have to define a save method for each entity (like ConcretePublicObjectOne in your example)

他不在意 2024-07-14 04:27:27

马克·格拉维尔:

谢谢。 正如我所说,尽管我认为使用您的示例,我必须保留从数据上下文中获取的模型,这将我与 LINQ 绑定在一起。

不过,你所做的正是我正在寻找的事情。 它根据对象找到类型,然后知道要提交哪个操作。 使用您的示例,我可以使用匿名类型对我的模型应用转换以使用该 linq 代码吗? 我有不同的存储介质,您可以看到我需要这些模型来全面使用它们。 这些模型是一种自包含的东西,对它们之外的任何东西一无所知,哈哈。

Marc Gravell:

Thanks for that. As I say though I think using your example I would have to keep to the models that I get from the datacontext which kind of binds me to LINQ.

What you have done though is exactly the kind of thing I am looking for. It finds the type based on the object, and then knows which action to commit. Using your example, could I use anonymous types to apply a transformation to my models to work with that linq code. I have different storeage mdeiums you see which I need the models to wotk with across the board. The models are kind of self contain and know nothing about anything out side of them lol.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文