Can't understand the last part of this code - Dart - dart

List<Person> persons = fromMap([{"id": 1, "name": "John Doe"}], [() => List<Person>, Person]);
I can't understand the part after the second comma. Can anyone explain it to me pls?

[() => List<Person>, Person]
This is specifying the type of the object that is going to be created, so, the first part is List<Person>, this will be same as the resultant object, of course, then, the next argument tells the type of the elements inside the object that will be created that is Person.
We need to do this because Dart is dynamically typed, doesn't care what the object is. But, if you need the code to run properly, so, you do care what the type is.

Related

How to add a copy of an object instead of reference to it

So - I am doping a loop in F# with some complex logic in it so I am using a System.Collections.Generic.List so I can add to it in the loop.
What I am finding though - is that when I add an item to the list - it still somehow has a reference to the variable so when I change the "currentPostCodeRange" variable, then all of the previous additions to the list are changed as well! is there a way I can assign a "copy" of the variable somehow so it doesn't reference every single item..?
let postcodeRanges: System.Collections.Generic.List<Postcode.PostcodeRange>
= new System.Collections.Generic.List<Postcode.PostcodeRange>()
let mutable currentPostCodeRange: Postcode.PostcodeRange = { RangeType = Postcode.RangeType.Normal; From="0001"; To="0001" }
Loop:
for postCode in 1 .. 9999 do
.....
if nextPostCodeisRural <> thisPostCodeisRural then
// this is the last one in this Range
currentPostCodeRange.To <- thisPostCodeString
postcodeRanges.Add(currentPostCodeRange)
.... etc, other logic
I think the easiest way to copy a record in F# is with a copy and update record expression. I strongly recommend against adding mutable objects to collections, though, precisely because it creates the aliasing problem you're seeing here. However, if you're determined to do it, you can add a copy like this:
postcodeRanges.Add({ currentPostCodeRange with To = currentPostCodeRange.To })
You have to include at least one field in the expression, so in this case, I've explicitly copied the To field, and implicitly copied the rest. It doesn't matter which one you pick.
Again, I strongly recommend against this sort of design. I think you're better off in the long run using a scan, or some other functional technique.

Need to utilize multiple arrays and have them display out to one dialog box

