问:
RDBMS (MySQL) logic question. How to join multiple "FRIENDS"
so this is a logic question as to creating the proper relationships in a MySQL database.

What logic would you choose for the following scenario?

This is a back-end for an app where people can have friends they can interact with, or "Add" if you will to their own "Network" for lack of better terms.

So, for example, should I have a "MyFriends" table where, for example, the "MyFriend.ID" column is identical to perhaps the "User.ID" of the person that initiated the connection? And have a subsequent column called "Friends" where the "User.ID" of everyone you "Added" to your "Network" is inserted?

Or do you have alternate suggestions as to create the Relationships necessary for the "Friends" to be connected inside each owns "Network"?

Body:
I'd have one table for Users and a second table for Relationships.

The Relationship table would have 2 foreign keys, both pointing back to records in the User table.

Here's a rough diagram:

+------------------+         +------------------------+
| User             |         | Relationship           |
|------------------|         |------------------------|
| UserPK  (int)    |         | RelationshipPK (int)   |
| ...              |<--------+ User1FK        (int)   |
|                  |<--------+ User2FK        (int)   |
|                  |         |                        |
|                  |         |                        |
|                  |         |                        |
|                  |         |                        |
+------------------+         +------------------------+

The Relationship table is technically the middle table in a many to many relationship.

Here's an example.  Note the diagram shows 2 records from the USER table:

+------------------+         +------------------------+      +------------------+
| User             |         | Relationship           |      | User             |
|------------------|         |------------------------|      |------------------|
| UserPK:     1    |         | RelationshipPK   100   |      | UserPK:     2    |
| Name:   ALICE    |<--------+ User1FK            1   |      | Name:     BOB    |
|                  |         | User2FK            2   +----->|                  |
|                  |         |                        |      |                  |
|                  |         |                        |      |                  |
|                  |         |                        |      |                  |
|                  |         |                        |      |                  |
+------------------+         +------------------------+      +------------------+

We have 3 records in the database: a USER record for Alice, a USER record for Bob, and a RELATIONSHIP record showing that Alice and Bob know each other.

You'll probably want to add additional fields to these tables.  For example, is the relationship "friends", "co workers", "family", etc.

If Alice is a friend of Bob and Bob is a friend of Alice (i.e., a symmetrical relationship), it might be easiest to just have two records in the relationship table (User1FK:Alice, User2FK:Bob and User1FK:Bob, User2FK:Alice).  That would make certain queries easier.


问:
Pluggable UI Components in Java Web Applications
I will try to explain my question with an example. Let us say that we are writing a hotel reservation system which is web based using Java. We will have different payment methods, say Paypal, Master Card and Visa Card. Different customers of our product will use different payment methods only. (Hotel XYZ, who is our customer, will only want to use Paypal and hotel ABC will only want to show the Master Card and Visa options).

In this kind of scenario, the normal way of doing things would be to have the different payment methods as plugins. And install only the needed plugins in a customer environment.

My question is, how to achieve this in a web application? From my understanding, the folder structure for a Java web application is as shown here. But if we have pluggable payment methods, the plugins themselves should be able to register the user interface components rather than having them in the main application. (For example let us say there is master_card.jsp file which handles the master card payments and visa.jsp etc..). With the above linked kind of folder structure, it seems to me that we would have to put in those jsp files in the webapp even though some customers might not need it.

Can someone please let me know whether

if my above observation is correct

how to have above kind of plugin structure with Java/Java EE?

are there any frameworks in Java that
supports above kind of thing?

答:
Here is one possible solution.  There are others, but this seems like quite a simple way of doing it to me.  I assume your site will be built using an MVC architecture, and that plugins provide three kinds of content: Java classes to implement controllers, JSP views, and static resource files that must be served.

Deploy your application as an expanded directory structure rather than a WAR file.  This means users will be able to drop plugins as JAR files in the WEB-INF/lib directory.  The server will automatically add any JAR file deployed in this directory to the classpath.

Use the Spring MVC framework, and enable its options to scan the classpath for components.

Each plugin can have one or more classes with the Spring annotation @Component, which will cause Spring to automatically create an instances of them.  They may use @PostConstruct on a method to cause that method to execute at startup, which can be used for any initialisation you require.

Integration with the rest of the site can be achieved by exporting services as objects of known type and using @Autowire annotations on fields of that type to cause Spring to provide references to them.

Use the technique described at http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.VE2HPfl_vTo to configure a resource handler that maps a directory name on the server to a directory in the classpath and store your static resources in your jar file at that location

Precompile your JSP files to Java classes and include them in your jar file (instructions here: http://docs.oracle.com/cd/E19146-01/821-0790/abxcc/index.html)

You should then be able to bundle up features of your web site into JAR files that can be deployed simply by copying the JAR file into the WEB-INF/lib directory of your web app.

ContentLicense: CC BY-SA 3.0


问:
How do browsers handle URLs in HTML5 offline mode?
I just read the de facto standard for HTML5 offline/localStorage.

It has me curious: if I build my web app to work regardless of whether or not there's an internet connection, and I use HTML5 localStorage to do this, then how are web app URLs affected when in "offline mode"?

For instance, normally my web app's home page might be http://myapp.example.com. How would this change in offline mode; what would the URL be?

I'm hoping that the browser would allow my users to still go to http://myapp.example.com, but then detect no network connection, and attempt to load the site from localStorage.

But it's also possible that, once they load the site from cache, they automatically change the URL to something like file:///path/to/localStorage/homepage.html.

It's also entirely possible I misunderstood that entire article and this is not something that even localStorage can do for me. Any ideas?

答:
localStorage is just a storage location that your web app can access, you still need internet access for everything else unless it has been cached already by the browser.

here's some info I dug up about localStorage:

localStorage doesn't change the url of your resources to local files, those are still stored in your server or your browser cache.

you have to access localStorage manually to access the stuff you stored. you do that like so localStorage.getItem('key')

from that, you have to manually code a check to see if you already have the resources you need in localStorage, if not then download it from the server via ajax or whatever you prefer


问:
If someone offers you an unverified statement regarding software development practices, do you respond with "citation needed"?
Recently I attended a lecture given by Greg Wilson (Chief Scientist of Software Carpentry). From the abstract:

The idea that claims about software development practices should be based on evidence is still foreign to software developers,
  but this is finally starting to change: any academic who claims that a particular tool or practice makes software development
  faster, cheaper, or more reliable is now expected to back up that claim with some sort of empirical study.

Overall, the lecture was very informative and left me thinking quite deeply about my approach to development. In particular, I now find myself looking for citations to back up a lot of statements. Previously, I had slipped into the habit of simply repeating offered truths, with perhaps a mental note to go check up on it later.

Putting it bluntly, I was being gullible.

Here's an example taken from the lecture:

"If more than 25% of the code needs refactoring, it's quicker to rewrite it".

Sounds plausible, but is it true? Where's the study backing this up? Is it true for all languages? And so on.

OK, it's quite possible to take this to an extreme and not believe anything by anyone unless you have derived it yourself from
first principles. That way lies madness (or maybe mathematics ;-) ). But, if someone comes up to you with a statement along the
lines of "Hey, by doing this in [pick language of moment] we'll be able to boost productivity by [pick multiple of 10]%" are you
inclined to just accept it, or are you going to ask for proven evidence?

If it's the latter (and I hope it is) then

where would you go to find this evidence?

how stringent would you be?

In short, if someone offers you an unverified statement, will you respond with "citation needed"?

答: I think with anything you never know until you try it. Even with proof to back up a statement, it's always possible to bend facts to the benefit of your point.  That being said you shouldn't try every new thing that hits the interwebs.  Make your best judgment.  Remember, if something is sounds too good to be true it probably is.  Always ask yourself why do you need to adopt something? What do you have to gain?  Does it make sense from a business prospective?  Never blinded except something on faith.</p>

ContentLicense: CC BY-SA 2.5


答:
Isn't that something that depends on a whole lot of intangible variables (variables that have no way of being scientifically measured)?

In my opinion, they are talking about an empirical method for measuring emotions. Something that not even Spock could accomplish. =)

Title: If someone offers you an unverified statement regarding software development practices, do you respond with "citation needed"?

Body:
Recently I attended a lecture given by Greg Wilson (Chief Scientist of Software Carpentry). From the abstract:

The idea that claims about software development practices should be based on evidence is still foreign to software developers,
  but this is finally starting to change: any academic who claims that a particular tool or practice makes software development
  faster, cheaper, or more reliable is now expected to back up that claim with some sort of empirical study.

Overall, the lecture was very informative and left me thinking quite deeply about my approach to development. In particular, I now find myself looking for citations to back up a lot of statements. Previously, I had slipped into the habit of simply repeating offered truths, with perhaps a mental note to go check up on it later.

Putting it bluntly, I was being gullible.

Here's an example taken from the lecture:

"If more than 25% of the code needs refactoring, it's quicker to rewrite it".

Sounds plausible, but is it true? Where's the study backing this up? Is it true for all languages? And so on.

OK, it's quite possible to take this to an extreme and not believe anything by anyone unless you have derived it yourself from
first principles. That way lies madness (or maybe mathematics ;-) ). But, if someone comes up to you with a statement along the
lines of "Hey, by doing this in [pick language of moment] we'll be able to boost productivity by [pick multiple of 10]%" are you
inclined to just accept it, or are you going to ask for proven evidence?

If it's the latter (and I hope it is) then

where would you go to find this evidence?

how stringent would you be?

In short, if someone offers you an unverified statement, will you respond with "citation needed"?


问:
Something like "tracking" for local branches in git?
I often have a workflow where I develop some feature in a Git branch, switch to another feature-branch, etc. During development, the original develop branch and the feature branches diverge.

Now when switching branches I am all of the sudden developing on an older version of my project (which is to be expected, of course).

