如何通过 WCF 使用 LinqToSql 维护子对象?

发布于 2024-07-12 03:27:38 字数 622 浏览 5 评论 0原文

我目前正陷入这个解决方案的设计中。

数据层设计由以下部分组成:

  • 配方(父高级对象)
    • 语言详细信息(名称、语言描述)(很多)
      • 标头(多个)
      • 步骤(很多)
        • 成分(多种)
        • 数量(很多)
        • 程序(很多)
      • 注释(很多)

我面临的挑战是如何创建一个数据访问设计,在填充对象时在数据库中添加/删除子对象来自 WCF SaveRecipe(recipe) 方法?

这一切都源于管理层要求我们在应用程序中添加一个通信层,现在我们的 UI 耦合到我们的业务层,而 BL 直接耦合到 DAL,我们基本上需要在 BL 和 DAL 之间注入 WCF 。

我在 这个线程 中读到,使用 L2S 并不是 WCF 的好主意,但由于设计这并不新鲜,我们必须使用这种类型的方法,然后在我们可以重构大量 UI 工作后就放弃它。

I am currently stuck in the design of this solution.

The data layer design consists of the following:

  • Recipe (parent high level object)
    • Language Detail (name, description by language) (many)
      • Headers (many)
      • Steps (many)
        • Ingredients (many)
        • Quantities (many)
        • Procedures (many)
      • Notes (many)

The challenge that I am having is how to create a data access design that will add/remove the child objects in the database when the object is populated from a WCF SaveRecipe(recipe) method?

This all stems from the management asking us to add a communications layer in our application, right now our UI is coupled to our business layer, and the BL is directly coupled to the DAL, we basically need the injection of WCF between the BL and DAL.

I have read in this thread that using L2S isn't a good idea over WCF, but since the design isn't new, we have to use this type of methodology, and then move away from it once we can refactor the heavy amounts of UI work.

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

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

发布评论

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

评论(4

椵侞 2024-07-19 03:27:38

如果您尝试使用 WCF 发送内容,我建议您创建一个要在域中移动的数据的模型。 据我发现,您无法序列化 IQueryable 对象,但您可以创建一组由 Linq 填充的类,然后对其进行序列化。 例如:

[DataContract]
public class Recipe {

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public List<Ingredient> Ingredients { get; set; }

}

然后用 WCF 填充该

List<Recipe> recipes = (from r in dc.recipe
                           select new Recpie {
                               Name = r.name,
                               Description = r.description,
                               Ingredients = (Linq code to make list of ingredients)
                           }).ToList();

列表,然后使用 WCF 发送列表就变得小菜一碟了。

If you are trying to send stuff down the line with WCF I suggest you create a model of the data you want to move around your domain. As far as I've found you can't serialize IQueryable objects, but you can create a set of classes that Linq populates and then serialize that. For example:

[DataContract]
public class Recipe {

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public List<Ingredient> Ingredients { get; set; }

}

Then fill that up with

List<Recipe> recipes = (from r in dc.recipe
                           select new Recpie {
                               Name = r.name,
                               Description = r.description,
                               Ingredients = (Linq code to make list of ingredients)
                           }).ToList();

And then sending the list down the line with WCF becomes a piece of cake.

誰認得朕 2024-07-19 03:27:38

我喜欢 Karl 在您引用的 线程 上的回答:

最好自己创建
用于传输数据的类
目的。 当然,这些课程,
将作为数据合同来实现。
在您的服务层中,您将转换
linq-to-sql 对象之间和
数据载体对象的实例。
这很乏味,但它解耦了
服务的客户来自
数据库架构。 它还具有
给你更好的控制的优势
传递的数据的
你的系统。

看来您无论如何都必须重构 - 最好从在 UI 层中重构对 linq2sql 的依赖关系开始。

我认为您没有快速且简单的解决方案。

I like this answer from Karl on the thread you referenced:

It is better to create your own
classes for the transfer of data
object. Those classes, of course,
would be implemented as DataContracts.
In your service layer, you'd convert
between the linq-to-sql objects and
instances of the data carrier objects.
It is tedious but it decouples the
clients of the service from the
database schema. It also has the
advantage of giving you better control
of the data that is passed around in
your system.

It looks like you're going to have to refactor anyways - just as well start by refactoring out the dependency to linq2sql in your UI layer.

I don't think you have a quick and easy solution.

海未深 2024-07-19 03:27:38

我不建议通过 WCF 公开 L2S 类。 创建 DataContract 对象的层次结构,并通过 WCF 传递它们。 这样做的缺点是您必须将 L2S 对象“深度复制”到 DataContract 层次结构中,但优点是您只能包含通过网络传输所必需且正确的字段。 例如,在我现在正在处理的一个项目中,我通过网络传递一个 EmployeeData 对象,其中包括我的大部分 L2S Employee 字段,但不包括员工的 salted 哈希和字段等内容。 密码盐。

I don't recommend exposing L2S classes over WCF. Create a hierarchy of DataContract objects, and pass those over WCF. The downside of this is that you have to "deep copy" your L2S objects into your DataContract hierarchy, but the upside is that you can only include fields that are necessary and proper for transport over a network. For example, in a project I'm working on now, I pass an EmployeeData object over the network that includes most of my L2S Employee fields but excludes things like the employee's salted hash & password salt.

删除会话 2024-07-19 03:27:38

创建包含和排除子条目的类也可能是一个想法。 我有时会创建这样的类:

class Recipe {
    [DataMember] public string Name;
    [DataMember] public string Description;
}

class RecipeWithIngredients : Recipe {
    [DataMember] public IList<Ingredient> Ingredients;
}

编辑:在帖子完成之前无意中发布。

It could also be an idea to create classes both including and excluding child entries. I have sometimes created classes like this:

class Recipe {
    [DataMember] public string Name;
    [DataMember] public string Description;
}

class RecipeWithIngredients : Recipe {
    [DataMember] public IList<Ingredient> Ingredients;
}

Edit: inadvertedly posted before the post was finished.

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