简体   繁体   English

通过服务插入与数据库访问

[英]Inserting through service vs database access

The code I'm working on right now needs to insert a bunch of related records to import external data. 我现在正在使用的代码需要插入一堆相关记录以导入外部数据。

There's an extensive piece of code that uses CustCustomerService to insert data followed by a smaller piece of code that uses table records to insert and update the rest. 有大量的代码使用CustCustomerService插入数据,然后是一小段使用表记录插入和更新其余代码的代码。 I've inquired with my colleague as to why the two different approaches and the answer was that when he started he heard going through Services was better but the service didn't support all the changes he needed, hence the switch. 我曾向我的同事询问为什么这两种不同的方法以及答案是,当他开始工作时,他听说通过服务方面的服务会更好,但是服务并不能支持他需要的所有更改,因此无法进行转换。

Researching this myself I see only information describing how to use a service and how different services differ. 我自己对此进行研究,只看到描述如何使用服务以及不同服务如何不同的信息。 But no information comparing them to in language database access. 但是没有信息可以将它们与语言数据库访问进行比较。

So. 所以。 Why exactly would I use a service over just inserting and updating records directly? 为什么要使用服务而不是直接插入和更新记录呢?

EDIT: I have to clarify. 编辑:我必须澄清。 With database access I mean something like: 对于数据库访问,我的意思是:

smaServiceObjectTable sob;

sob.clear();
sob.Foo = Bar;
// ...
sob.insert();

The two main technical reasons (I can think of) are: 我想到的两个主要技术原因是:

  1. Business Logic 商业逻辑
  2. RecId's RecId的

AX maintains the RecId number sequence, so you shouldn't ever perform inserts, and more importantly using the services for inserts/updates allows business logic to run by both you and others . AX保持RecId编号顺序,因此您永远不要执行插入操作, 更重要的是,使用插入/更新服务可以使其他人运行业务逻辑。

So imagine the user decides to buy a 3rd party ISV that interacts with customers whenever a record is updated/inserted...how would this happen with direct SQL? 因此,想象一下用户决定购买每当记录被更新/插入时与客户进行交互的第三方ISV ...在直接SQL中会如何发生?

What if the user later wanted to have some logic validate CreditLimits on customer accounts when anything was changed on the customer...how will they reliably know what methods change the customer with direct SQL? 如果用户后来想要一些逻辑来验证客户帐户上的任何更改时如何验证客户帐户上的CreditLimits,该怎么办...他们将如何可靠地知道使用直接SQL更改客户的方法?

The RecId's are often used as foreign keys, and there is the feature of surrogate keys as well. RecId通常用作外键,并且还具有代理键的功能。 Direct SQL does not easily/reliably let one maintain those normalized relationships. Direct SQL不能轻易/可靠地维护那些规范化的关系。

The RecId sequence is stored in a table but also cached, so it's not easy to just pull the next one unless you really know what you're doing. RecId序列存储在一个表中,但也被缓存,因此除非您真正知道自己在做什么,否则不容易拉出下一个。

Then to build off of what @FH-Inway was saying, what happens when a new developer comes into the picture. 然后以@ FH-Inway所说的为基础,当新的开发人员进入画面时会发生什么。 That dev will have to figure out whatever custom SQL was written before. 该开发人员将必须弄清楚之前编写的任何定制SQL。 The services are much more re-usable as well and force better practices. 这些服务也具有更高的可重用性,并要求更好的实践。

Edit: To respond to your edits in your post, you're talking about interacting with tables in AX via X++ vs interacting with an entity service or something similar, and not direct SQL. 编辑:为了响应您在帖子中所做的编辑,您是在谈论通过X ++与AX中的表进行交互,而不是与实体服务或类似服务进行交互,而不是直接使用SQL。

This is a scenario-dependent thing, I think, and it's difficult to write a comprehensive answer. 我认为这是与场景有关的事情,很难写出一个全面的答案。 The service approach allows a central interface, which can provide consistency and additional validation when you have multiple moving parts. 该服务方法允许一个中央界面,当您有多个活动部件时,该界面可以提供一致性和其他验证。 You can just think of it like copying and pasting the same 10 lines of code or encapsulating it in a method for reuse. 您可以将其视为类似于复制和粘贴相同的10行代码,或将其封装在可重用的方法中。 That way when you want to change the way those 10 lines function, you just modify the method. 这样,当您要更改这10行的功能时,只需修改方法即可。

So for some entities (Customer/Address/Vendor/etc) it makes sense to use the services if you can to ensure every other piece of business logic is hit. 因此,对于某些实体(客户/地址/供应商/等),如果可以确保其他所有业务逻辑都被使用,则使用服务是有意义的。

There are many times where it makes perfect sense to just interact with the tables. 在很多情况下,与表进行交互是非常有意义的。

A good example I can give is if you were to create a new customer. 我可以举一个很好的例子,如果您要创建一个新客户。 If you use the service, you just provide the name, address, credit limit, etc and it will create it and run associated business logic. 如果使用该服务,则只需提供名称,地址,信用额度等,它将创建该服务并运行相关的业务逻辑。 If you were to try and just insert a new record into CustTable , you may not remember to assign a PartyId , whereas using the service this is done automatically for you. 如果您尝试将新记录插入CustTable ,则可能不记得要分配PartyId ,而使用该服务会自动为您完成。 There may also be other underlying tables that automatically get populated with data when you create a new customer that will be taken care of by the service. 当您创建将由服务负责的新客户时,可能还会有其他基础表自动填充数据。

Another example is creating a new item in InventTable . 另一个示例是在InventTable创建一个新项目。 The underlying tables InventItemInventSetup , InventItemPurchSetup , and InventItemSalesSetup may need records that you could forget to create when doing InventTable.insert() . 基础表InventItemInventSetupInventItemPurchSetupInventItemSalesSetup可能需要在执行InventTable.insert()时可能忘记创建的记录。 So this is why using the service internally can make more sense as well. 因此,这就是为什么在内部使用服务也更有意义的原因。

Because services allow you to integrate AX with other systems out-of-the-box (at least in theory). 因为服务使您可以直接将AX与其他系统集成(至少在理论上如此)。 If you insert and update records directly and you want to integrate that with another system, you would have to implement the integration yourself. 如果直接插入和更新记录,并且想要将其与另一个系统集成,则必须自己实施集成。

Yes, customizing the AX services can be painful, but depending on the requirements it can still be less effort than writing the integration completely from scratch. 是的,自定义AX服务可能很麻烦,但是根据要求,与完全从头编写集成相比,它仍然可以节省很多精力。

Of course if you are just doing an import without an integration to another system, a custom implementation might be easier. 当然,如果您只是在导入时未集成到另一个系统,则自定义实现可能会更容易。 But in that case I would recommend to take a look at the data import/export framework (DIXF). 但是在那种情况下,我建议您看一下数据导入/导出框架(DIXF)。

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

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