I do wonder however whether something like a "tracking" behaviour can be configured with Git, which would mean that specially-marked feature branches would be automatically rebased on another branch (like develop or master). This would simplify a lot, especially rebuilds.

答:
No, you just have to

```<code>git checkout feature-branch
git merge master|develop
</code>```

Done, your branch is now up to date. It will also let you a chance to resolve conflicts early. Perform the merge periodically, and you'll be fine.

Body:
No, you just have to

git checkout feature-branch
git merge master|develop

Done, your branch is now up to date. It will also let you a chance to resolve conflicts early. Perform the merge periodically, and you'll be fine.


答: No, there's no automatic rebasing. The easiest way to achieve a very similar effect is to do single branch development, as recommended for continuous delivery (relevant article).</p>

Title: Something like "tracking" for local branches in git?

Body:
I often have a workflow where I develop some feature in a Git branch, switch to another feature-branch, etc. During development, the original develop branch and the feature branches diverge.

Now when switching branches I am all of the sudden developing on an older version of my project (which is to be expected, of course).

I do wonder however whether something like a "tracking" behaviour can be configured with Git, which would mean that specially-marked feature branches would be automatically rebased on another branch (like develop or master). This would simplify a lot, especially rebuilds.


问:
What's the most absurd myth about programming issues?
To put it another way... What is the most commonly held and frustrating misunderstanding about programming, you have encountered?

Which widespread and longstanding myths/misconceptions do you find hard for programmers to dispel/correct.

Please, explain why this is a myth.

答:
That premature optimization means you shouldn't optimize at all. I've seen more horrendously bad databases because no one wanted to consider performance (critical to any database system) in design as that was premature optimization than any other database design issue . Rubbish, there are known performance killers, stop using them as your first choice.

Another myth, it's too hard to refactor the database. No but you have to consider how to do refactoring in the design phase to do it effectively. And BTW, the longer you wait to fix that annoying design-based performance issue, the harder it's going to be to fix.

Another bad myth, database design should reflect OOP principles. No, databases are designed to work with sets not OOP principles. Some OOP things will cause horrible performance problems and others are just pain silly in database terms.

Finally, you should enforce data integrity in the application.  Databases are going to last past the application and would lose the rules when the application is replaced, mulitple applications are going to access them and there will often be the need to run direct queries to fix things that do not go through the application.  I have never seen a database that refuses to enforce data integrity in the datbase that has good data.


问:
DTO - service layer - statelessness best practices interfering with each other
I am designing a REST API coupled with a service layer that takes DTOs as input and produces them as output. This works fine for most service calls where the DTO is used to access an underlying library (an embedded OLAP server) that does the work.

There is one service, however, where something seems wrong. The service is responsible for creating, manipulating, and optionally storing objects that represent reports (think excel reports). It does this through the underlying library, which does not expose reports as a resource. Since the reports don't represent a server-side resource unless they are persisted (persistence being my own implementation, not a concept in the library), the service needs to take a report object that holds all the information necessary to construct a report from scratch to be stateless.

I decided that the report service methods should take a ReportDTO, manipulate it, and return the modified version. (This would include operations such as swapping the row and column axes, sorting an axis, and filtering an axis.) My thinking was that since I have all the information available in the ReportDTO necessary to perform all these operations, I might as well perform them on the DTO and return a modified version rather than duplicate the DTO as a domain object. That is not to say the server doesn't interface with any model. Indeed, the server-side code involves validating the report using a third party library (i.e., the validation model is not my own -- i don't have a domain model for the report itself, just DTOs), saving the report if the user so chooses, and executing the report against a database to obtain fresh data. So there is work to be done besides manipulating DTOs, it's just that it's not work tied to a domain model I needed to design myself, but rather one provided to me.

After I implemented the aforementioned operations, I realized something. The operations are business logic operations, are they not? A DTO should not have this. I then had numerous other thoughts: "Okay, that's fine, I'll do the operations in the service layer... Oh, wait. The service layer should only coordinate domain objects that contain business logic, not have business logic itself. Alright, I'll make the ReportDTO a bonafide domain object. Crap, now I am exposing my domain objects from the service layer. This is an API, I should really stick with DTOs as the input and output for the service layer so that changes in my domain model (or changes in the 3rd party domain model I utilize) do not affect the contract with service clients. Fine, I'll have a Report and a ReportDTO.... that have exactly the same members because the ReportDTO already has all the necessary information to manipulate a report (just not execute, validate, or save it) due to the transient report decision I made for the sake of a RESTful API."

Clearly I have countered my every proposed solution. So, what the heck do I do? I don't want to make reports stateful because I really don't see them as a resource until they are actually saved. If I make them stateful I'll need to manage a map or cache of them in my service and deal with multiple clients trying to give their report the same name, implement security to ensure that a user can only see his own reports, etc. All these problems go away with a stateless approach. The side effects, however, are the aforementioned issues. Did I go wrong somewhere? Please help me tackle this design issue before it becomes a permanent part of my API. Thanks.

答: Who says that a service layer should contain no logic of its own, and instead just delegate to domain objects? That sounds like much too strict a rule to me. it's quite clear that in this case the logic belongs in the service layer, as it does not relate to any object that is persisted as part of the model. Instead it looks to me like you've discovered a utility abstraction, which you may want to consider piling out as a separate service to anything that persists the reports.</p>


问:
Why doesn't array declaration guarantee proper memory allocation whereas malloc does?
In C++,
when I declare an array like int array[10];,
it sometimes uses memory assigned to other variable where as this problem is non-existent when I use an integer pointer and malloc to allocate memory to it. Why does this happen?

答:
This looks like a Stack Overflow (hehe).

When you allocate on the stack, there are other entities present surrounding the allocated array (in the memory with an address immediately larger than the array). Since you said in your comment "after few iterations the value of n suddenly changes to something garbage" I assume that n is declared in your code after the array:

```<code>int array[10];
...
int n;
</code>```

Not before the array:

```<code>int n;
...
int array[10];
</code>```

This doesn't look like a "proper memory allocation" issue, but accessing the array with an invalid index, and writing in overflow. I think your memory corruption is present when you use malloc as well, but since you do not have memory allocated right after the array (when using malloc), the memory corruption affects areas of unallocated memory (and this is not visible to you).

Here's an example of overflow (and a wild guess on my part):

You say you allocate array of n*n elements and iterate from 0 to n*n - 1. Are you sure? If you use two imbricated for loops, do not do this:

```<code>for(int i = 0; i <=n; ++i) // this is incorrect
    for(int j = 0; j < n; ++j)
        // access at i, j
</code>```

This doesn't iterate n*n, but [(n+1) * n] - 1 (and would cause an overflow).

Body:
This looks like a Stack Overflow (hehe).

When you allocate on the stack, there are other entities present surrounding the allocated array (in the memory with an address immediately larger than the array). Since you said in your comment "after few iterations the value of n suddenly changes to something garbage" I assume that n is declared in your code after the array:

int array[10];
...
int n;

Not before the array:

int n;
...
int array[10];

This doesn't look like a "proper memory allocation" issue, but accessing the array with an invalid index, and writing in overflow. I think your memory corruption is present when you use malloc as well, but since you do not have memory allocated right after the array (when using malloc), the memory corruption affects areas of unallocated memory (and this is not visible to you).

Here's an example of overflow (and a wild guess on my part):

You say you allocate array of n*n elements and iterate from 0 to n*n - 1. Are you sure? If you use two imbricated for loops, do not do this:

for(int i = 0; i <=n; ++i) // this is incorrect
    for(int j = 0; j < n; ++j)
        // access at i, j

This doesn't iterate n*n, but [(n+1) * n] - 1 (and would cause an overflow).


问:
why Web API 2 has RouteConfig.cs file?
I m working on Web API project, and it is using WebApiConfig.cs file to defined routs for Web API.

Web API fold RouteConfig.cs file. I have googled and they say RouteConfig.cs is for MVC routing. It is actually a confusing statement. Can you please guide why WebAPI has RouteConfig.cs file ? Why would one need MVC routing in Web API ? What function it will do ?

Thanks for your help and guidance.

答:
The RouteConfig.cs file isn't required for Web API. It is included by default in new Web API projects because that template also includes an MVC webpage (for the index page that appears when you open the project in a browser).

If you delete both the RouteConfig.cs and the HomeController.cs file (as well as modify Global.asax.cs to not call RouteConfig.RegisterRoutes()), then your project will still compile and the API controllers will still work fine, you will just be missing a pretty homepage.

Note also that MVC Areas have their own MVC routing and do not use the global MVC routing, which is why the /Help page still works even when RouteConfig.cs has been removed.

Title: why Web API 2 has RouteConfig.cs file?


问:
Model object and additional attributes
I have a model e.g. Book. Besides 'natural' attributes of a book, I have some mandatory IDs I need to carry on, like companyId, groupId. They are related to which organization unit this book belongs to.

Now, I see these two flags like something that is not part of the Book interface. Since all the work with a book is done in the context of one organization unit. In other words, organization unit is always known for every operation out there. Yet, we need to store these data to database and have them in the model (for other modules as well).

[A] Should I put these attributes part of the Book? Then they become mandatory flags and must be set in the constructor of the Book. Meaning, I need to set them every time i create a book. Which somehow I don't like, as these fields belong to the organizational units, which Book should not know anything about.

[B] Should I put these attributes in some 'thread local context? ThenBook` can not be used without the thread local, and this technical dependency is not visible from the outside.

[C] I can store them in the Book fields, but not expose it to the Book interface. However, when I send Book to some other module, that module must be aware of these IDs. Should I use reflection - but then, I must be sure that fields are named the same.

So I wonder if it is possible somehow to have these fields with the Book but not to become part of the Book interface.

答:
Introductory texts about object oriented design always focus on these "natural" objects that are modeled by code. This, I think, is usually a mistake, and results in a lot of confusion when trying to implement an architecture, since you're trying to model the wrong thing.

