Ruby级间通信(Ruby inter class communication)
我遇到了开发一个项目的奇怪困难。 我没有足够的课程经验所以这就是我在这里问的原因。 我有一个用一个参数初始化的类,我需要其他类来调用该类函数,但我不能直到该类被初始化,所以我问我怎么能这样做。
以下是我所谈论的一些例子:
class AVR def initialize(device) @device = device @avr_conf = YAML::load(File.open("./devices/#{device}.yaml")) end def avr_conf return @avr_conf end end class IO def setMode(a,b) "#{AVR.avr_conf[a]} = #{b}" end end
I met strange difficulty developing one project. I dont have enough experience with classes so this is why I ask here. I have one class which is initialized with one parameter and I need other classes to call that class functions but I cant until that class is initialized so I asking how I could do that.
Here is some example what I talking about:
class AVR def initialize(device) @device = device @avr_conf = YAML::load(File.open("./devices/#{device}.yaml")) end def avr_conf return @avr_conf end end class IO def setMode(a,b) "#{AVR.avr_conf[a]} = #{b}" end end
原文:https://stackoverflow.com/questions/8898372
最满意答案
您可以覆盖SaveChanges以查找DbEntityValidationException的改进验证消息。
1.在与EDMX所在位置相同的位置创建新的部分类。
2.使用以下C#代码覆盖SaveChanges实现。
public partial class NorthwindEntities { public override int SaveChanges() { try { return base.SaveChanges(); } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } } }
有关详细信息,请参阅此链接: http : //devillers.nl/improving-dbentityvalidationexception/
You can override SaveChanges to find improved validation message of DbEntityValidationException.
1.Create a new partial class in the same location as where your EDMX resides.
2.Use the following piece of C# code to override the SaveChanges implementation.
public partial class NorthwindEntities { public override int SaveChanges() { try { return base.SaveChanges(); } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } } }
Refer this link for more details : http://devillers.nl/improving-dbentityvalidationexception/
相关问答
更多-
将数据保存到多个表(Saving data to multiple tables)[2021-10-08]
您可以覆盖SaveChanges以查找DbEntityValidationException的改进验证消息。 1.在与EDMX所在位置相同的位置创建新的部分类。 2.使用以下C#代码覆盖SaveChanges实现。 public partial class NorthwindEntities { public override int SaveChanges() { try { return base.SaveChanges(); } catch (DbEnt ... -
您的代码没有任何问题,只是错过了一些带有三个'm'的回声,分号和错误注释 $this->Form->create($article) //missing echo and semicolon; echo $this->Form->create($article); //the correct one $this->Form->end() //missing submit button echo $this->Form->button(__('Submit')); //the correct one e ...
-
最好的方法是在开始将数据写入数据库之前验证所有数据。 我没有使用ZF2,这个解决方案实际上是依赖于框架的,所以你需要检查ZF2文档。 例如,在Yii中,您只需为模型的每个字段定义验证规则,因此您可以在开始将数据保存到数据库之前确保您的Organization包含所有数据,可能Zend中可能存在类似情况。 请注意,验证并不仅仅意味着检查空值,您可能需要验证不同的内容,例如:“电子邮件是正确的电子邮件,如xxx@yyy.com”,“名称不为空”,“名称长度更多超过3个字符“,”名称长度小于1000个字符“等等。 ...
-
ZF2保存到多个表(ZF2 saving to multiple tables)[2022-11-04]
为了避免重复代码创建一个额外的方法,让我们称之为insertIntoTable() 。 此方法将是用于将数据插入特定表的辅助函数: public function save(OrganizationInterface $organization) { $result = $this->insertIntoTable('TABLE NAME', $values); $result1 = $this->insertIntoTable('OTHER TABLE NAME', $v ... -
将模型保存到多个表(Saving Model to multiple tables)[2022-04-11]
我认为您想要的是创建一个模型映射器来处理您的用户数据,这些数据将引用您的表模型。 这里有一个很好的例子: 使用Zend_Model_Mappers和Zend_Db_Table_Abstract的单个模型的多个表 I think what you want todo is create a model mapper for handling your users data that will reference both your table models. There is a good example h ... -
您只需保存视图模型上属性的模型。 使用教程中的示例,如果要为这两个中的每一个添加新记录,可能是这样的: db.People.Add(model.People); db.Organizations.Add(model.Organizations); db.SaveChanges(); 您可以进行所有添加/修改/等。 你想要在数据库上下文( db对象及其属性)中,当你调用.SaveChanges()它们都将被发送到数据库。 在上面的代码中,数据库调用最终会成为两个INSERT语句。 You'd just sa ...
-
使用实体框架为多个表保存数据的更好方法(Better approach for saving data for multiple tables using entity framework)[2021-12-24]
您可以通过使所有Saver类的方法非静态(它还可以防止在您不需要时将数据保存在内存中)来实现,并使其实现IDisposable接口。 之后,您只需要遵循Microsoft建议的一次性模式( https://msdn.microsoft.com/en-us/library/b1yfkh5e%28v=vs.110%29.aspx ),例如: public class Saver : IDisposable { private readonly AuraEntities db; private b ... -
使用实体框架在一个事务中将数据保存在多个表中(saving data in multiple tables in one transaction using entity framework)[2022-06-30]
您的示例代码看起来很好。 实体框架是一个更改跟踪器,可跟踪所有更改。 您在上下文中对对象所做的每个更改都将由本地上下文存储,并且当您调用DataContext.SaveChanges();时,所有更改都将立即提交DataContext.SaveChanges(); 。 所以在这种情况下,如果你按InsertObject1调用你的InsertObject1 ,那么InsertObject2和InsertObject3 ,然后是DataContext.SaveChanges(); ,一切都将一气呵成。 如果你的 ... -
找到了答案。 https://stackoverflow.com/a/13862746/4685379 但是,这个答案没有说明如何实现对多个表的插入。 关键是将所有插入放在beginTransaction()和commit()方法之间,如下所示: try { $this->getAdapter()->getDriver()->getConnection()->beginTransaction(); // your inserts go here $this->getAdapter ...
-
是的,您必须使用VBA代码或宏。 基本上,您必须定义表单的添加,更新和删除操作。 在表单级别定义“After Update”事件,“After Delete Confirm”和“After Insert”。 在那里更新你的表。 您可能必须根据需要定义其他事件。 Yes, you have to use VBA code or macros. Basically, you have to define the add,update and delete operations of the form. Defi ...