I've posted something earlier about this and didn't get too far and after re doing most of my code maybe it will be a bit easier to see where I am trying to go...
Its still partially done, I am having compiler errors when I am trying to assign the number of values that correlate with "numberOfTaxpayers" into the new arrays created for userGross, userTax, and userNumChild. It compiled with the userfName and userlName strings so any guidance to the right path?
what I have to do is create arrays for each of those categories then have them display all the inputs at the end. So there should be a JOptionPane showing for example:
First Name: Example FName
Last Name: Example LName
Gross Income: example income
Number of children: example number
Tax Due: amount of tax due
and it has to do this for as many users as you enter but output all of them in one box!
not looking for completed code, just some guidance as to where I failed!! (Array lists cannot be used)
import javax.swing.JOptionPane;
class AssignmentSeven
{
public static void main(String[] args)
{
CaPiTALiZaTiOn MatTeRs! That's your issue.
numberOfTaxPayers != numberOfTaxpayers
But the key lesson to get from this is to not ignore the error message as it will often tell you exactly what is wrong. Here it's telling you that it can't recognize the numberOfTaxPayers variable, and if you look closely at the variable, you'll see that it makes sense, because a variable of exactly that name and capitalization was never declared.

Hydrating Database

I am new to learning and understanding how Hydration works, just wanted to point that out first. I'm currently able to Hydrate Select and Insert queries without any problems.
I am currently stuck on trying to Hydrate Update queries now. In my entity I have setup the get/set options for each type of column in my database. I've found that the ObjectProperty() Hydrator works best for my situation too.
However whenever I try to update only a set number of columns and extract via the hydrator I am getting errors because all the other options are not set and are returning null values. I do not need to update everything for a particular row, just a few columns.
For example in my DB Table I may have:
name
phone_number
email_address
But I only need to update the phone_number.
$entity_passport = $this->getEntityPassport();
$entity_passport->setPrimaryPhone('5551239876');
$this->getTablePassport()->update($this->getHydrator()->extract($entity_passport), array(
'employeeid' => '1'
));
This returns an error because setName() and setEmailAddress() are not included in this update and the query returns that the values cannot be null. But clearly when you look at the DB Table, there is data already there. The data that is there does not need to be changed either, only in this example does the PrimaryPhone() number.
I've been looking and reading documentation all over the place but I cannot find anything that would explain what I am doing wrong. I should note that I am only using Zend\Db (Not Doctrine).
I'm assuming I've missed something someplace due to my lack of knowledge with this new feature I'm trying to understand.
Perhaps you don't Hydrate Update queries... I'm sort of lost / confused. Any help would be appreciated. Thank you!
I think you're having a fundamental misconception of hydration. A hydrator simply populates an entity object from data (hydrate) and extracts data from an entity object (extract). So there are no separate hydrators for different types of queries.
In your update example you should first retrieve the complete entity object ($entity_passport) and then pass it to the TableGateway's update method. You would retrieve the entity by employeeid, since that's the condition you're using to update. So something like this:
$entity_passport = $passportMapper->findByEmployeeId(1);
$entity_passport->setPrimaryPhone('5551239876');
$this->getTablePassport()->update($this->getHydrator()->extract($entity_passport), array(
'employeeid' => $entity_passport->getId()
));
This is assuming you have some sort of mapper layer. Otherwise you could use your passport TableGateway (I assume that's what getTablePassport() returns, no?).
Otherwise, if you think retrieving the object is too much overhead and you just want to run the query you could use just a \Zend\Db\Sql\Sql object, ie:
$sql = new \Zend\Db\Sql\Sql($dbAdapter);
$update = $sql->update('passport')
->set(array('primary_phone' => $entity_passport->getPrimaryPhone()))
->where(array('employeeid' => $employeeId));
Edit:
Maybe it was a mistake to bring up the mapper, because it may cause more confusion. You could simply use your TableGateway to retrieve the entity object and then hydrate the returned row:
$rows = $this->getTablePassport()->select(array('employeeid' => 1));
$entity_passport = $this->getHydrator($rows->current());
[...]
Edit 2:
I checked your gist and I noticed a few things, so here we go:
I see that your getTablePassport indeed does return an object which is a subclass of TableGateway. You have already set up this class for it to use a HydratingResultset. This means you don't need to do any manual hydrating when retrieving objects using the gateway.
You also already implemented a Search method in that same class, so why not just use that? However I would change that method, because right now you're using LIKE for every single column. Not only is it very inefficient, but it will also give you wrong results, for example on the id column.
If you were to fix that method then you can simply call it in the Service object:
$this->getTablePassport->Search(array('employeeid' => 1));
Otherwise you could just implement a separate method in that tablegateway class, such as
public function findByEmployeeId($employeeId)
{
return $tableGateway->select(array('employeeid' => $employeeId));
}
This should already return an array of entities (or one in this specific case). P.S. make sure to debug and check what is actually being returned when you retrieve the entity. So print_r the entity you get back from the PassportTable before trying the update. You first have to make sure the retrieval code works well.

Difference on working of object_id method with Fixnum object and String object

I am confused about the working of one of the innate method present in all ruby objects i.e object_id method. When I run object_id method on any Fixnum object again and again, for example in irb if I do this,
>>100.object_id
=>201
and do this again,
>>100.object_id
=>201
But when I work with String object for example
>>"Hello".object_id
=>162333336
and do this again,
>>"Hello".object_id
=>15502236
Why so? In ruby, everything is an object, and every object has an innate method named object_id which uniquely identifies the object. But here, ruby is confusing me as it treats two strings with same text (i.e "Hello") as different, but two Fixnum objects with same value (i.e.100) as the same and gives the same object id for them. Why so? Can any one please help me?
Fixnums are immutable objects in Ruby. There is exactly one instance created and you work with that object "directly". i.e references are not used unlike other regular objects. So They have a fixed object_id. This is ok because you have only one instance of the object.
But when you write "hello", a new string object is created. And in the same script, if you give another "hello", even though they have same content, a new object is created. Hence the different object_ids.
Such behaviour is a matter of Ruby implementation, not specification. Most likely, you're using MRI (compiled from C source), in JRuby you can get different results.
For performance purposes, MRI handles true, false, nil, Fixnum and symbol specially. A couple of links where you can find more info about it: http://www.oreillynet.com/ruby/blog/2006/02/ruby_values_and_object_ids.html , http://rhg.rubyforge.org/chapter02.html
If you wants to get same object id of string you have to convert it to symbol first then your object_id remains constant
'Hello'.to_sym.object_id

RavenDB ID for child documents

I like how cleanly an object is stored in ravenDB, but have a practical question for which I'm not sure of the best answer.
Lets say i have a quote request:
QuoteRequest.cs
int Id;
dateTime DateCreated;
List<Quotes> Quotes;
Quote.cs
int ProviderId;
int Price;
int ServiceDays;
int ServiceTypeId;
when someone hits a page, i spit out a list of quotes from which they can choose. These quotes are only related to an instance of the quote request.
My question is, since a child object, such as a quote in the list, doesnt have an Id generated by the database, how do I generate a querystring to let the next page know which quote the user wants to buy?
There could be multiple quotes by one providerId.
My thoughts were either add a QuoteId and increment it based on this.Quotes.Count, but that seems a little hacky, or generate a random number, also a little hacky.
How do people generally handle something like this?
Do you really need to associate the purchase (what the user chose to buy) with the original quote? I'm guessing that you take the quote and then convert it to a purchase.
If that is so, then don't worry about an id at all. Just pass along the constituent values to the next step. In other words, treat quote like a Value in the DDD sense.
However, if you do need to store an association to the purchase... well, then it depends on what you really need to track. For example, you could just update the QuoteRequest, marking the selected quote. (Add an IsSelected or something similar to the quote class Quote.) Then the purchase could be linked back to the quote request, and you could identify the quote by way of the flags.
Again, all this depends on the context (and I'm just making guesses about that).
Since no one has answered this yet I'll just say how I would do it;
Why add a Id at all? just use the index of the List? It the request is "?quote=0" they get the quote at position 0?
Not really sure If I'm not getting something here though...
One option is to have the parent object store the last used id. When adding a new child object you increment the id-counter and add that to the child. When the object is saved the id-counter is automatically incremented.
Lets say you have blog post with comments:
public class Post
{
public int NextCommentId;
public List<Comment> Comments;
...
}
...
var comment = new Comment { Id = post.NextCommentId++ };
post.Comments.Add(comment);
session.SaveChanges();
The code above might not be 100% correct, but should give you an idea of how to do it at least!

Resources