An object oriented architecture deals with types and classes in your solution's problem domain. If you're building a book-tracking application, you might not really interested in many "natural" qualities of a book, like the typeface it's printed in or the condition of its spine, but rather in those qualities that are relevant to your application - its name and author, certainly, but perhaps also its owner and current location, both relevant to the entity Book in your domain context, even if not part of some platonic ideal of a book.

So I say, go ahead and add properties like CompanyId to your Book model, if that's the relevant entity you're trying to model. If that seems jarring, stop calling it a Book - call it a ManagedBook or a TrackedBook, and suddenly you see that those properties do make sense.

Title: Model object and additional attributes


答:
You said it yourself, there is some kind of context, in which every book is handled. This OrganizationContext might be a class. And every time you load or save a book, this class knows about it and can do it's own logic on those field. Also, to make the coupling looser you could have HasOrganizationContext interface to represent those IDs and allow for different entities to belong to this context and not just Books. Then, other modules could know about this context, but don't need to know about book itself.

Also, if you use C#, you could use explicit interface implementation to "hide" the fields from view, while still making them visible to those who know about the interface.

Title: Model object and additional attributes

Body:
I have a model e.g. Book. Besides 'natural' attributes of a book, I have some mandatory IDs I need to carry on, like companyId, groupId. They are related to which organization unit this book belongs to.

Now, I see these two flags like something that is not part of the Book interface. Since all the work with a book is done in the context of one organization unit. In other words, organization unit is always known for every operation out there. Yet, we need to store these data to database and have them in the model (for other modules as well).

[A] Should I put these attributes part of the Book? Then they become mandatory flags and must be set in the constructor of the Book. Meaning, I need to set them every time i create a book. Which somehow I don't like, as these fields belong to the organizational units, which Book should not know anything about.

[B] Should I put these attributes in some 'thread local context? ThenBook` can not be used without the thread local, and this technical dependency is not visible from the outside.

[C] I can store them in the Book fields, but not expose it to the Book interface. However, when I send Book to some other module, that module must be aware of these IDs. Should I use reflection - but then, I must be sure that fields are named the same.

So I wonder if it is possible somehow to have these fields with the Book but not to become part of the Book interface.


问:
Algorithm for a UI showing X percentage sliders whose linked values always total 100%
A system I'm building includes a set of UI sliders (the number varies) each with a scale of 0-100. By slider I mean a UI where you grab an element and drag it up and down, like a volume control. They are connected by an algorithm that ensures they always total 100. So when one slider is moved up, the others all move down, eventually to zero. When one is moved down, the others move up. At all times the total must be 100. So here sliders have various values, but they total 100%:

```<code>----O------ 40
O----------  0
--O-------- 20
--O-------- 20
--O-------- 20
</code>```

If the first slider then moves UP from 40 to 70, the others have to move DOWN in value (as the slider is dragged). Note three sliders changed from 20 to 10, and one stayed at zero as it cannot go lower.

```<code>-------O--- 70
O----------  0
-O--------- 10
-O--------- 10
-O--------- 10
</code>```

Of course when any slider reaches 0 or 100, it cannot move any further, which is where my head really starts to hurt. So if a slider moves higher, the others move lower, but when any of them reach zero only the remaining ones that have not yet reached zero can move lower.

I'm asking this here as this question is specific to the algorithm not the implementation. FWIW the platform is Android Java, but that's not especially relevant.

The approach I took with my first stab was to calculate the percentage change of the slider that moved. I then split that change and applied it (in the other direction) to the other slider values. The problem though is that by using percentages and multiplying, if any slider gets to zero, it can never be increased again from zero - the net result of that is that individual sliders get stuck at zero. I've used sliders with a range of 0 - 1,000,000 to avoid rounding issues and that appears to be helpful, but I've yet to create an algorithm that handles all scenarios well.

答:
Greedy Algorithm

When a slider moves up (down), all others need to move down (up). Each one has some space it can move (for down - their position, for up: 100-position).

So when one slider moves, take the other sliders, sort them by the space they can move, and simply iterate through them.

On each iteration, move the slider in the needed direction by (total to move left / sliders left in queue) or the distance it can move, whichever is smaller.

This is linear in complexity (since you can use the same ordered queue over and over, once it has been sorted).

In this scenario, no sliders get stuck, all try to move as much as they can, but only up to their fair share.

Weighted move

A different approach would be to weight the move they need to do. I think this is what you tried to do, judging by your "sliders get stuck at 0" statement. IMHO this is more natural, you just need to do more tweaking.

Speculating again, I would say you try to weight the moves of different sliders by their position (This would directly translate to your stuck at 0 problem). However note that you can view the slider position from different directions - from the start or from the end. If you weight by position from start when decreasing and position from end when increasing, you should avoid your problem.

This is quite similar in terminology to the previous part - don't weight the move to make by the position of the sliders, weight it by the space they have left to move in that direction.


问:
What should every programmer know about programming?
Please, stay on technical issues, avoid behavior, cultural, career or political issues.

答:
Documentation is very important. More so if you are building something from scratch. It helps to document your ideas before writing any code.

I learned this the hard way.


问:
DDD and collections and paging of related models?
I have a rich model, where e.g. one model A depends on many related entities/value objects. Therefore we have methods in A for retrieving collections of related objects: getFoos(), getBars() and so on.

At one point, one or many of these related collections should not be fetched eagerly, and, moreover, should be fetched using pagination.

I do not want to 'pollute' the model with methods like getFoosPage(from, to, size). This is not part of the business, it's part of the viewing problem.

How should I solve the pagination?

Next, sometimes I need to get just A with Foos, but not Bars. Should I have a method in my repo saying: getAWithFoos()? I do not like that are returned A will be only partially populated (no boos), and you can not tell that just by inspecting the model.

For now, I am thinking in building a 'query' model, where I would have different classes for scenarios I need, like 'AWithFoos` that contains A and related Foos and so on. Does that make sense?

答:
You should consider the Lazy Load pattern If you wish to fetch items only per request

As for the need of the view. I used a different solution.
I noticed that on sophisticated views I need to load lots of entities which then in turn loaded their children. Way too much work on the DB / process memory.
So I created a Report object that basically extend the object that can populate result set from SQL result (in my case it was ZF2 ResultSet). Then all I had to do was:

Set the SQL (left join or whatever, I call views that hide complexity)

Initiate the object (passing adapter)

Work on the result set

Its a simple one query, read only solution.

```<code><?php

namespace MyProj\Mapper\Reports;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;

class AccountsWithGroupsUsersCount extends ResultSet
{
    protected $sql = 'SELECT * FROM v_accounts_with_groups_and_users_count';

    /**
     * Execute the script and initilize the users list
     * @param int $accountId
     * @param Adapter $dbAdapter
     */
    function __construct(Adapter $dbAdapter)
    {
        parent::__construct();
        $stmt   = $dbAdapter->query($this->sql);
        $result = $stmt->execute();
        $this->buffer()->initialize($result);
    }

}
</code>```

Body:
You should consider the Lazy Load pattern If you wish to fetch items only per request

As for the need of the view. I used a different solution.
I noticed that on sophisticated views I need to load lots of entities which then in turn loaded their children. Way too much work on the DB / process memory.
So I created a Report object that basically extend the object that can populate result set from SQL result (in my case it was ZF2 ResultSet). Then all I had to do was:

Set the SQL (left join or whatever, I call views that hide complexity)

Initiate the object (passing adapter)

Work on the result set

Its a simple one query, read only solution.

<?php

namespace MyProj\Mapper\Reports;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;

class AccountsWithGroupsUsersCount extends ResultSet
{
    protected $sql = 'SELECT * FROM v_accounts_with_groups_and_users_count';

    /**
     * Execute the script and initilize the users list
     * @param int $accountId
     * @param Adapter $dbAdapter
     */
    function __construct(Adapter $dbAdapter)
    {
        parent::__construct();
        $stmt   = $dbAdapter->query($this->sql);
        $result = $stmt->execute();
        $this->buffer()->initialize($result);
    }

}


答:
What about Round Robin approach?
Create a transaction that insure that adding value to one slider will reduce from its peer. and vice versa.

Then every time a slider change run the transaction with a different peer slider (I would create an iterator that return the peer slider in turns).
If the peer slider is zero then proceed to the next one.

Title: Algorithm for a UI showing X percentage sliders whose linked values always total 100%

Body:
A system I'm building includes a set of UI sliders (the number varies) each with a scale of 0-100. By slider I mean a UI where you grab an element and drag it up and down, like a volume control. They are connected by an algorithm that ensures they always total 100. So when one slider is moved up, the others all move down, eventually to zero. When one is moved down, the others move up. At all times the total must be 100. So here sliders have various values, but they total 100%:

```<code>----O------ 40
O----------  0
--O-------- 20
--O-------- 20
--O-------- 20
</code>```

If the first slider then moves UP from 40 to 70, the others have to move DOWN in value (as the slider is dragged). Note three sliders changed from 20 to 10, and one stayed at zero as it cannot go lower.

```<code>-------O--- 70
O----------  0
-O--------- 10
-O--------- 10
-O--------- 10
</code>```

Of course when any slider reaches 0 or 100, it cannot move any further, which is where my head really starts to hurt. So if a slider moves higher, the others move lower, but when any of them reach zero only the remaining ones that have not yet reached zero can move lower.

I'm asking this here as this question is specific to the algorithm not the implementation. FWIW the platform is Android Java, but that's not especially relevant.

The approach I took with my first stab was to calculate the percentage change of the slider that moved. I then split that change and applied it (in the other direction) to the other slider values. The problem though is that by using percentages and multiplying, if any slider gets to zero, it can never be increased again from zero - the net result of that is that individual sliders get stuck at zero. I've used sliders with a range of 0 - 1,000,000 to avoid rounding issues and that appears to be helpful, but I've yet to create an algorithm that handles all scenarios well.


问:
Should I hide certain HTTP status codes?
So I have different seniors taking different positions on this.  Some saying hide nothing, those codes are there for a reason.  Others saying to hide most status codes, reducing the number to a much smaller group (like only 400 and 404).  Even RFC 2616 suggests a few substitutions are allowed (403 being treated as a 404 for a resource the web server will never deliver and will never explain why).

I'm not talking about the error page the user sees.  Of course that should look nice for the user and should not dump a stack trace, ect.  I'm talking about the http status included in the response.  Meaning if they try to access a resource they aren't authorized for, instead of giving a 401/403, actually making the response be a 404 so that there is no way to know there was something there they weren't authorized for.

Is there a clear cut answer on which is the better practice (kinda like how it is best practice to not reroll your own password hashing)?

答:
No, there's no pre-defined set of "must-have" HTTP status codes. Out of the 40-or-so status codes listed in RFC 2616 I put together a list of codes that, in my experience, are the ones you should consider using to their full extent.

200, 201

302, 304

400, 403, 404

500

8 status codes, not that bad. Depending on your particular application, there are others that may be helpful (202, 503, etc) but the majority of status codes (particularly in the 1xx/4xx/5xx groups) are only useful in specific conditions or will be handled by your application server.

In short, use the codes that match the semantics of your application's API.

To address your specific example of replacing 401/403 with 404, that's a special case where revealing the existence of a resource is itself a security risk. There are few applications where this behavior is necessary.

GET /users/admin/edit should return 403.

GET /docs/top-secret/missile-launch-codes should return 404.


答:
The approach I'd take is slightly different, and involves using a different internal representation of each slider.

each slider can take any value from 0..100 (X) which is used as a weighting factor for that slider (not a %)

add up all the slider values to get the total figure (T)

to determine each slider's displayed value, use ROUND(X * 100 / T)

when a slider is moved up or down, you only change the one slider's value; the weighting for that slider will increase or decrease relative to all the other sliders, and the above calc will ensure that the change to all the other sliders will be spread as evenly as possible.


答:
I think you're over-complicating things by trying to adjust the current value by a percentage of the change of the 'moved' slider, which gives you percentages of percentages, which is introducing rounding errors.

Since you know you're only ever dealing with a total value of 100, I'd keep things as integers and work backwards from the 100, avoiding any serious messing about with rounding. (In the below example I handle any rounding as whole integers at the end)

My technique would be to set the sliders as simple 0-100. Subtract the 'new' value from 100 to work out how much to redistribute, spread that between the other sliders according to their weight, then clean up)

This is not, as far as I'm aware, valid Android code :p

```<code>// see how much is "left" to redistribute
amountToRedistribute = 100 - movedSlider.value

//What proportion of the original 100% was contained in the other sliders (for weighting)
allOtherSlidersTotalWeight = sum(other slider existing values)

//Approximately redistribute the amount we have left between the other sliders, adjusting for their existing weight
for each (otherSlider)
    otherSlider.value = floor(otherSlider.value/allOtherSlidersWeight * amountToRedistribute)
    amountRedistributed += otherSlider.value

//then clean up because the floor() above might leave one or two leftover... How much still hasn't been redistributed?
amountLeftToRedistribute -= amountRedistributed

//add it to a slider (you may want to be smarter and add in a check if the slider chosen is zero, or add it to the largest remaining slider, spread it over several sliders etc, I'll leave it fairly simple here)
otherSlider1.value += amountLeftToRedistribute
</code>```

This should intrinsically handle any 0 or 100 value

Body:
I think you're over-complicating things by trying to adjust the current value by a percentage of the change of the 'moved' slider, which gives you percentages of percentages, which is introducing rounding errors.

Since you know you're only ever dealing with a total value of 100, I'd keep things as integers and work backwards from the 100, avoiding any serious messing about with rounding. (In the below example I handle any rounding as whole integers at the end)

My technique would be to set the sliders as simple 0-100. Subtract the 'new' value from 100 to work out how much to redistribute, spread that between the other sliders according to their weight, then clean up)

This is not, as far as I'm aware, valid Android code :p

// see how much is "left" to redistribute
amountToRedistribute = 100 - movedSlider.value

//What proportion of the original 100% was contained in the other sliders (for weighting)
allOtherSlidersTotalWeight = sum(other slider existing values)

//Approximately redistribute the amount we have left between the other sliders, adjusting for their existing weight
for each (otherSlider)
    otherSlider.value = floor(otherSlider.value/allOtherSlidersWeight * amountToRedistribute)
    amountRedistributed += otherSlider.value

//then clean up because the floor() above might leave one or two leftover... How much still hasn't been redistributed?
amountLeftToRedistribute -= amountRedistributed

//add it to a slider (you may want to be smarter and add in a check if the slider chosen is zero, or add it to the largest remaining slider, spread it over several sliders etc, I'll leave it fairly simple here)
otherSlider1.value += amountLeftToRedistribute

This should intrinsically handle any 0 or 100 value


问:
Do you maintain your own utilitiy library
So, i've started working as a freelancer and therefore find myself copy/pasting util methods from one class to another. Not good to say the least.

I do this on my mobile frontend dev and backend .net.

I'm thinking of creating a Utility library for each platform I develop on. Then, when I start a new project drop the library in and I have instant access to helpers for e.g. persisting kvp settings, Basic ORM, generating HMACs for API requests etc..

Obviously this is a good idea and i'm not asking should I do it, more about any tips people would have or the best way to integrate and maintain.

I'm basically putting this out there to here what others do so it might give me some ideas when I start.

Thanks

答:
Yes, with the caveat that I explicitly state in any contract that:

any utility code remains my property, with a perpetual licence for the client to use that code within the confines of the project in which it was supplied

the client does not have an exclusive licence for use of that code, I reserve the right to re-use the utilities for other clients

the above does not affect any code written specifically for the client, for which they have an exclusive license (if applicable and paid for)

This neatly gets around any problems of exclusive licenses - the client exclusively gets any new code written for them, but not my utilities which I make clear are re-used, non project specific and keep the costs down for them as I don't have to re-write generic code.

The perpetual license for the life of their project avoids any "what if he withdraws permission" worries, and is harmless to me as they're just basic utilities with no real value.

I make it clear in the source and contract exactly which parts are my utility library and which are their code.

Note that if you only ever grant a licence to use your code or have a non exclusive agreement, the above is irrelevant :)


答:
The example from the lecture is a heuristic, a rule of thumb and nothing more. That should be implicitly obvious.

Heuristics are like anything else: they are subject to a certain context and dependent on any number of unstated assumptions, and their usefulness can be very non-deterministic. As much arbitrary judgement goes into finding them useful as goes into formulating them in the first place.

Does that means that they are without worth? I wouldn't say so at all.

Heuristics are one of the approaches that we can take toward addressing NP-complete problems, and in many respects, software engineering is itself an NP-complete problem.


问:
Which data structures and algorithms should I consider for a directed rooted tree that has high churn?
I need to represent a directed rooted tree in memory.

What would be a good data structure and algorithms for performing main actions, given the particulars listed below?

Size: ~40,000 nodes. But ideally should scale well to 10x the size or more.

Arity: Very variable. Some nodes have 2-3 children. Some have 10. Some (rarer) have 1000.

Data is NOT static - throughout the day, ~1-3% of the nodes/edges will be added/removed/moved to different parts of the tree (for a good real life example, let's pretend it's a filesystem tree where people are very active, and has symlinks).

For the purposes of notation:

D(V) - depth of the vertex (distance from the root).

A(V) - arity of the vertex (how many children it has).

V - Size of the tree (how many total vertices)

The following actions are needed (with desired speed of action indicated).

Ability to add a single vertex or a new subtree

O(N) where N is the amount of vertices being ADDED (not already in the tree) and O(D(V)) where V is the vertex where new nodes are attached.

Ability to delete a subtree

O(N) where N is the amount of vertices being deleted and O(D(V)) where V is the vertex where new nodes are deleted from.

Ability to move a subtree to another parent elsewhere in the tree

O(N) where N is the amount of vertices being moved and O(D(V)) where V is the vertex where new nodes are moved between.

Note that due to retrieval needs described below, I do NOT anticipate that you can do this in O(1) independent of how many nodes are being moved, like in a regular basic tree data structure with child node pointers.

Ability to find a node by its name (needed for all the subsequent steps)

Ideally, O(1). Most definitely much less than O(log V).

Ability to know the full path up from the vertex to the root

O(1) ideally. But definitely less than O(D(V)). This is very important; and is the reason why standard linked implementations of the tree won't work, even with 2-way links.

答:
As you said it is similar to a file system, why can't you actually use the Windows file system. Make each node an extentionless file (to save memory) and put them in folders. Once you index the locations, Windows will use the best possible algorithm to find the required subtrees (folders).

Using a hash table with double hashing with 1st function based on any 3-5 random digits of the name and 2nd based on all the rest of the characters.


问:
Peer review for tests just like code reviews
Does anyone practice the "code review" process for functional tests? Do you find it useful? The way that my current employer practices SCRUM we include functional testing as part of our "must have done" stuff in any given sprint.</p>

答:
You can do pair inspections!

Pair Inspections are:

A review of documents actively & informally as part of the authoring & document production cycle.

The reasons why this works well with testing is that:

You can frequently inspect requirements or documents with more than one pair of eyes.

You can involve more than just developers: Try BA with Test Lead, BA with a PM, BA with Dev.

You can set re-curing meetings as part of an Agile process – Make sure to be serious about it with solid commitments from team members.

You can use these pair inspections as part of a rapport-building exercise and communication exercise with your stakeholders. Get the conversation going!


答:
The "query model" you mention totally makes sense, actually there's a whole approach based on that (CQRS). Query side would expose Read Model Facades for all the fancy tailored display models you need, and command side has Repositories for business transactions through Aggregates.

In a traditional non-CQRS DDD app, I wouldn't be shocked to see paging and filtering parameters in repo methods though, since they are also your query objects.

You can also have a look at the Rules of Effective Aggregate Design by Vaughn Vernon. Through smaller Aggregates and more careful domain design, it addresses the kind of entity loading and performance issues you seem to experience.

Title: DDD and collections and paging of related models?

Body:
I have a rich model, where e.g. one model A depends on many related entities/value objects. Therefore we have methods in A for retrieving collections of related objects: getFoos(), getBars() and so on.

At one point, one or many of these related collections should not be fetched eagerly, and, moreover, should be fetched using pagination.

I do not want to 'pollute' the model with methods like getFoosPage(from, to, size). This is not part of the business, it's part of the viewing problem.

How should I solve the pagination?

Next, sometimes I need to get just A with Foos, but not Bars. Should I have a method in my repo saying: getAWithFoos()? I do not like that are returned A will be only partially populated (no boos), and you can not tell that just by inspecting the model.

For now, I am thinking in building a 'query' model, where I would have different classes for scenarios I need, like 'AWithFoos` that contains A and related Foos and so on. Does that make sense?


问:
How to design URIs to initate batch jobs in a RESTful service
If I want to run a job at the end of the month to calculate the commission for all the sales consultants based on the current transaction data, eg commission rate for the person, sales done in the month, and payments received from their customers, that becomes potentially a complex batch operation.

My existing resources are something like:

/consultant <-- Details of each sales consultant

/payment <-- transactions of moneys received from customers

/customer <-- Details of customers in the file

/order <-- Something that translates into an invoice, refers to a sales consultant and to a customer, as well as other things, eg what service and goods were sold on that order

What will be my batch initiating URI?  I'm thinking either
/consultant?filter=have-made-sales&action=generate-commission-report

But it doesn't feel right, so the other way would be to treat "batchjobs" as a virtual resource, eg

/batchjobs?jobname=consultants-comission

/batchjobs?jobname=email-invoices

Which allows me to return a job-id, which I can use in a subsequent GET to find out whether the job is done, eg
/batchjobs?jobid=123

Another resource which isn't a resource is what to do about Reports.  These are quite similar to Batch.  When the client requests that a report be generated, the service should return OK 200 and a few minutes later the PDF report should sit in the requestor's email inbox.  In this case I may name my URIs as follow:

/reports?report=sales-for-the-month

/reports?report=accounts-outstanding

Or is there a better, more acceptable alternative?

It seems the problem I'm facing is about Resource vs Operation.  The issue is that batch operations affects many resources, so to be RESTFUL a client should GET all the resources's state, perform the batch, then PUT the updated state for each and every resource.  There is a performance issue, particularly since the internet introduce unacceptable latency, and as result a risk to data integrity (race conditions - PUT should be idempotent)  There is little to prevent another client from PUT'ing something else to one of the affected resources while the batch is being processed, or even immediately after, so undoing the batch work for once specific resource.  Locking resources seems to be unRESTful.  So how does one do this?

答:
You could POST /salesreport with { "start" : <START>, "end": <END> }.

That would  give you a 201 Created, Location: /salesreport/<ID>

When you first GET it will be { "start" : <START>, "end": <END>, "status": "pending", "progress": <PROGRESS> } (consider using cache directives to limit client polling rate).

You can use DELETE to abort a report generation while it is underway.

Batch jobs related to a specific resource type could be nested accordingly, i.e. the URL could be /order/salesreport or /consultant/commission/ or whatever.

I don't think it is wise to expose long running operations through an API anyway, but that's a whole different story.

OwnerDisplayName: Johan


问:
No repeated coordinates in random walker ensemble
i'm simulating an ensemble of random walkers in 2D in python, I set [x,y] coordinates of my walkers to be gaussian in a 2D grid, then use a rand array of -1 and +1 to move up down left right:

```<code>N=20 #number of walkers

Coor[:,1]=100*random(N) #X's coordinates
Coor[:,2]=100*random(N) #Y's coordinates
</code>```

To do the simulation more realistic I want to impose that none of the walkers could have the same X Y coordinates as another at the same time, so as example if at certain time after random moves my coordinates array looks like this:

```<code>Coor[:,1]=([1,3,5,7,7,8,....])
Coor[:,2]=([4,8,3,2,2,9,....])
</code>```

then the 4th and 5th elements have the same coordinates in X and Y so How I find when that happens and change one of the values to don't be in that situation?

答:
Instead of updating the coordinates by adding a randomized vector of -1 and +1 to them, you could simply iterate over walkers and select a random direction from the currently available ones. This way your system never gets into invalid state so no extra checks are needed. I cannot prove it but I suspect that pure vectorized solution for the problem might not even exist.

Also, your current solution only gives the walkers 4 possible directions (the diagonals) to move to. I'm not sure if this was the intention.


问:
Determining a class structure
I'm currently in the early stages of designing an e-commerce application.  I'm working out some designs for my classes which have so far been fairly straightforward, but I've run into something of a problem which I suspect is related to the Circle/Square problem.  The following (simplified) class diagram outlines what I've designed so far

There are two classes of sellable products in the system

Physical products representing an object that exists through the real world (Bikes, Lego, CDs, etc)

Downloadable products representing something you can purchase over the web (MP3s, eBooks, etc)

A physical product has attributes such as shipping weight

A downloadable product has attributes such as file type and size

Products can themselves be made up of products (components).  An album is composed of tracks, a bike is composed of frame, wheels, handlebars, gears, etc, each of which is a product in its own right

A third type of product, a product bundle, is for grouping otherwise unrelated products together for selling as a single unit (A bike could be bundled with a pump, helmet, etc, a collection of eBooks could be bundled together, and so on).

The Product Bundle is where the difficulties are springing up.  A bundle of physical products would have things such as the shipping weight being the total shipping weights of all the products that make it up, a stock level equal to the level of the composing product with the least stock, etc.  A bundle of downloadable products would have a total file size of all the files that make up the bundle, a file name list composed of all the files in the bundle and so on.

But what about a bundle that comprises both physical and digital products?  Suppose for example you want to provide a digital download of a movie when someone buys the same movie on DVD, or you want to give an eBook about the Tour De France away with a bicycle, now there's a mixture of properties that put the bundle in neither the physical or digital product grouping.

I've thought of the following solutions but none seem ideal:

Put all the properties applicable to both in the superclass

The Bundle class can easily override methods defined on the superclass to be able to compute the composite values (sum all product weights together, etc)

This seems like a really poor encapsulation

You'll have methods on physical products that are only really applicable to digital ones and vice versa

The superclass starts to become a "god object"

The storage requirements for objects mean that you always need to store attributes that don't apply to the actual type of object (you're always storing a shipping weight of 0 for downloads, a filesize of 0 for physical products, etc)

Implement abstract methods in the Product class, override them in the subclasses to do something where appropriate, throw an exception where not

Doesn't really solve the encapsulation problem

Code that deals with products now has to be prepared to determine actual class of object being processed, deal with exceptions, etc

Possible violation of the liskov substitution principle?

As above but return default values where the method is not appropriate (0 for weight on a digital product, 0 for filesize on physical products, etc)

Code no longer has to deal with exceptions or class

Could lead to unforeseen logical errors when trying to process products in inappropriate ways

Implement physical properties/methods in physical product class, implement file properties/methods in digital product class, both in bundle class with interfaces to tell them all apart (physical implements one, digital implements another, bundle implements both)

Better encapsulation

Adds a fair chunk of boilerplate code (interfaces to be implemented, etc)

The Bundle class will still need to be aware of what type its component classes are

The problem of what the Bundle class should do when methods that aren't appropriate for a given circumstance remains.  Should getShippingWeight return 0 or throw an exception if the bundle's products are all digital?

Some other solution that hasn't occurred to me yet

????

Most of the above approaches would still require the Bundle class to do additional work to determine the types of its constituent parts.

Basically, is there an approach of the ones highlighted above that would be preferable to address these issues, or is there some other approach from these that could be better applied to get a good design that doesn't overload a base class with too much functionality and doesn't leave considerable special cases that need to be dealt with elsewhere?  Any input is appreciated.

答:
I would expect the bundle class to have 2 lists, I am assuming that the downloadable and physical products are in 2 different tables with 2 different sets of keys.  So you can have you 2 separate lists.

I would have a method in the product bundle that returns an object with total digital items, total digital size, total physical items and total physical weight, 0 being perfectly legitimate answers to return and can be filtered out for different display purposes.  Though if you prefer you can have one method for each value.

Title: Determining a class structure


答:
There are several ways to approach this depending on what tradeoffs you want to make.

One such approach would be to have a set of classes that accept a Product and provide different information from it. For example, a ShippingWeightViewAdapter which knows how to get the shipping weight from any Product or subclass, providing an aggregate value where appropriate.

This is a similar approach to some MVC frameworks, e.g. Eclipse. You can have a window that provides information on some object, and another object that adapts it for the view. For example, by filtering compiler errors based on what is selected. If you select an object that is not compiled (e.g. a plain text file) it simply shows nothing.

The same principle applies here. A view adapter for the shipping weight could see that a product has no weight, and assume zero. Maybe that product is bundled with another product that does have a shipping weight, and the view can handle that by adding up the shipping weight only for physical products.

This approach has several advantages:

It is extensible. Add a new property? Add a new view adapter to go along with it. If this is plugging into some web framework, you might not even need much code.

Products can stick to the single responsibility principle. They contain data, and do not need to know how to aggregate data as well.

View adapters also have a single responsibility: aggregate a single piece of data.

Different types of products do not need to know about each other. There is no need to keep default method implementations in the superclass, nor do subclasses need to "return 0" for properties they do not have. Each class has its own and only its own properties.

The view adapter classes can be plugged into arbitrary other classes: they have interface segregation (the I in SOLID) which helps keep them loosely coupled.

Title: Determining a class structure

Body:
I'm currently in the early stages of designing an e-commerce application.  I'm working out some designs for my classes which have so far been fairly straightforward, but I've run into something of a problem which I suspect is related to the Circle/Square problem.  The following (simplified) class diagram outlines what I've designed so far

There are two classes of sellable products in the system

Physical products representing an object that exists through the real world (Bikes, Lego, CDs, etc)

Downloadable products representing something you can purchase over the web (MP3s, eBooks, etc)

A physical product has attributes such as shipping weight

A downloadable product has attributes such as file type and size

Products can themselves be made up of products (components).  An album is composed of tracks, a bike is composed of frame, wheels, handlebars, gears, etc, each of which is a product in its own right

A third type of product, a product bundle, is for grouping otherwise unrelated products together for selling as a single unit (A bike could be bundled with a pump, helmet, etc, a collection of eBooks could be bundled together, and so on).

The Product Bundle is where the difficulties are springing up.  A bundle of physical products would have things such as the shipping weight being the total shipping weights of all the products that make it up, a stock level equal to the level of the composing product with the least stock, etc.  A bundle of downloadable products would have a total file size of all the files that make up the bundle, a file name list composed of all the files in the bundle and so on.

But what about a bundle that comprises both physical and digital products?  Suppose for example you want to provide a digital download of a movie when someone buys the same movie on DVD, or you want to give an eBook about the Tour De France away with a bicycle, now there's a mixture of properties that put the bundle in neither the physical or digital product grouping.

I've thought of the following solutions but none seem ideal:

Put all the properties applicable to both in the superclass

The Bundle class can easily override methods defined on the superclass to be able to compute the composite values (sum all product weights together, etc)

This seems like a really poor encapsulation

You'll have methods on physical products that are only really applicable to digital ones and vice versa

The superclass starts to become a "god object"

The storage requirements for objects mean that you always need to store attributes that don't apply to the actual type of object (you're always storing a shipping weight of 0 for downloads, a filesize of 0 for physical products, etc)

Implement abstract methods in the Product class, override them in the subclasses to do something where appropriate, throw an exception where not

Doesn't really solve the encapsulation problem

Code that deals with products now has to be prepared to determine actual class of object being processed, deal with exceptions, etc

Possible violation of the liskov substitution principle?

As above but return default values where the method is not appropriate (0 for weight on a digital product, 0 for filesize on physical products, etc)

Code no longer has to deal with exceptions or class

Could lead to unforeseen logical errors when trying to process products in inappropriate ways

Implement physical properties/methods in physical product class, implement file properties/methods in digital product class, both in bundle class with interfaces to tell them all apart (physical implements one, digital implements another, bundle implements both)

Better encapsulation

Adds a fair chunk of boilerplate code (interfaces to be implemented, etc)

The Bundle class will still need to be aware of what type its component classes are

The problem of what the Bundle class should do when methods that aren't appropriate for a given circumstance remains.  Should getShippingWeight return 0 or throw an exception if the bundle's products are all digital?

Some other solution that hasn't occurred to me yet

????

Most of the above approaches would still require the Bundle class to do additional work to determine the types of its constituent parts.

Basically, is there an approach of the ones highlighted above that would be preferable to address these issues, or is there some other approach from these that could be better applied to get a good design that doesn't overload a base class with too much functionality and doesn't leave considerable special cases that need to be dealt with elsewhere?  Any input is appreciated.


问:
Write data to SQL Server directly from BizTalk or use external service?
An external source will be sending us XML data that BizTalk will pick up and transform into an internal schema.  We need this data to be loaded into a SQL Server database as we're going to expose some of the data to our web front-end via a custom WCF service.

The question is: what is the recommended approach for doing something like this?  Options we're considering are having BizTalk write to the database directly or having BizTalk call a custom WCF service which would handle the save operation.  Another briefly considered idea was having BizTalk write to an MSMQ and have a custom service pull from there and store it in the database.

What are some of the guidelines or questions that should be asked in assessing these options?   There are concerns related to overhead from calling the extra service, duplication of efforts if the schema is modified in the future (which it will be to some extent), and simply the best way to design within a service-oriented architecture that we're struggling with.

答:
If the database is local, you can use the WCF-SQL adapter to directly use stored procedures in send ports and receive locations. A benefit of this is that you can coordinate the database transaction with the BizTalk message transactions as well.

If the database is remote, i would advice using services or a messaging / queueing approach.


问:
Is there an effective way to test XSL transforms/BizTalk maps?
Creating repeatable tests for BizTalk maps is frustrating. I can't find a way to handle testing them like I'd do unit testing, because I can't find ways to break them into logical chunks. They tend to be one big monolithic unit, and any change has the potential to ripple through the map and break a lot of unit tests. Even if I could break it up, creating XML test inputs is painful and error prone.

Is there any effective way of testing these? I'd settle for recommendations for testing XSL transforms in general, but I specifically mention BizTalk maps primarily for the reason that when using the mapper, there really isn't any way to break your XSLT into templates (which I'd imagine you could use to break up your logic into testable chunks, but I've honestly never gotten that far with XSLT).

答:
It depends on what you want to prove with your tests.

If you want to prove that the transform works, you can use one or more input messages, apply the transformation, and use validation on the output message.

More specific validations can be done with XPath checks on specific fields. These kind of tests can be used to validate upto 80% of the working of the transform.

If you want to test specific corner cases (what happens when a node is not present, when this node is empty, etc..), you can create specific test messages, this will help you to reach even higher confidence in the correctness of the transform.

I think that in most cases it is not cost effective to try and test every line and every possible scenario using the transform. Therefore i would focus on the easy cases first, and specific testcases only when  you see a risk, or an actual defect was discovered during testing.

Most important is to have enough real test messages as input. Try to collect as many diffrent possible input messages from the source system, and wrte test code to apply the transform on all these testcases.

I'm currently building a set of transforms, and i'm testing them using (our in-house built) BizTalk Testing Framework. I have recenty added an XmlAssert class that can evaluate node values based on xPath expressions. That works really well. Executing BizTalk maps in unit tests is quite easy.


问:
Is it permissible to use explicit interface implementation to hide members in C#?
I understand how to work with interfaces and explicit interface implementation in C#, but I was wondering if it's considered bad form to hide away certain members that would not be used frequently. For example:

```<code>public interface IMyInterface
{
    int SomeValue { get; set; }
    int AnotherValue { get; set; }
    bool SomeFlag { get; set; }

    event EventHandler SomeValueChanged;
    event EventHandler AnotherValueChanged;
    event EventHandler SomeFlagChanged;
}
</code>```

I know in advance that the events, while useful, would very rarely be used. Thus, I thought it would make sense to hide them away from an implementing class via explicit implementation to avoid IntelliSense clutter.

答:
Yes, it's generally bad form.

Thus, I thought it would make sense to hide them away from an implementing class via explicit implementation to avoid IntelliSense clutter.

If your IntelliSense is too cluttered, your class is too big. If your class is too big, it's likely doing too many things and/or poorly designed.

Fix your problem, not your symptom.


问:
How do you identify bugfix commits on QA environment with Git repository?
I got used to the following approach on bugfixing phase of the project development. The developer should add SVN commit number as a comment to the JIRA issue when resolving it. Latest artifact from CI-server shows build number which include major version, branch name and commit number. This approach helps QA easily to identify whether the bugfix was included in the latest build or not. Even when same issue was reopened and fixed again.

After moving from SVN to Git we lost incremental commit number. It is possible to solve this problem with tagging the code but tags are tied to the sprint numbers. And in case of the issue reopening this approach moves this issue resolving on the next sprint. But client is dissatisfied with the fact of having certain issue unfixed for a several sprints.

So, how do you identify bugfix commits in for QA environment?
Thanks.

UPD: The question is NOT about linking issues to commits, but about ability of QAs figure out whether certain fix (ticket) is included in certain build just by looking on build number and ticket comments.

答: You might want to flip around the work flow here -- rather than making the devs visit a different system to note commit numbers you can have the tracking system pull references out of the commit messages -- JIRA supports specially formed comments that do just this. See this page to get started.</p>


答:
I might be a sign of messy code, but sometimes the real world is messy. One example from .NET framework is ReadOnlyColection which hides the mutating setter [] ,Add and Set.

I.E ReadOnlyCollection implements IList< T>. See also my question  to SO several years ago when i pondered this.

Title: Is it permissible to use explicit interface implementation to hide members in C#?

Body:
I understand how to work with interfaces and explicit interface implementation in C#, but I was wondering if it's considered bad form to hide away certain members that would not be used frequently. For example:

```<code>public interface IMyInterface
{
    int SomeValue { get; set; }
    int AnotherValue { get; set; }
    bool SomeFlag { get; set; }

    event EventHandler SomeValueChanged;
    event EventHandler AnotherValueChanged;
    event EventHandler SomeFlagChanged;
}
</code>```

I know in advance that the events, while useful, would very rarely be used. Thus, I thought it would make sense to hide them away from an implementing class via explicit implementation to avoid IntelliSense clutter.


答:
There's also a Key-Value approach.  Each type of product would have its own set of properties stored as individual key-value items in your database.  So you would not have a shippingWeight property as part of a class.  Rather, you'd request the 'shippingWeight' value as needed.  Some ecommerce systems use this (Magento, I believe) for it's flexibility.  The downside is it can be a PITA to work with.  But I thought the option worth mentioning to add to the list you presented.

Personally, barring any really good reasons to do otherwise, I'd put the fields into the parent class to keep things simple.  The increased storage is unlikely to be a practical problem. But I would move much of the functionality, such as shipping calculations, into other classes like @snowman was describing.

Title: Determining a class structure


答:
Use the feature for what it was intended for. Reducing namespace clutter is not one of those.

Legitimate reasons aren't about "hiding" or organization, they are to resolve ambiguity and to specialize. If you implement two interfaces that define "Foo", the semantics for Foo might differ. Really not a better way to resolve this except for explicit interfaces. The alternative is to disallow implementing overlapping interfaces, or declaring that interfaces cannot associate semantics (which is just a conceptual thing anyway). Both are poor alternatives. Sometimes practicality trumps elegance.

Some authors/experts consider it a kludge of a feature (the methods aren't really part of the type's object model). But like all features, somewhere, someone needs it.

Again, I would not use it for hiding or organization. There are ways to hide members from intellisense.

```<code>[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
</code>```

Body:
Use the feature for what it was intended for. Reducing namespace clutter is not one of those.

Legitimate reasons aren't about "hiding" or organization, they are to resolve ambiguity and to specialize. If you implement two interfaces that define "Foo", the semantics for Foo might differ. Really not a better way to resolve this except for explicit interfaces. The alternative is to disallow implementing overlapping interfaces, or declaring that interfaces cannot associate semantics (which is just a conceptual thing anyway). Both are poor alternatives. Sometimes practicality trumps elegance.

Some authors/experts consider it a kludge of a feature (the methods aren't really part of the type's object model). But like all features, somewhere, someone needs it.

Again, I would not use it for hiding or organization. There are ways to hide members from intellisense.

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]


问:
Original source of `(seed * 9301 + 49297) % 233280` random algorithm?
If you search for examples of creating a seeded (pseudo)Random number generator, you will run into stuff like this (specific example http://indiegamr.com/generate-repeatable-random-numbers-in-js/):

```<code>// the initial seed
Math.seed = 6;

// in order to work 'Math.seed' must NOT be undefined,
// so in any case, you HAVE to provide a Math.seed
Math.seededRandom = function(max, min) {
    max = max || 1;
    min = min || 0;

    Math.seed = (Math.seed * 9301 + 49297) % 233280;
    var rnd = Math.seed / 233280;

    return min + rnd * (max - min);
}
</code>```

Those specific numbers (9301, 49297, 233280) and algorithm are used over and over, but nobody seems to have a definitive reference for this.  Who invented this algorithm and tested the distribution?  Is there a paper or something to cite?

答:
A quick search of Google Books shows these numbers (9301, 49297, 233280) have been used in a number of references:

Numerical Recipes in FORTRAN 77

An Introduction to Numerical Methods in C++

CGI Developer's Resource: Web Programming in TCL and PERL

Effective Fortran 77 for Engineers & Scientists

JavaScript development

All on C

Java Examples in a Nutshell

Seminumerical algorithms

An Introduction To Mechanics

The oldest is 1977's Computer methods for mathematical computations by George Elmer Forsythe, Michael A. Malcolm, Cleve B. Moler (Prentice-Hall), although Google doesn't show where the text was used in the book so it cannot be verified.

The earliest showing the text is Numerical Recipes in Pascal (First Edition): The Art of Scientific Computing, Volume 1 by Press, Teukolsky, Vetterling and Flannery in a full-page table of "Constants for Portable Random Number Generators". These particular numbers are given with an overflow at 2^31.

The Numerical Recipes series of books are hugely popular, and have been in print since 1986.


问:
Process to generate hierarchical structure based on relational data?
I have a csv of employee ids, names, and a reference column with the id of their direct manager, say something like this

```<code> emp_id, emp_name, mgr_id
1,The Boss,,
2,Manager Joe,1
3,Manager Sally,1
4,Peon Frank,2
5,Peon Jill,2
6,Peon Rodger,3
7,Peon Ralph,3
</code>```

I would like to be able to generate a (json) object representing this structure, something along the lines of

```<code>DATA = {
  "parent": "The Boss",
  "children: [
    {
      "parent": "Manager Joe",
      "children": [ {"parent": "Peon Frank"}, {"parent": "Peon Jill"} ]
    },
    {
      "parent": "Manager Sally",
      "children": [ {"parent": "Peon Rodger"}, {"parent": "Peon Ralph" } ]
    }]
}
</code>```

So from the data, an entry with no mgr_id represents something like the CEO or Leader.

So just collecting some thoughts.. I know this could be represented by some tree data structure with the tree traversal generating the correct output. The parser would have to be responsible for inserting into the tree. Maybe the number of children would give you a weight? Just gathering thoughts. Not sure how I would pivot around the fact that multiple objects can be constructed with children.

Is there an algorithm defined that can parse this structure I am not thinking of? Descending down into the children seems relatively straightforward. I am not seeing this in my head, and could use some help. Thank you

答:
You've got a CSV file as your form of persistence.  There are other approaches such as SQLite that you might wish to investigate giving you a bit more ability to query the structure, but you will still have the problem that to build the full tree structure to serialize out, you will need to have the full tree structure.

Working with CSV, you will need to read every line in, build the corresponding tree structure, and then write it back out, possibly with a library for json serialization.  Noting you have a few ruby bits over on SO, that could look something like the answers in Ruby objects and JSON serialization (without Rails) and for reading, the CSV gem.  This is just an example though, such CSV parsers and JSON serialization targets exist for every language of remotely practical value.

Thats what it really boils down to.  Its possible that you could have something that looks like:

```<code>1,The Boss,,
2,Founder Dev,7
3,Manager Joe,1
4,Peon Frank,3
5,Peon Jill,3
6,Peon Rodger,7
7,Manager Sally,1
</code>```

And so, you have to read it all before you can go about creating the json to serialize it to.  There is no way around that.

Now, there is the possibility of a different structure in the csv that allows you do some more useful queries and quickly without needing to build the full three structure to then work with.

The nested set which can also be seen as a modified preorder tree can be useful.

The tree looks like this:

```<code>                1 Boss 14

2 Manager Sally 7       8 Manager Joe 13

3 Dev 4 | 5 Rodger 6    9 Frank 10 | 11 Jill 12
</code>```

And the corresponding file would be written out as:

```<code># name  L   R
Boss,   1,  14
Sally,  2,  7
Dev,    3,  4
Rodger, 5,  6
Joe,    8,  13
Frank,  9,  10
Jill,   11, 12
</code>```

With this tree, one can do things like 'find all the managers' by finding all the lines where R-L > 1.  Or, 'find everyone reporting to Sally' by finding all the lines where L > 2 and R < 7.  The number of people reporting to a given person is (R-L-1)/2. This allows specific questions against the tree to be able to be read much faster (without needing to build the tree itself).

The modified preorder tree does come with the expense that updates to it will touch a large number of lines compared to the adjacency list approach to an org tree.  An insert will shift numbers around all over the place.  For example, if Sally had another person reporting to her, that person would have the value 7,8 and you would need to modify everyone's if(L > 7) L = L + 2 and if (R > 7) R = R + 2.

Body:
You've got a CSV file as your form of persistence.  There are other approaches such as SQLite that you might wish to investigate giving you a bit more ability to query the structure, but you will still have the problem that to build the full tree structure to serialize out, you will need to have the full tree structure.

Working with CSV, you will need to read every line in, build the corresponding tree structure, and then write it back out, possibly with a library for json serialization.  Noting you have a few ruby bits over on SO, that could look something like the answers in Ruby objects and JSON serialization (without Rails) and for reading, the CSV gem.  This is just an example though, such CSV parsers and JSON serialization targets exist for every language of remotely practical value.

Thats what it really boils down to.  Its possible that you could have something that looks like:

1,The Boss,,
2,Founder Dev,7
3,Manager Joe,1
4,Peon Frank,3
5,Peon Jill,3
6,Peon Rodger,7
7,Manager Sally,1

And so, you have to read it all before you can go about creating the json to serialize it to.  There is no way around that.

Now, there is the possibility of a different structure in the csv that allows you do some more useful queries and quickly without needing to build the full three structure to then work with.

The nested set which can also be seen as a modified preorder tree can be useful.

The tree looks like this:

                1 Boss 14

2 Manager Sally 7       8 Manager Joe 13

3 Dev 4 | 5 Rodger 6    9 Frank 10 | 11 Jill 12

And the corresponding file would be written out as:

# name  L   R
Boss,   1,  14
Sally,  2,  7
Dev,    3,  4
Rodger, 5,  6
Joe,    8,  13
Frank,  9,  10
Jill,   11, 12

With this tree, one can do things like 'find all the managers' by finding all the lines where R-L > 1.  Or, 'find everyone reporting to Sally' by finding all the lines where L > 2 and R < 7.  The number of people reporting to a given person is (R-L-1)/2. This allows specific questions against the tree to be able to be read much faster (without needing to build the tree itself).

The modified preorder tree does come with the expense that updates to it will touch a large number of lines compared to the adjacency list approach to an org tree.  An insert will shift numbers around all over the place.  For example, if Sally had another person reporting to her, that person would have the value 7,8 and you would need to modify everyone's if(L > 7) L = L + 2 and if (R > 7) R = R + 2.


答:
There are several ways to create structure
from flatland. Recursion is one of the best.
This
program uses it, with a preliminary step of figuring
out which items are parents.

While the strategy is language-agnostic, every language
has different details of data structures and
building blocks. I've rendered the approach in Python.

NB, about half this code is for display and demonstration purposes, so you can follow along and see how the algorithm is working. For production, fee free to remove those parts.

Oh...and I changed the labels from 'parent' to 'name' and 'children' to 'reports' because I found that more agreeable. You can, of course, choose whatever you like.

from pprint import pprint
from random import shuffle
from collections import defaultdict
import json

def show_val(title, val):
    """
    Debugging print helpler.
    """
    sep = '-' * len(title)
    print "\n{0}\n{1}\n{2}\n".format(sep, title, sep)
    pprint(val)

# ORIGINAL NAMES:   emp_id, emp_name, mgr_id
# SIMPLIFIED NAMES: eid,    name,     mid
text = """
323,The Boss,
4444,Manager Joe,323
3,Manager Sally,323
4,Peon Frank,4444
33,Peon Dave,3
5,Peon Jill,4444
6,Peon Rodger,3
7,Peon Ralph,3
233,Clerk Jane,99
99,Supervisor Henri,3
"""

# parse text into lines
lines = [ l.strip() for l in text.strip().splitlines() ]

# construct list of people tuples
people = [ tuple(l.split(',')) for l in lines ]

# for demonstration and testing only, shuffle the results
shuffle(people)
show_val("randomized people", people)

# contstruct list of parents
parents = defaultdict(list)
for p in people:
    parents[p[2]].append(p)
show_val("parents", parents)

def buildtree(t=None, parent_eid=''):
    """
    Given a parents lookup structure, construct
    a data hierarchy.
    """
    parent = parents.get(parent_eid, None)
    if parent is None:
        return t
    for eid, name, mid in parent:
        report = { 'name': name }
        if t is None:
            t = report
        else:
            reports = t.setdefault('reports', [])
            reports.append(report)
        buildtree(report, eid)
    return t

data = buildtree()
show_val("data", data)

show_val("JSON", json.dumps(data))
```

<p>Running this shows the following output:</p>

```-----------------
randomized people
-----------------

[('233', 'Clerk Jane', '99'),
 ('4444', 'Manager Joe', '323'),
 ('33', 'Peon Dave', '3'),
 ('6', 'Peon Rodger', '3'),
 ('99', 'Supervisor Henri', '3'),
 ('3', 'Manager Sally', '323'),
 ('5', 'Peon Jill', '4444'),
 ('323', 'The Boss', ''),
 ('4', 'Peon Frank', '4444'),
 ('7', 'Peon Ralph', '3')]

-------
parents
-------

defaultdict(<type 'list'>, {'99': [('233', 'Clerk Jane', '99')], '323': [('4444', 'Manager Joe', '323'), ('3', 'Manager Sally', '323')], '3': [('33', 'Peon Dave', '3'), ('6', 'Peon Rodger', '3'), ('99', 'Supervisor Henri', '3'), ('7', 'Peon Ralph', '3')], '4444': [('5', 'Peon Jill', '4444'), ('4', 'Peon Frank', '4444')], '': [('323', 'The Boss', '')]})

----
data
----

{'name': 'The Boss',
 'reports': [{'name': 'Manager Joe',
              'reports': [{'name': 'Peon Jill'}, {'name': 'Peon Frank'}]},
             {'name': 'Manager Sally',
              'reports': [{'name': 'Peon Dave'},
                          {'name': 'Peon Rodger'},
                          {'name': 'Supervisor Henri',
                           'reports': [{'name': 'Clerk Jane'}]},
                          {'name': 'Peon Ralph'}]}]}

----
JSON
----

'{"name": "The Boss", "reports": [{"name": "Manager Joe", "reports": [{"name": "Peon Jill"}, {"name": "Peon Frank"}]}, {"name": "Manager Sally", "reports": [{"name": "Peon Dave"}, {"name": "Peon Rodger"}, {"name": "Supervisor Henri", "reports": [{"name": "Clerk Jane"}]}, {"name": "Peon Ralph"}]}]}'
```

Some preliminaries: It also uses print to help show nested data
structures. We'd normally get data through a database
connection, here we just parse it out of static text.
Finally, while the data you presented is beautifully ordered, with the
bosses at the top and with the lowest employeed id numbers (simplifying the)
problem, I'd like to confirm that the code works in any order. So I've modified
some of the id numbers to reflect a non-sequential allocation, and brought in
random.shuffle to randomize the order of data. You wouldn't do this in
production, but as a part of testing, it increases confidence the logic is
working by design, not accident.

Title: Process to generate hierarchical structure based on relational data?

Body:
I have a csv of employee ids, names, and a reference column with the id of their direct manager, say something like this

```<code> emp_id, emp_name, mgr_id
1,The Boss,,
2,Manager Joe,1
3,Manager Sally,1
4,Peon Frank,2
5,Peon Jill,2
6,Peon Rodger,3
7,Peon Ralph,3
</code>```

I would like to be able to generate a (json) object representing this structure, something along the lines of

```<code>DATA = {
  "parent": "The Boss",
  "children: [
    {
      "parent": "Manager Joe",
      "children": [ {"parent": "Peon Frank"}, {"parent": "Peon Jill"} ]
    },
    {
      "parent": "Manager Sally",
      "children": [ {"parent": "Peon Rodger"}, {"parent": "Peon Ralph" } ]
    }]
}
</code>```

So from the data, an entry with no mgr_id represents something like the CEO or Leader.

So just collecting some thoughts.. I know this could be represented by some tree data structure with the tree traversal generating the correct output. The parser would have to be responsible for inserting into the tree. Maybe the number of children would give you a weight? Just gathering thoughts. Not sure how I would pivot around the fact that multiple objects can be constructed with children.

Is there an algorithm defined that can parse this structure I am not thinking of? Descending down into the children seems relatively straightforward. I am not seeing this in my head, and could use some help. Thank you

Body:
There are several ways to create structure
from flatland. Recursion is one of the best.
This
program uses it, with a preliminary step of figuring
out which items are parents.

While the strategy is language-agnostic, every language
has different details of data structures and
building blocks. I've rendered the approach in Python.

NB, about half this code is for display and demonstration purposes, so you can follow along and see how the algorithm is working. For production, fee free to remove those parts.

Oh...and I changed the labels from 'parent' to 'name' and 'children' to 'reports' because I found that more agreeable. You can, of course, choose whatever you like.

from pprint import pprint
from random import shuffle
from collections import defaultdict
import json

def show_val(title, val):
    """
    Debugging print helpler.
    """
    sep = '-' * len(title)
    print "\n{0}\n{1}\n{2}\n".format(sep, title, sep)
    pprint(val)

# ORIGINAL NAMES:   emp_id, emp_name, mgr_id
# SIMPLIFIED NAMES: eid,    name,     mid
text = """
323,The Boss,
4444,Manager Joe,323
3,Manager Sally,323
4,Peon Frank,4444
33,Peon Dave,3
5,Peon Jill,4444
6,Peon Rodger,3
7,Peon Ralph,3
233,Clerk Jane,99
99,Supervisor Henri,3
"""

# parse text into lines
lines = [ l.strip() for l in text.strip().splitlines() ]

# construct list of people tuples
people = [ tuple(l.split(',')) for l in lines ]

# for demonstration and testing only, shuffle the results
shuffle(people)
show_val("randomized people", people)

# contstruct list of parents
parents = defaultdict(list)
for p in people:
    parents[p[2]].append(p)
show_val("parents", parents)

def buildtree(t=None, parent_eid=''):
    """
    Given a parents lookup structure, construct
    a data hierarchy.
    """
    parent = parents.get(parent_eid, None)
    if parent is None:
        return t
    for eid, name, mid in parent:
        report = { 'name': name }
        if t is None:
            t = report
        else:
            reports = t.setdefault('reports', [])
            reports.append(report)
        buildtree(report, eid)
    return t

data = buildtree()
show_val("data", data)

show_val("JSON", json.dumps(data))

Running this shows the following output:

-----------------
randomized people
-----------------

[('233', 'Clerk Jane', '99'),
 ('4444', 'Manager Joe', '323'),
 ('33', 'Peon Dave', '3'),
 ('6', 'Peon Rodger', '3'),
 ('99', 'Supervisor Henri', '3'),
 ('3', 'Manager Sally', '323'),
 ('5', 'Peon Jill', '4444'),
 ('323', 'The Boss', ''),
 ('4', 'Peon Frank', '4444'),
 ('7', 'Peon Ralph', '3')]

-------
parents
-------

defaultdict(<type 'list'>, {'99': [('233', 'Clerk Jane', '99')], '323': [('4444', 'Manager Joe', '323'), ('3', 'Manager Sally', '323')], '3': [('33', 'Peon Dave', '3'), ('6', 'Peon Rodger', '3'), ('99', 'Supervisor Henri', '3'), ('7', 'Peon Ralph', '3')], '4444': [('5', 'Peon Jill', '4444'), ('4', 'Peon Frank', '4444')], '': [('323', 'The Boss', '')]})

----
data
----

{'name': 'The Boss',
 'reports': [{'name': 'Manager Joe',
              'reports': [{'name': 'Peon Jill'}, {'name': 'Peon Frank'}]},
             {'name': 'Manager Sally',
              'reports': [{'name': 'Peon Dave'},
                          {'name': 'Peon Rodger'},
                          {'name': 'Supervisor Henri',
                           'reports': [{'name': 'Clerk Jane'}]},
                          {'name': 'Peon Ralph'}]}]}

----
JSON
----

'{"name": "The Boss", "reports": [{"name": "Manager Joe", "reports": [{"name": "Peon Jill"}, {"name": "Peon Frank"}]}, {"name": "Manager Sally", "reports": [{"name": "Peon Dave"}, {"name": "Peon Rodger"}, {"name": "Supervisor Henri", "reports": [{"name": "Clerk Jane"}]}, {"name": "Peon Ralph"}]}]}'

Some preliminaries: It also uses print to help show nested data
structures. We'd normally get data through a database
connection, here we just parse it out of static text.
Finally, while the data you presented is beautifully ordered, with the
bosses at the top and with the lowest employeed id numbers (simplifying the)
problem, I'd like to confirm that the code works in any order. So I've modified
some of the id numbers to reflect a non-sequential allocation, and brought in
random.shuffle to randomize the order of data. You wouldn't do this in
production, but as a part of testing, it increases confidence the logic is
working by design, not accident.