问:
PHP Source Code Encryption Concept
Scenario:

I'm looking to protect my software that is written in PHP. The nature of PHP is that it is delivered as plain text and therefore cannot be protected by itself. I don't want to install libs to the server like ZendGuard, IonCube or SourceGuardian. I want that individuals should use the software but should not interfere with the protected part of the application (licensing, sensitive parts). The software is distributed to clients as trial software.

Solution Concept:

Encrypt the payload with a blockcipher (AES) and store the key on a remote system. To software would require the key to decrypt the payload. An obfuscated part of the software should contact the key server via cURL over SSL and request the decryption key. The decryption key should only be sent back to the Server IP that holds a trial license. A checksum of the files could also be posted with the key request and if it even fails once, the key server could reject to provide the decryption key forever, which should hopefully prevent tampering.

Notes:

The client requesting the trial will be known in advance so unauthorized copies of the software should never receive a (correct) decryption key in first place.

The decryption keys are unique to every client and the payload parts are too.

The code will never be stored decrypted but only decrypted at runtime and eval()'ed.

Questions:

Is this a feasable or doomed to failure?

Wouldn't this protect even against the tiniest tampering by anyone?

Thank you!

答:
Let's see. Once your key server sends the decryption key, the hacker can monitor network activity to retrieve the key and use it to decrypt the source code. From this moment, he can do whatever he wants with it, especially:

Modify it in a way that it will either send the checksums you are expecting, and not the actual checksums of files,

Replace the part which contacts the key server, or simply remove it to avoid the check,

Post on P2P networks the decrypted source code with key checking part removed.

On the other hand, some part of the legitimate users of your product will probably prefer using the product of your competitors, because:

They'll find that your app is slow to start. Contacting the remote key server takes time, which may or may not be accepted by the end users.

Decrypting takes time too, which can waste too much resources on servers. Your competitors' products may achieve much better CPU footprint if they don't add this sort of complexity.

Banning by IP will  cause additional problems. Most users have dynamic IPs, which means that a ban will affect the concerned person only for a very limited amount of time (such as one day), and then prevent other persons to access your product. In the same way, banning the whole company or a wifi spot is rather unfortunate in terms of marketing.

They won't accept to rely on a fragile product (unless you work in a very large company which guarantees that the product will be maintained for the next 5, 10 or 15 years). Not having the source code means that:

If your company stops maintaining it, your customers will be using an obsolete product which may contain known but unpatched bugs.

If a customer wants to modify it, he has no other choice than to pay you (often a lot, because of the monopoly) to do the change.

They won't accept to rely on a product which can contain malware. If you're a known company with excellent reputation, this is not an issue. If you're a startup or don't have a well-established reputation, some customers would not install your app because of the risk of malicious code. This is especially important on servers which are often more protected than desktop PCs in companies.

If you actually invented something that should be protected (and very probably, you haven't), the only way you can protect it is to move the sensitive code to the servers you own, and then provide an API to access the functionality from the outside. As soon as you give the code, no matter how well is it protected, it can be decrypted; otherwise, it wouldn't be possible to run it.

If the only goal is to have a trial version, simply host it on your own servers. Potential customers will be able to try it, and if they are interested, purchase the product. Of course, in order to convince sysadmins as well, you should also provide very detailed information about the way your app should be deployed and hosted.

Later, when your product becomes successful, you may consider evolving your offer, by providing:

The limited in time demo hosted on your server,

The pay-per-month subscription where the product is still hosted on your server. Multiple variants of subscriptions may target separately individuals, small companies and medium-sized companies. For small entities, the product may be out of charge for a year or some other long period of time (or forever).

The more expensive solution where the customer deploys your app on his own servers. This solution may target large companies.

With this model (used by many startups), encryption of source code becomes mostly irrelevant. Large companies won't try to download your product from a P2P network, because of all the compliance policies which prevent them from using unlicensed products.

Title: PHP Source Code Encryption Concept


问:
Would you use C, today, for a software project?
If yes, where and why would you use it?

If no, please provide an explanation to why C is not acceptable to you.

答:
I work mostly with the Xen hypervisor, the assorted libraries it features and the Linux kernel. On occasion, I have to write a device driver (or re-write one so that nxx virtual machines can share a single device such as a HRNG). C is my primary language and I am quite happy with that.

Would I try to write a spreadsheet program using it? No way. Each tool has its applications, and I'm happy that I have many tools.

I love C, but I don't try to pound screws with a hammer.

If C is a sensible choice for a new project, sure. If not, I'll use something else.

ContentLicense: CC BY-SA 2.5


问:
Efficient storing and multidirectional lookup of hierachical data
I make a game and among others I have nations, provinces, tiles and armies. Each army belongs to exactly one tile, each tile belongs to exactly one province, each province to exactly one nation. I want to ask the following questions:

Which armies/tiles/provinces belong to a nation?

Which armies/tiles belong to a province?

Which armies belong to a tile?

To which tile/province/nation belongs this army?

To which province/nation belongs this tile?

To which nation belongs this province?

These questions are multidirectional. A hierachical tree with nations on top and provinces, tiles, armies below will only answer questions 1-3 efficiently while answers 4-6 would require a loop over all nations/provinces/tiles to find the nation/province/tile that belongs to a province/tile/army.

So one could store 4 different trees, each time with nations/provinces/tiles or armies on top to enable a fast lookup. However then modifying the whole structure (adding/deleting/modifying a nation/province/tile/army) gets a complex task and therefore prone to errors.

I'm a bit lost here. I would like to keep the code and the underlying data structures simple to prevent errors when modifying the structure or when adding more functionality but I would not like to give up speed for answering the mentioned questions. I'm looking for a balance of code complexity and speed where code simplicity is more important.

In particular: Can I have a single data structure (and easy adding/deleting) while not having to loop over all nations/... when asking the reverse question? What is the simplest data structure/algorithm that allows asking all these questions with reasonable speed, i.e. without having to loop over every nation/...?

答:
One Option is to give every of your provinces, tiles and armies a reference to its parent tile/province/nation. You then have to consider this reference on each update of course as you now have some redundancy in your code and a little memory overhead. Though, the good thing is that it gives you the answers for questions 4-6 in an instant (constant time).

(From my comment to the question, see also: https://meta.stackexchange.com/questions/1555/mark-a-comment-as-answer-to-a-question)

ContentLicense: CC BY-SA 3.0


问:
Bulk update in client-server application
Sometimes when coding a client-server application, the client needs to make a bulk update. For example: "mark all pending orders as dispatched". To implement these I typically write specific controller methods, which perform the required database operation.

It crossed my mind that theoretically this could be done without specific controller methods. In a simple approach, the client could iterate through the pending orders (which it can already access) and mark each as dispatched (again, it already has a call to do this). The problem with that approach is that it causes many network round-trips, increasing latency.

But this made me wonder, is there some kind of generic client-server query/update language? I would hesitate to allow SQL from client to server, unless I could create some very tight controls to prevent SQL injection. But in principle I want to client to tell the server to "UPDATE orders SET status='dispatched' WHERE status='pending'" - and the server to action this, while enforcing any security controls applicable to the current user.

I figured this probably already has a solution, I just don't know the name of it - hence this question. Any help appreciated.

Edit: on reflection, maybe this is a bad idea. When I explicitly code a controller, I can ensure the database has appropriate indexes to make the update efficient. If the query only exists on the front-end, then ensuring appropriate indexes are in place creates an uncomfortable front/back dependency.

答:
flask-restless supports this behaviour with it's multiple patch feature.

PUT /api/person

Sets specified attributes on every instance of Person
  which meets the search criteria described in the q parameter.

I still don't know the name for this general technique, but this does what I need.


答:
You wrote in a comment to MainMa's answer that:

I thought about this for a simple trial period of maybe a couple of days and after the purchase has been made, deliver the decrypted version. The main goal is to deliver the product without delivering the source (at first) so that the user can see if he likes the product (or if it's useful) .

which coincidentally turns your question into an XY problem. In other words, you are seeking to solve a problem, and instead of asking about the problem you want to solve, you ask about your proposed solution. (Which can work, if the proposed solution is actually good and you just need some minor detail straightened out.)

If the above is your reason for doing this, then forget about obfuscating the source code. It will add a huge amount of complexity and risk, with many downsides as explained by MainMa and no real, tangible advantages for anyone.

Instead, host a copy of your own that prospective customers can log in to and experiment with. Restore a blank database every midnight or something like that, make it clear to any users that any and all data they enter is publicly accessible and will be deleted, and prominently publish a link to it as well as the username and password to log in to the demo.

If the data needs to be preserved for some non-trivial amount of time for the user to be able to determine whether the application suits their needs (which it doesn't sound like based on your talking about a few days), offer prospective customers the ability to set up a trial installation on your server which is automatically deleted after some reasonable, short period of time.

In addition, this solution will give the users the ability to easily evaluate whether an upgrade provides additional value to them before they upgrade. If the upgrade is in any way whatsoever disruptive, or if a downgrade is non-trivial, that alone can be an advantage.

And all that for the cost of you having some additional web hosting, a cron job and storing a database snapshot and/or a few additional copies of the default database contents.

Title: PHP Source Code Encryption Concept

Body:
Scenario:

I'm looking to protect my software that is written in PHP. The nature of PHP is that it is delivered as plain text and therefore cannot be protected by itself. I don't want to install libs to the server like ZendGuard, IonCube or SourceGuardian. I want that individuals should use the software but should not interfere with the protected part of the application (licensing, sensitive parts). The software is distributed to clients as trial software.

Solution Concept:

Encrypt the payload with a blockcipher (AES) and store the key on a remote system. To software would require the key to decrypt the payload. An obfuscated part of the software should contact the key server via cURL over SSL and request the decryption key. The decryption key should only be sent back to the Server IP that holds a trial license. A checksum of the files could also be posted with the key request and if it even fails once, the key server could reject to provide the decryption key forever, which should hopefully prevent tampering.

Notes:

The client requesting the trial will be known in advance so unauthorized copies of the software should never receive a (correct) decryption key in first place.

The decryption keys are unique to every client and the payload parts are too.

The code will never be stored decrypted but only decrypted at runtime and eval()'ed.

Questions:

Is this a feasable or doomed to failure?

Wouldn't this protect even against the tiniest tampering by anyone?

Thank you!


问:
Same android app connecting to multiple database -concept
I am looking for best method to create application which are connected to different databases based on client

So basically it is a multi tenancy model for an android app (enterprise use)

Deploy the same app to different customers but connect to different database for different customer.

The things I thought about is making a key  and for each customer the key varies and a correct webservice will be contacted based on the key ( individual webservices for each client)?

Has anyone done something similar? What is the best method to approach this.
Thanks

答:
In any case you will require a server API (webservice, REST or similar) to connect your Android app to the database. Assuming your database is available through such an API, there are two options.

Design the database & server to serve multiple customers (multi-tenancy). With this your server API will expect the tenancy selection to happen on a per-request basis (e.g. a HTTP header or a query attribute), or at session initialization (e.g. when the user logs in).

REST example:

```<code># get customers for tenant ABC
GET example.com/api/customer/?tenant=ABC
# create a new customer for tenant ABC
POST example.com/api/customer/?tenant=ABC
</code>```

Obviously, your server will have to make sure clients can only access data that they are allowed to. Instead of passing the tenant as a query parameter, it can also be passed as an HTTP header or set as part of the session.

Create different API endpoints, with each endpoint connecting to a different database. In this case your Android app needs to be configurable in some manner, e.g. by requesting the API endpoint to use from (say) the central login server.

Example:

```<code># Request API endpoint to use
# we assume the configuration response to contain the apiUrl parameter
apiUrl = GET login.example.com/configuration/?user=XY
GET <apiUrl>/customer/
...
</code>```

The server will run multiple endpoints, e.g. abc.example.com or xyz.example.com for tenants ABC and XYZ, respectively. So the actual API calls will look like this:

```<code># get customers for tenant ABC
GET abc.example.com/customer
# get customers for tenant XYZ
GET xyz.example.com/customer
</code>```

As always with engineering decisions, there are a couple of trade-offs to consider:

Data secrecy/segregation v.s. operation efficiency. Multitenancy is relatively simple to solve in a database (add a tenantfield or similar to at least the core tables of your data model, or every table for that matter). Operating one database is easier than operating many. However, in some industries you may need to keep data in separate databases.

Declarative v.s. explicit client code. In the first example, your app client needs to be aware of the actual tenant it runs for to be able to send the right parameters to the server. In the second example, all that the app client needs to know is the endpoint's URL to connect to.

Note that the first and second example could also be mixed:

In the second example, the same database can be used for different customers. To do so, run a front-end webserver that adds a tenant header to the HTTP request depending on the API endpoint.

In the first example, the server could route the request to a different database depending on the tenant selection. Some frameworks make such routing trivial, e.g. Django has a database router built-in.

Body:
In any case you will require a server API (webservice, REST or similar) to connect your Android app to the database. Assuming your database is available through such an API, there are two options.

Design the database & server to serve multiple customers (multi-tenancy). With this your server API will expect the tenancy selection to happen on a per-request basis (e.g. a HTTP header or a query attribute), or at session initialization (e.g. when the user logs in).

REST example:

# get customers for tenant ABC
GET example.com/api/customer/?tenant=ABC
# create a new customer for tenant ABC
POST example.com/api/customer/?tenant=ABC

Obviously, your server will have to make sure clients can only access data that they are allowed to. Instead of passing the tenant as a query parameter, it can also be passed as an HTTP header or set as part of the session.

Create different API endpoints, with each endpoint connecting to a different database. In this case your Android app needs to be configurable in some manner, e.g. by requesting the API endpoint to use from (say) the central login server.

Example:

# Request API endpoint to use
# we assume the configuration response to contain the apiUrl parameter
apiUrl = GET login.example.com/configuration/?user=XY
GET <apiUrl>/customer/
...

The server will run multiple endpoints, e.g. abc.example.com or xyz.example.com for tenants ABC and XYZ, respectively. So the actual API calls will look like this:

# get customers for tenant ABC
GET abc.example.com/customer
# get customers for tenant XYZ
GET xyz.example.com/customer

As always with engineering decisions, there are a couple of trade-offs to consider:

Data secrecy/segregation v.s. operation efficiency. Multitenancy is relatively simple to solve in a database (add a tenantfield or similar to at least the core tables of your data model, or every table for that matter). Operating one database is easier than operating many. However, in some industries you may need to keep data in separate databases.

Declarative v.s. explicit client code. In the first example, your app client needs to be aware of the actual tenant it runs for to be able to send the right parameters to the server. In the second example, all that the app client needs to know is the endpoint's URL to connect to.

Note that the first and second example could also be mixed:

In the second example, the same database can be used for different customers. To do so, run a front-end webserver that adds a tenant header to the HTTP request depending on the API endpoint.

In the first example, the server could route the request to a different database depending on the tenant selection. Some frameworks make such routing trivial, e.g. Django has a database router built-in.


问:
Using streams as method parameters and/or return types
So let's say I have a 'Transformer' interface that can transform one file-type to another. Let's say I also wish to use streams to provide the data to the Transformer. Which is the approach that is considered 'best practise';

```<code>public InputStream transform(InputStream data);
</code>```

OR

```<code>public void transform(InputStream originalData, OutputStream newData);
</code>```

Although the first method seems to be slightly more concise to me (ie, you get an inputstream that you can consume immediately) it seems to violate the whole 'creator of a stream should be the one who closes it' principle.

The second method, whilst needing a bit more work on the part of the client to handle the outputstream, seems like it might be more useful in some cases (ie, passing a ServletOutputStream as the 'newData' argument) and the client code will handle all opening/closing of streams.

I am leaning towards the second method but I have seen both used in various APIs I have come across and am wondering if there are situations where one would be preferred over the other?

答:
Returning an Input Stream enables your calculation to be performed on an ongoing basis as data is read from it, potentially even in a separate thread. It is  equivalent to a unix command line pipe. If the processing takes a significant time to complete, this will effectively hide that processing time from the user by distributing it (or shunting it off to another core) and returning results a little at a time. You can easily build up multiple stages of transformation.

When passing an output stream, it's like using the old pre-windows DOS prompt: fine if all you're doing is sending data straight back to the user, but if not you'll have to store it temporarily somewhere (either a file or a ByteArrayOutputStream) before continuing the processing. This will introduce delays before any output is produced, makes taking advantage of multiple cores harder, and uses resources unnecessarily.

Additionally, you state that there is a general rule that the opener of a stream should close it, but I don't think this is the complete rule. I believe the rule that the java IO system is built around is best phrased as "the opener of a stream should be responsible for closing it, unless it is later wrapped in another object which also provides a close method, in which case the opener should call that method instead, which should then close the original stream on the opener's behalf." This is what you do, for example, when using a BufferedInputStream.

All of this suggests that the former should generally be preferred, but it is worth noting that for many complex transformations, the latter is much easier to implement (assuming you do not wish to use multiple threads). This may way in favour of the former in some cases, but note that if a multithreaded solution is preferred, code in the latter style can be adapted to the former interface by placing in its own thread and using a Piped*Stream pair for  communication. Thus I'd say that other than very simple cases the former is best, but the latter might be acceptable and easier if you have a simple case and/or don't want to get involved with threads.

ContentLicense: CC BY-SA 4.0


问:
What is "using namespace" pollution?
I was looking at the google coding guide [here] and they do not recommend that one use the using namespace or namespace::function - if I did not misinterpret it.

Does this apply to std as well? cout<< does not work without it. This book, recommends the same. So how do I go about using cout<< without using namespace std; or std::cout<< ?

What is the recommended way? std::cout<<? Most c++ text books teach beginners with using namespace std; are they propagating poor coding practice?

答:
Although the question has already useful answers, one detail seems to fall too short.

Most programmers are at first a bit confused with the using keyword and the descriptions of namespace usage, even if they try to learn it by looking up the reference, because declaration and directive reads somewhat equivalent, both are relatively abstract long words starting with d.

Identifiers within namespaces are accessible by explicitly naming the namespace:

```<code>myNamespace::myIdent
</code>```

this may be a lot more keys to type. But it may also decrease the significance of your code, if most identifiers get prefixed the same way. The using keyword helps to prevent these namespace downsides. Since using works on the compiler level (it's no macro), its effect lasts for the whole scope it is used in. That's why Google style restricts its usage to well defined scopes, i.e. classes in header files or functions in cpp files.

...of course there is a difference between using declaration

```<code>using myNamespace::myIdent; // make myIdent an alias of myNamespace::myIdent
</code>```

and using directive

```<code>using myNamespace; // make all identifiers of myNamespace directly accessible
</code>```

If used in huge scopes, the latter leads to much more confusion.

Title: What is "using namespace" pollution?

Body:
Although the question has already useful answers, one detail seems to fall too short.

Most programmers are at first a bit confused with the using keyword and the descriptions of namespace usage, even if they try to learn it by looking up the reference, because declaration and directive reads somewhat equivalent, both are relatively abstract long words starting with d.

Identifiers within namespaces are accessible by explicitly naming the namespace:

myNamespace::myIdent

this may be a lot more keys to type. But it may also decrease the significance of your code, if most identifiers get prefixed the same way. The using keyword helps to prevent these namespace downsides. Since using works on the compiler level (it's no macro), its effect lasts for the whole scope it is used in. That's why Google style restricts its usage to well defined scopes, i.e. classes in header files or functions in cpp files.

...of course there is a difference between using declaration

using myNamespace::myIdent; // make myIdent an alias of myNamespace::myIdent

and using directive

using myNamespace; // make all identifiers of myNamespace directly accessible

If used in huge scopes, the latter leads to much more confusion.


问:
What do you put into an Architecture Specification?
I'm currently revising a number of document templates for my company. One thing we've never had is a formal Architecture Specification, so I'm starting to put one together. </p>

<p>What sort of things do you put into your architecture specs? Feel free to copy and paste a table of contents - that would be helpful. Are there any good templates already available on the web? </p>

答:
I've modeled my Software Architecture Documents (or SADs, which is a double entendre because it is both the acronym and what developers feel after looking at them)  -- after the Rational Unified Process's Software Architecture document .

It provides a good template for explaining the high-level architecture and some pointers / descriptions of what should go where.  Actually I've found the entire suite of RUP templates invaluable when used correctly (don't be afraid to edit the documents and model it after information you deem useful.

Other than that -- a high level UML diagram of the overall components/packages of the system.  Then important sub-components (class diagrams), and if sufficiently complex, some sequence diagrams describing the important interactions among compontents.

The important thing is maintaining a balance between usefulness, brevity, and maintainability -- don't get too caught up in unnecessary details.


问:
GPL Copyright notice in fork of dual licenced software when copyright of original has been sold subsequently
Since 2009 we are developing a fork of a CMS that was started in 1999 as commercial project, but released 2009 additionally under GPL. The original programmer sold copyright, code, name and website in 2012 to a new owner (who is now using the name and website because they have a high google pagerank).

Should our copyright notice be:

@copyright 1999-2009 name of original programmer

@copyright 2009-2014 our names

or

@copyright 1999-2009 name of new copyright owner

@copyright 2009-2014 our names

答:
I emailed the FSF licensing lab, and here is the response I received (NB: it was from a licensing volunteer, not from a lawyer, and does not constitute legal advice):

Your obligations under sections 4 and 5 of the GPLv3 are to maintain the existing copyright notices. It has never been a requirement of the GPL to research, affirm or update the copyright headers for modifications to a work for which you are not the copyright holder.

Even if the copyright headers haven't been updated, the current copyright holders would be able to legally enforce the terms of the license by proving that they got the relevant copyrights from the original holders. In addition, the original owners may have a reciprocal license agreement with the current copyright holders which permit them to further distribute the software in certain ways, so you can't know for sure that removing their copyright notice is the right thing even if there is a new copyright holder.

(Bold emphasis mine.)

Therefore, you should keep the licenses intact, as you received them. It appears that your first option is correct:

```<code>@copyright 1999-2009 name of original programmer
@copyright 2009-2014 our names
</code>```

assuming that the software you received had the original header:

```<code>@copyright 1999-2009 name of original programmer
</code>```

Body:
I emailed the FSF licensing lab, and here is the response I received (NB: it was from a licensing volunteer, not from a lawyer, and does not constitute legal advice):

Your obligations under sections 4 and 5 of the GPLv3 are to maintain the existing copyright notices. It has never been a requirement of the GPL to research, affirm or update the copyright headers for modifications to a work for which you are not the copyright holder.

Even if the copyright headers haven't been updated, the current copyright holders would be able to legally enforce the terms of the license by proving that they got the relevant copyrights from the original holders. In addition, the original owners may have a reciprocal license agreement with the current copyright holders which permit them to further distribute the software in certain ways, so you can't know for sure that removing their copyright notice is the right thing even if there is a new copyright holder.

(Bold emphasis mine.)

Therefore, you should keep the licenses intact, as you received them. It appears that your first option is correct:

@copyright 1999-2009 name of original programmer
@copyright 2009-2014 our names

assuming that the software you received had the original header:

@copyright 1999-2009 name of original programmer


问:
What should an object be called that contains only setters and getters?
Suppose there's an object that contains only setters and getters:

```<code>class Config
{
    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return this.name;
    }

    public void setLanguage(String language)
    {
        this.language = language;
    }

    public String getLanguage()
    {
        return this.language;
    }

    // And so on, you got the point!
}
</code>```

There are several terms that describe such objects:

DAO (Data Access Object)

DTO (Data Transfer Object)

VO (Value object)

Here lies my confusion:

It can be called Data Access Object, because it's responsible for data access. It can be called Data Transfer Object, because it might be responsible for transferring data between different objects. And finally, it can be called Value Object, because it has no behaviour and contains only values.

What are the origins of these different terms, which term should I choose in which context, and why?

答:
A DAO class usually has the CRUD operations like save, update, and delete. Whereas the DTO is just an object that holds data. So there is actually a difference between those two. The term then depends on what the object does.

A VO and DTO used to be synonyms: early Java EE literature used the term value object to describe a DTO, but it changed it in a later version.
"A Value Object doesn't have any identity. It is entirely identified by its value and is immutable." ~ http://www.adam-bien.com/roller/abien/entry/value_object_vs_data_transfer


答: I've heard and used both Data Access Object and Data Transfer Object, though I usually just call them Data Models (Objects) because it implies a container and doesn't imply what you are actually going to be doing with it.</p>

Title: What should an object be called that contains only setters and getters?

Body:
Suppose there's an object that contains only setters and getters:

```<code>class Config
{
    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return this.name;
    }

    public void setLanguage(String language)
    {
        this.language = language;
    }

    public String getLanguage()
    {
        return this.language;
    }

    // And so on, you got the point!
}
</code>```

There are several terms that describe such objects:

DAO (Data Access Object)

DTO (Data Transfer Object)

VO (Value object)

Here lies my confusion:

It can be called Data Access Object, because it's responsible for data access. It can be called Data Transfer Object, because it might be responsible for transferring data between different objects. And finally, it can be called Value Object, because it has no behaviour and contains only values.

What are the origins of these different terms, which term should I choose in which context, and why?


答:
I certainly wouldn't call it a value object because that conflicts with the idea in domain-driven-design. Going from wikipedia:

In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.

For example, a Person might have a Name. The Person is not a value object because even if they change their name, they are still the same person. Their equality is based on identity. The Name itself on the other hand is a value object- change any letter in that name and it becomes a new, different name.

As for what to call these, another term I've commonly heard is Property Bag. This is maybe a bit more of a C#-centric name than Java-centric, but it still describes the same idea. The difference between this and DTO/DAO is that it's being used to describe what the class is rather than what it's for. Whether this is more or less appropriate depends on the situation.


答:
if it has to be both

fast, and

portable

then I use C. Maybe C++.

Title: Would you use C, today, for a software project?

Body:
If yes, where and why would you use it?

If no, please provide an explanation to why C is not acceptable to you.


问:
Name/Fix for Production Code Whose Sole Purpose is to Facilitate Testing?
The linked "duplicate" question is an iffy match at best, because it's asking

is pattern X OK (YES/NO)

and I'm clearly already in the NO camp, and subsequently asking

what is pattern X called

what steps can be taken to fix pattern X

(neither of which are addressed by the linked question).

I recently did a code review on a block of code that looked something like this:

```<code>public class MyClass
{
    private ISomething mySomething;
    // ...Other variables omitted for brevity

    public MyClass() { mySomething = new Something();  }

    /// <summary>
    /// Constructor - ONLY USE THIS FOR UNIT TESTING
    /// </summary>
    public MyClass(ISomething something) { mySomething = something; }

    public void MyMethod()
    {
        // Gets called by the framework, and changes the internal state of the class by using mySomething...
    }

    // Other methods...
}
</code>```

I'm concerned specifically with the overloaded constructor. It was added purely to test this class, and will make its way into production code.

Is there a name for this pattern/anti-pattern, and what can be done to solve it?

For clarification, the implementation of Something was added specifically for the purpose of being able to add an overloaded constructor to MyClass. It's used nowhere else. Its existence is an instance of the very issue I'm concerned about.

ISomething is very tightly coupled to MyClass. It needn't have been extracted. Implementation and interface might as well look like:

```<code>public interface ISomething
{
    string GetClassName();
}

public class Something : ISomething
{
    public string GetClassName() { return "MyClass"; }
}
</code>```

That means that MyClass.MyMethod()'s body could just be replaced with return "MyClass";

However, the interface abuse/premature optimization seems like a separate issue and not in the spirit of the original question (i.e. consider it a given that the class/interface is structured like so and leave it as a separate [but valid] concern).

答:
There isn't, so far as I'm aware, a general name for production code created specifically for the purpose of unit tests. With the notable exception of friend-assemblies (for unit-testing internal units) such code would normally be a smell indicating that something's up with the shape of your class' public signature.

This particular example, however, does have a specific name. It is called "Poor Man's Dependency Injection" (or sometimes "Bastard Injection") because of the way it looks like proper dependency injection but doesn't actually break the coupling between the caller and the dependency.

The anti-pattern here isn't:-

```<code>public MyClass(ISomething something) { mySomething = something; }
</code>```

The anti-pattern here is:-

```<code>public MyClass() { mySomething = new Something(); }
</code>```

Now, it's not necessarily the case that anything needs fixed here immediately. You might be ok to let it slide this once, but if it comes up often you'll want to remove the parameterless constructor and have MyClass always receive its dependency from external code - possibly with the aid of an IoC Container or ServiceLocator.

(Note: While IoC Containers are much more fashionable than ServiceLocators these days, the important thing is that you separate configuration and usage. Using either is better than using neither.)

Responding to your edit:-

As I said above, testing difficulties are normally indicative of poor design. You can fix that in two ways. Either you can open up some doors for test code to (inappropriately, as you put it) access the internals of an otherwise impossible-to-test object, or you fix the underlying design problem.

Now, that's a bit strong, because the correlation between "well-designed code" and "code that is easy to test" isn't quite 100%. In the case you've called out in your OP, the design problem looks obvious (Object instantiates its own dependency -> DIP violation, although on closer inspection it may also be the case that the dependency is unnecessary). Sometimes the design problem is less obvious, or - especially in legacy code where architectural refactoring is difficult and risky - not worth solving. You'll need to use your judgement to figure out which is which.

Body:
There isn't, so far as I'm aware, a general name for production code created specifically for the purpose of unit tests. With the notable exception of friend-assemblies (for unit-testing internal units) such code would normally be a smell indicating that something's up with the shape of your class' public signature.

This particular example, however, does have a specific name. It is called "Poor Man's Dependency Injection" (or sometimes "Bastard Injection") because of the way it looks like proper dependency injection but doesn't actually break the coupling between the caller and the dependency.

The anti-pattern here isn't:-

public MyClass(ISomething something) { mySomething = something; }

The anti-pattern here is:-

public MyClass() { mySomething = new Something(); }

Now, it's not necessarily the case that anything needs fixed here immediately. You might be ok to let it slide this once, but if it comes up often you'll want to remove the parameterless constructor and have MyClass always receive its dependency from external code - possibly with the aid of an IoC Container or ServiceLocator.

(Note: While IoC Containers are much more fashionable than ServiceLocators these days, the important thing is that you separate configuration and usage. Using either is better than using neither.)

Responding to your edit:-

As I said above, testing difficulties are normally indicative of poor design. You can fix that in two ways. Either you can open up some doors for test code to (inappropriately, as you put it) access the internals of an otherwise impossible-to-test object, or you fix the underlying design problem.

Now, that's a bit strong, because the correlation between "well-designed code" and "code that is easy to test" isn't quite 100%. In the case you've called out in your OP, the design problem looks obvious (Object instantiates its own dependency -> DIP violation, although on closer inspection it may also be the case that the dependency is unnecessary). Sometimes the design problem is less obvious, or - especially in legacy code where architectural refactoring is difficult and risky - not worth solving. You'll need to use your judgement to figure out which is which.


问:
Stored Procedures, ORMs and other application layers
I'm just starting out on a project to develop a new, fairly substantial web application which has an underlying MSSQL database. We're hiring a team of developers to write the application (in .NET) and I'm building the database (I've actually already started).

My initial plan was to write Stored Procedures in my database to handle all of the business logic, retrieving and updating data and so forth, and have the developers just make calls, passing the appropriate parameters. Then, because there's a strong chance we'll want to create mobile apps which replicate some parts of the web app's functionality, I thought we'd create a middle layer of web services to interact with the database and serve the applications, but I was still going to have this middle layer call Stored Procedures.

I've spent most of the past few days reading up on ORMs and reading the arguments for and against using them instead of SProcs or as well SProcs, or not at all. It's a minefield, and I'm really looking for some clear guidance on how I should be structuring this project. I know there's the potential for discussion here but I'm hoping to avoid that by asking some clear questions:

As I understand it, an ORM will basically automatically generate SQL to perform the tasks that the developer needs, e.g. creating a new record, deleting a record, etc. I can see how that works for basic data manipulation, but what if things are a bit more complex? What if, for example, I don't actually want any records deleting from the database but, rather, I just want a record to be flagged as 'deleted' when a user deletes it so that it is hidden in the application but still present in the table? Presumably, an ORM would just generate code to simply delete the record and actually remove it, and calls to retrieve records from that table would include records flagged as 'deleted' unless the developer knew the underlying data structure well enough to know to exclude these.  By contrast, I can just write a SProc called 'DeleteRecord', which flags the specified record as 'deleted' and another called 'GetRecord' which only returns records not flagged as 'deleted', and when the developer uses these they don't need to know what's actually happening in the database. And this is just a simple example - some of my SProcs were going to do several things in response to a seemingly simple request (e.g. writing to an Audit table when a record is modified). How would an ORM handle this sort of thing?

The only reason for me creating a middle layer between the application and the database is so that future apps, particularly mobile apps, can access the data using the same method. My idea was to create web services which would retrieve data from the SProcs and return it in XML or JSON, as well as providing services for creating, updating and deleting. Does this seem reasonable?

Reading online about layered application development, recommendations vary from two to about eight different layers! Am I just being naïve when I wonder whether it's possible to just end up going down a 'layer black hole' in all of this? Our application will be used by hundreds, not thousands, and will contain hundreds of thousands (maybe eventually millions) of records in its key tables, not billions. I can see the benefits of a degree of abstraction, but I prefer to keep things as simple as I can at the same time!

If our hired developers arrive and I tell them that they're accessing the database using purely SProc calls and not an ORM, are they likely to be comfortable with this? Are they likely to look at me as if I'm from the dark ages? Would they be justified in thinking that we were going about this the wrong way?

There are probably lots more questions, I'm just having trouble articulating the utter confusion and slightly depressing sinking feeling that comes from having thought I had it all worked out and now wondering if I'm in way over my head.

答:
First of all no matter what you choose you want your business logic outside your database as much as possible. There are many reasons for this, the top couple being you get tied to your database so if you need to change to Oracle or SQL Server it becomes a much harder task, SQL isn't designed to handle business logic gracefully which results in massive stored procedures and functions that could have been a few lines of code instead.

Also you will want a layer that handles all the data access between your app and database no matter if you choose an ORM or stored procedures. One reason for this is so it could potentially be used for multiple apps, it also makes your app less reliant on the actual database making changing that easier as well.

As for deciding whether to use an ORM or not, there is likely nothing in you app that would expose a weakness of an ORM. Your concern about hard vs. soft deletion isn't an issue, the steps are basically the same either way, you put an update statement in the DeleteRecord procedure, or you change the deleted property in the record object in the DeleteRecord method you create. The shortfall of ORMs is generally always performance, and they have been steadily improving on that front. Orms start lsoing when you start getting close to Big Data territory (millions of rows and maybe even tens of millions) or you are at extreme levels of traffic (Amazon, HFT).

There are developers that are comfortable with either style, that said ORMs are more popular now and you will definitely have a bigger talent pool willing to work with an ORM, this is not necessarily a better talent pool.


答:
I would call them structs. An old school name for old school design.

DAOs contain persistence-related functionality. That's not a DAO.

That could be a poorly implemented value object. Value objects should generally be immutable and they should have proper constructors. (However, a configuration could very well be an example of a value-like object that could be reasonably designed as mutable. It could be thought of as a map with fixed keys.)

A DTO could look like that, but an object is a DTO only when its sole purpose is to carry data over remote interfaces or such.


问:
How to write a custom EventHandler class
When I started using Java, I created a custom Event class to mimic C#'s implementation of Events. Since then, I've added some other features, like event listener priorities, and cascading events were priorities are respected. Now that I'm using C# again, I wanted to create an eventhandler class, or at least a class that I can use the event keyword with, that incorporated these features. Starting off with, I've hit a bit of a brick wall where I can't subclass EventHandler, being told that it's a sealed class, and I can only use the event keyword with EventHandler/EventHandler and its subclasses.

Is this a fool's errand? And should I just lump for a more straight-forward translation of my event class, ignoring the event system already in-place? Or is there I way I can do this successfully?

答:
I'm not sure I understood your question, but with the introduction of generics you don't need anymore to write a custom eventhandler, but you can create a custom EventArgs...

```<code>public event EventHandler<MyEventArgs> MyEvent;
public class MyEventArgs : EventArgs { //your stuff here }
</code>```

if you really need a custom EventHandler you can write:

```<code>public delegate void CustomEventHandler(object sender, MyEventArgs e);
public event CustomEventHandler MyEvent
</code>```

Body:
I'm not sure I understood your question, but with the introduction of generics you don't need anymore to write a custom eventhandler, but you can create a custom EventArgs...

public event EventHandler<MyEventArgs> MyEvent;
public class MyEventArgs : EventArgs { //your stuff here }

if you really need a custom EventHandler you can write:

public delegate void CustomEventHandler(object sender, MyEventArgs e);
public event CustomEventHandler MyEvent


答:
Yes!

C is a low level language and there are situation which C is almost the only option like I use C for programing micro-controllers, or put some code together to interact with devices from classic ports like Parallel, Serial or even Modem!


答:
You want to build a database with all the accessibility code and business logic and then hire developers to build some sort of user interface. You're taking on a lot of the development responsibility here and not giving the developers enough room to do their job. Is there some reason you don't feel you're going to hire competent people who can help you figure all this out?

Here are answers to your questions:

Either the people you hire know how to follow the requirements using an ORM or they don't. You call it delete, but it's really an update to the flagged/soft deleted record.

Middle Layers not only allow other apps to share the code, but it accomplishes a separation of concerns. If a new developer wants to know where the business logic is, they'll probably look to the middle layer, but in your case, they're going to be surprised to find it in your database.

I don't multiple layers/abstractions is about handling large data sets. It's about maintainability as well. It's just assumed that large data sets are more complex, but that's not always the case.

Not devs will agree, but if you do choose stored procs over an orm, you better know what you're doing since you're handling the business logic. They may have had bad experiences with a DBA who did not. Also, can you keep up with their demands or will you be a bottleneck. In your absence, they're stuck with working with procs, so they may not like it in the long run.

Title: Stored Procedures, ORMs and other application layers

Body:
I'm just starting out on a project to develop a new, fairly substantial web application which has an underlying MSSQL database. We're hiring a team of developers to write the application (in .NET) and I'm building the database (I've actually already started).

My initial plan was to write Stored Procedures in my database to handle all of the business logic, retrieving and updating data and so forth, and have the developers just make calls, passing the appropriate parameters. Then, because there's a strong chance we'll want to create mobile apps which replicate some parts of the web app's functionality, I thought we'd create a middle layer of web services to interact with the database and serve the applications, but I was still going to have this middle layer call Stored Procedures.

I've spent most of the past few days reading up on ORMs and reading the arguments for and against using them instead of SProcs or as well SProcs, or not at all. It's a minefield, and I'm really looking for some clear guidance on how I should be structuring this project. I know there's the potential for discussion here but I'm hoping to avoid that by asking some clear questions:

As I understand it, an ORM will basically automatically generate SQL to perform the tasks that the developer needs, e.g. creating a new record, deleting a record, etc. I can see how that works for basic data manipulation, but what if things are a bit more complex? What if, for example, I don't actually want any records deleting from the database but, rather, I just want a record to be flagged as 'deleted' when a user deletes it so that it is hidden in the application but still present in the table? Presumably, an ORM would just generate code to simply delete the record and actually remove it, and calls to retrieve records from that table would include records flagged as 'deleted' unless the developer knew the underlying data structure well enough to know to exclude these.  By contrast, I can just write a SProc called 'DeleteRecord', which flags the specified record as 'deleted' and another called 'GetRecord' which only returns records not flagged as 'deleted', and when the developer uses these they don't need to know what's actually happening in the database. And this is just a simple example - some of my SProcs were going to do several things in response to a seemingly simple request (e.g. writing to an Audit table when a record is modified). How would an ORM handle this sort of thing?

The only reason for me creating a middle layer between the application and the database is so that future apps, particularly mobile apps, can access the data using the same method. My idea was to create web services which would retrieve data from the SProcs and return it in XML or JSON, as well as providing services for creating, updating and deleting. Does this seem reasonable?

Reading online about layered application development, recommendations vary from two to about eight different layers! Am I just being naïve when I wonder whether it's possible to just end up going down a 'layer black hole' in all of this? Our application will be used by hundreds, not thousands, and will contain hundreds of thousands (maybe eventually millions) of records in its key tables, not billions. I can see the benefits of a degree of abstraction, but I prefer to keep things as simple as I can at the same time!

If our hired developers arrive and I tell them that they're accessing the database using purely SProc calls and not an ORM, are they likely to be comfortable with this? Are they likely to look at me as if I'm from the dark ages? Would they be justified in thinking that we were going about this the wrong way?

There are probably lots more questions, I'm just having trouble articulating the utter confusion and slightly depressing sinking feeling that comes from having thought I had it all worked out and now wondering if I'm in way over my head.


答:
In the .NET world, your class qualifies as a plain old CLR object (POCO).   </p>

<p>In the Java world, you have plain old Java objects (POJOs).  </p>

Body:
In the .NET world, your class qualifies as a plain old CLR object (POCO).

In the Java world, you have plain old Java objects (POJOs).


答:
As others have answered, DAO and DTO reflect the role of the Object whether you want to use it in a persistence layer or to transfer informations in a call.

A value Object should be immutable.

Since your code is in Java (and the terminology is quite Java Oriented), the term that comes to my mind is JavaBean but your class should implement Serializable.

I think you should have a look at that answer on Stack Overflow


问:
Rideshare algorithm to be used for scheduled and adhoc journeys
I need some help writing some pseudo code for a rideshare system for taxi drivers.

The idea behind it is that it would combine real-time incoming taxi journeys and pre-bookings in real-time.

The steps I have come up so far are as follows:

The system sets a planning horizon of 1 hour (in the picture below its happening between 8am and 9am) and culls all the journey from the system within that time period (pre-bookings, adhoc bookings, etc)

The system then finds all jobs booked within 5 minutes of each other and groups them together

The system then determines if the passengers in the cars have agreed to share the journey

As a vehicle can only take 4 passengers, the system tries distribute the passengers accordingly, i.e. if one vehicle has 2 customers and another has 2 customers, and the above 2 conditions have been met (within 5 minutes of each other, passengers have agreed to share), then the system combines the journeys.

Of course there is the issue of figuring out how close and far each node (pickup or destination) is from each other. I have the lat lon positions for start and destination points but I haven't figured out how am I going to implement this.

The first thing I want to determine is whether my logic is right so far, and i will need your help with this.

Second thing, I want to do is figure out how I would determine whether a pickup is worthwhile. A passenger that lives 10 miles away from another one but meets all of the current conditions would have their journey combined. How do I tackle this issue?

答:
You are trying to solve an optimization problem. Realizing this is important for two reasons:

You do not necessarily have to find the absolutely best solution (that would take too long to calculate), but you can settle for a good-enough solution. You can fine-tune your algorithm later.

You have to very precisely state what you're optimizing for. I assume you want to minimize passenger expenses. However, combining multiple journeys will usually make them longer. Saving a penny is not worth it when my journey takes half an hour longer due to a detour. So there is a secondary optimization to keep the time for each journey reasonably minimal.

If we optimize for time and cost independently, we end up with a large solution space of equally optimal answers. This is undesirably; instead we need to combine cost and time into a single optimization variable. This connection could be something like “for every 10 minutes of detour, I want to save at least £2”. It also makes sense to impose additional constraints such as “Ride sharing may not prolong my journey by more than 20%” or “I want to save at least £2 by ride sharing, otherwise don't bother combining my journey”.

Once we have set up such a system of rules, we could solve the problem simply by brute force – iterate through all sets of journeys with up to 4 passengers, and figure out whether they ought to be combined. This is not a practical approach.

Instead, we need heuristics and filters.

We have to keep the search space as small as possible. Therefore, we can bung out obviously incompatible pairings at once. For example, we can see if the durations of the journeys overlap within that 20% margin. This implies that we not only need the start and end location for each journey, but also the expected travelling time without ride sharing. Filtering for temporal proximity is guaranteed not generate false negatives, provided you use something like this 20% margin.

Example

Let's assume we have 6 rides numbered 1–6. For each ride, we have the columns id, start, and duration. We want to find possibly compatible pairings. With brute force, we would have to look at n(n-1)/2 = 15 pairings (because for any IDs a and b, the pairing (a, a) is not valid, and (a, b) = (b, a)).

Our table (sorted by start time) might look like this:

ID start duration (start + f*duration)
1  08:00 10min     08:12
2  08:05 15min     08:23
3  08:10  5min     08:16
4  08:20  5min     08:26
5  08:30  9min     08:41
6  08:40  5min     08:46
```
<p>For each ride <code>a</code>, we can possibly combine it with any later ride <code>b</code> where <code>a.start + f*a.duration > b.start</code>, where <code>f</code> is the maximal ride elongation constraint (in my example: 20% → <code>f = 1.2</code>). Applying this criterion gives us the following 5 possible pairings:</p>
<pre class="lang-python prettyprint-override"><code>(1, 2), (1, 3)
(2, 3), (2, 4)
(5, 6)
</code>```

Python-like pseudocode for this algorithm:

rides = rides.sort(by: (r => r.start))
pairings = []
for i from 0 to rides.length:
  max_end = rides[i].start + f * rides[i].duration
  for j from i to rides.length:
    if rides[j].start < max_end:
      pairings.append([i, j])
    else:
      break
```
<p>This is also a good place to introduce the passenger constraint. Instead of blindly adding a pairing, we only do this if there are enough seats left:</p>
<pre class="lang-python prettyprint-override"><code>seats_per_ride = 4
...
if rides[j].start < max_end:
  total_passengers = rides[i].passengers + rides[j].passengers
  if total_passengers <= seats_per_ride:
    pairings.append([i, j])
else:
  break
</code>```

This still ignores that we can combine more than two rides. However, this can now be done as a post-processing of the resulting rides. As long as the passenger count is suitable, we can fold (a, b) and (a, c) into (a, b, c), which should be considered in addition to the existing pairings. Remember also that (a, b) and (b, a) are equivalent, so it makes sense to view combined rides as sets of rides, not just as tuples or pairs. If we apply these thoughts to the five pairings we already found, we also get

(1, 2, 3), (1, 2, 3, 4), (2, 3, 4)
```
<p>Finding <em>all</em> sharing opportunities becomes fairly easy if we change the above algorithm to work backwards through the rides, as this can reuse existing calculations. This makes heavy use of the ordering to maintain correctness.</p>
<pre class="lang-python prettyprint-override"><code>rides = rides.sort(by: (r => r.start), descending)
pairings = []
last_pairings = []
for i from 0 to rides.length:
  max_end = rides[i].start + f * rides[i].duration
  found_pairings = [[i]]
  for pairing in last_pairings:
    if pairing[0].start < max_end:
      new_pairing = [i, *pairing]
      total_passengers = sum(new_pairing.map(id => rides[id].passengers))
      if total_passengers <= seats_per_ride:
        pairings.append(new_pairing)
        if total_passengers < seats_per_ride:
          found_pairings.append(new_pairing)
    else:
      break
   last_pairings = found_pairings
return pairings
</code>```

However, it will also be useful to apply possibly inaccurate filters, as we're only interested in a “good enough” solution. For example, we can ignore pairings if they are too far apart, where “too far” is where that inaccurate part comes in. We'd want to defer actual route finding as far as possible, because that really takes time. While we really should be optimizing for time, our heuristics might use the distance on the map. In short, you could draw geometric shapes such as rectangles, circles, or ellipses around each route, and see which ones overlap. That's still pretty crude, and you might get better performance if you store your journeys in a spatial database from the start, and explicitly query for close journeys using such a metric.

After we've applied all filters, we still end up with a couple of candidate ride sharing opportunities. There is no way around calculating possible combined routes. The candidate combined routes are all routes with all start and end points for each passenger as waypoints, with the additional restriction that each end point must come after the corresponding start point. You can then grade each route for each passenger by our optimization metric (and possibly realize that this route would violate our additional constraints).

We have now arrived at the solution space (a set of possible rides for each passenger), and have to pick one solution (which passenger picks which ride). The remaining problem is that one passenger may have multiple options to share rides. When the user picks one option, the passengers of the other options will have to pay more. It is difficult to find the perfect solution where all passengers combined pay as little as possible, so I'd settle for a non-deterministic strategy (at least for now, you can always fine-tune later). The most obvious strategy tries to find the local optimum for each passenger:

for each passenger:
  pick the ride where that passenger has to pay the least fare.
  eliminate all other sharing opportunities that included this passenger.
```

More advanced strategies might use a priority queue of passenger that sorts them by the amount saved, or by an external metric such as booking time.

Body:
You are trying to solve an optimization problem. Realizing this is important for two reasons:

You do not necessarily have to find the absolutely best solution (that would take too long to calculate), but you can settle for a good-enough solution. You can fine-tune your algorithm later.

You have to very precisely state what you're optimizing for. I assume you want to minimize passenger expenses. However, combining multiple journeys will usually make them longer. Saving a penny is not worth it when my journey takes half an hour longer due to a detour. So there is a secondary optimization to keep the time for each journey reasonably minimal.

If we optimize for time and cost independently, we end up with a large solution space of equally optimal answers. This is undesirably; instead we need to combine cost and time into a single optimization variable. This connection could be something like “for every 10 minutes of detour, I want to save at least £2”. It also makes sense to impose additional constraints such as “Ride sharing may not prolong my journey by more than 20%” or “I want to save at least £2 by ride sharing, otherwise don't bother combining my journey”.

Once we have set up such a system of rules, we could solve the problem simply by brute force – iterate through all sets of journeys with up to 4 passengers, and figure out whether they ought to be combined. This is not a practical approach.

Instead, we need heuristics and filters.

We have to keep the search space as small as possible. Therefore, we can bung out obviously incompatible pairings at once. For example, we can see if the durations of the journeys overlap within that 20% margin. This implies that we not only need the start and end location for each journey, but also the expected travelling time without ride sharing. Filtering for temporal proximity is guaranteed not generate false negatives, provided you use something like this 20% margin.

Example

Let's assume we have 6 rides numbered 1–6. For each ride, we have the columns id, start, and duration. We want to find possibly compatible pairings. With brute force, we would have to look at n(n-1)/2 = 15 pairings (because for any IDs a and b, the pairing (a, a) is not valid, and (a, b) = (b, a)).

Our table (sorted by start time) might look like this:

ID start duration (start + f*duration)
1  08:00 10min     08:12
2  08:05 15min     08:23
3  08:10  5min     08:16
4  08:20  5min     08:26
5  08:30  9min     08:41
6  08:40  5min     08:46

For each ride a, we can possibly combine it with any later ride b where a.start + f*a.duration > b.start, where f is the maximal ride elongation constraint (in my example: 20% → f = 1.2). Applying this criterion gives us the following 5 possible pairings:

(1, 2), (1, 3)
(2, 3), (2, 4)
(5, 6)

Python-like pseudocode for this algorithm:

rides = rides.sort(by: (r => r.start))
pairings = []
for i from 0 to rides.length:
  max_end = rides[i].start + f * rides[i].duration
  for j from i to rides.length:
    if rides[j].start < max_end:
      pairings.append([i, j])
    else:
      break

This is also a good place to introduce the passenger constraint. Instead of blindly adding a pairing, we only do this if there are enough seats left:

seats_per_ride = 4
...
if rides[j].start < max_end:
  total_passengers = rides[i].passengers + rides[j].passengers
  if total_passengers <= seats_per_ride:
    pairings.append([i, j])
else:
  break

This still ignores that we can combine more than two rides. However, this can now be done as a post-processing of the resulting rides. As long as the passenger count is suitable, we can fold (a, b) and (a, c) into (a, b, c), which should be considered in addition to the existing pairings. Remember also that (a, b) and (b, a) are equivalent, so it makes sense to view combined rides as sets of rides, not just as tuples or pairs. If we apply these thoughts to the five pairings we already found, we also get

(1, 2, 3), (1, 2, 3, 4), (2, 3, 4)

Finding all sharing opportunities becomes fairly easy if we change the above algorithm to work backwards through the rides, as this can reuse existing calculations. This makes heavy use of the ordering to maintain correctness.

rides = rides.sort(by: (r => r.start), descending)
pairings = []
last_pairings = []
for i from 0 to rides.length:
  max_end = rides[i].start + f * rides[i].duration
  found_pairings = [[i]]
  for pairing in last_pairings:
    if pairing[0].start < max_end:
      new_pairing = [i, *pairing]
      total_passengers = sum(new_pairing.map(id => rides[id].passengers))
      if total_passengers <= seats_per_ride:
        pairings.append(new_pairing)
        if total_passengers < seats_per_ride:
          found_pairings.append(new_pairing)
    else:
      break
   last_pairings = found_pairings
return pairings

However, it will also be useful to apply possibly inaccurate filters, as we're only interested in a “good enough” solution. For example, we can ignore pairings if they are too far apart, where “too far” is where that inaccurate part comes in. We'd want to defer actual route finding as far as possible, because that really takes time. While we really should be optimizing for time, our heuristics might use the distance on the map. In short, you could draw geometric shapes such as rectangles, circles, or ellipses around each route, and see which ones overlap. That's still pretty crude, and you might get better performance if you store your journeys in a spatial database from the start, and explicitly query for close journeys using such a metric.

After we've applied all filters, we still end up with a couple of candidate ride sharing opportunities. There is no way around calculating possible combined routes. The candidate combined routes are all routes with all start and end points for each passenger as waypoints, with the additional restriction that each end point must come after the corresponding start point. You can then grade each route for each passenger by our optimization metric (and possibly realize that this route would violate our additional constraints).

We have now arrived at the solution space (a set of possible rides for each passenger), and have to pick one solution (which passenger picks which ride). The remaining problem is that one passenger may have multiple options to share rides. When the user picks one option, the passengers of the other options will have to pay more. It is difficult to find the perfect solution where all passengers combined pay as little as possible, so I'd settle for a non-deterministic strategy (at least for now, you can always fine-tune later). The most obvious strategy tries to find the local optimum for each passenger:

for each passenger:
  pick the ride where that passenger has to pay the least fare.
  eliminate all other sharing opportunities that included this passenger.

More advanced strategies might use a priority queue of passenger that sorts them by the amount saved, or by an external metric such as booking time.


问:
Is MapReduce an architectural pattern?
For a documentary reseach: Architectural Patterns (ex MapReduce), I started by reading the MapReduce: Simplified Data Processing on Large Clusters paper. the authors define MapReduce as "a programming model and an associated implementation for processing and generating large data sets". Can we consider that MapReduce is an architectural pattern? or a design pattern? Why?.</p>

答: MapReduce is a lot more than simply stringing together a Map and a Reduce operation.  It's about distributed computing, running operations on a cluster over a large data set.  It contains architecture for initiating, monitoring and coordinating operations on the cluster, as well as for restarting operations on a system that fails (which happens sometimes on a large cluster), etc.  It definitely counts as an architecture pattern, beyond what appears on the surface to be the core functionality.</p>

Title: Is MapReduce an architectural pattern?


问:
How to deal with Windows .NET configuration files
We use the normal app.config file for the configuration of the application. Although it works for now it has some pretty significant drawbacks:

Upgrading an application with a modified app.config is hard. Either it overwrites the existing configuration file or the upgraded app.config isn't installed at all.

An error in app.config often renders the entire configuration invalid.

The files are read-only and cannot be altered from the application itself.

To overcome these issues I am considering some alternative options.

Custom XML document transformation

An alternative would be to revert back to a custom XML file to prevent these drawbacks. I think of a solution that uses XML document transformation.

The installer will install the default configuration file default.xml and is read-only. It shall never be modified, because it is overwritten during an upgrade. It contains all the default settings and should be sufficient to start the application.

Modifications can be done in the custom-xxx.xml files that are processed in alphabetic order. The modifications are done using the XDT syntax as described here.

The resulting file will be written to configuration.xml that is actually been used. The configuration.xml file should never be directly altered, because it will be overwritten when the application is started again.

I think this approach will have some advantages:

The default configuration can be updated during an application upgrade.

Local configuration changes can be applied and will be persisted during upgrades.

If transformation cannot be applied (syntax error) then the file is skipped, but the default configuration can always be read.

There are some disadvantages too:

XDT is not trivial for most users and error-prone.

Modifying the configuration from within the application is difficult, because you should write an XDT file.

Not all settings can be done outside the app.config, because the .NET framework looks for certain settings in this file.

Custom.xml only

The application has default settings specified in code. These settings can be overriden by the application by using a custom.xml file. This has some advantages:

Defaults are specified in code, so will be upgraded automatically when the application is updated.

Local configuration changes can be applied and will be persisted during upgrades.

If the custom.xml contains errors, then this file is skipped and the default settings can still be used.

Relatively easy to create a GUI to modify the settings and only write the non-default settings.

There are some disadvantages too:

There is no default.xml, so not all options are clearly visible in the XML file.

When the default configuration contains a list, then there is no way to remove items from the list in the custom.xml. Configurations should be modelled in such a way that these circumstances can be handled.

Wrap-up

There are some different scenarios to deal with configuration files. I would like to know how you deal with configurations in situations where the configuration files need to be customized and you want to maintain automatic upgrade scenarios.

答:
I think part of the issues you're facing is in the premise:

Upgrading an application with a modified app.config is hard. Either it
  overwrites the existing configuration file or the upgraded app.config
  isn't installed at all.

Don't do that, basically. Configuration is a software artefact, and it should be versioned and go through the same lifecycle as any other artefact, subject to change control and whatever other safeguards your team implements for software artefacts.

I suppose realistically, in a production environment you are likely to have hacks made to the application config to fix problems in an emergency. Ideally the immediate consequence of having to do that would be a properly-managed change that would be deployed to production formally as soon as humanly possible. Therefore a scheduled upgrade would already have this change, and regression shouldn't be a problem.

Criticising the premise probably doesn't provide an adequate answer, so could you expand your question to provide an example of the kind of settings you find to be problematic?


问:
How to design a scriptable communication emulator?
Requirement:
We need a tool that simulates a hardware device that communicates via RS232 or TCP/IP to allow us to test our main application which will communicate with the device.

Current flow:

User loads script

Parse script into commands

User runs script

Execute commands

Script / commands (simplified for discussion):

Connect RS232 => RS232ConnectCommand

Connect TCP/IP => TcpIpConnectCommand

Send data => SendCommand

Receive data => ReceiveCommand

Disconnect => DisconnectCommand

All commands implement the ICommand interface. The command runner simply executes a sequence of ICommand implementations sequentially thus ICommand must have an Execute exposure, pseudo code:

void Execute(ICommunicator context)

The Execute method takes a context argument which allows the command implementations to execute what they need to do. For instance SendCommand will call context.Send, etc.

The problem
RS232ConnectCommand and TcpIpConnectCommand needs to instantiate the context to be used by subsequent commands. How do you handle this elegantly?

Solution 1:
Change ICommand Execute method to:

ICommunicator Execute(ICommunicator context)

While it will work it seems like a code smell. All commands now need to return the context which for all commands except the connection ones will be the same context that is passed in.

Solution 2:
Create an ICommunicatorWrapper (ICommunicationBroker?) which follows the decorator pattern and decorates ICommunicator. It introduces a new exposure:

void SetCommunicator(ICommunicator communicator)

And ICommand is changed to use the wrapper:

void Execute(ICommunicationWrapper context)

Seems like a cleaner solution.

Question

Is this a good design? Am I on the right track?

答:
I think solution 2 is the better solution. You want to avoid treating connection commands as special cases. That would add complexity to your script engine and/or script language. Your commands should receive a general purpose execution context (or script state).

Some psuedo-code could be:

```<code>interface ICommand {
    void Execute(IScriptContext context);
}

interface IScriptContext {
    ICommunicator Channel { get; set; }
}

interface ICommunicator{
    int Read(byte[] buffer, int offset, int count);
    int Write(byte[] buffer, int offset, int count);
    int GetBytesAvailable();
}

class RS232CommunicationChannel : ICommunicator...
class TcpIpCommunicationChannel : ICommunicator...
class SharedMemoryCommunicationChannel : ICommunicator...

class ConnectRS232Command : ICommand {
    void Execute(IScriptContext context) {
        context.CommunicationChannel = new RS232CommunicationChannel();
    }
}
</code>```

The execution loop is then very simple:

```<code>IList<ICommand> commandList = LoadCommands();
IScriptContext context = new ScriptContextImpl();
foreach(ICommand cmd in commandList) {
    cmd.Execute(context)
}
</code>```

Body:
I think solution 2 is the better solution. You want to avoid treating connection commands as special cases. That would add complexity to your script engine and/or script language. Your commands should receive a general purpose execution context (or script state).

Some psuedo-code could be:

interface ICommand {
    void Execute(IScriptContext context);
}

interface IScriptContext {
    ICommunicator Channel { get; set; }
}

interface ICommunicator{
    int Read(byte[] buffer, int offset, int count);
    int Write(byte[] buffer, int offset, int count);
    int GetBytesAvailable();
}

class RS232CommunicationChannel : ICommunicator...
class TcpIpCommunicationChannel : ICommunicator...
class SharedMemoryCommunicationChannel : ICommunicator...

class ConnectRS232Command : ICommand {
    void Execute(IScriptContext context) {
        context.CommunicationChannel = new RS232CommunicationChannel();
    }
}

The execution loop is then very simple:

IList<ICommand> commandList = LoadCommands();
IScriptContext context = new ScriptContextImpl();
foreach(ICommand cmd in commandList) {
    cmd.Execute(context)
}


答:
Just leave out the event keyword, instead of

```<code>public event SampleEventHandler SampleEvent;
</code>```

use just standard delegates (in C#, delegates are multicast enabled). Declare it like this:

```<code>delegate void SampleEventHandler(object sender, MyEventArgs e);
</code>```

Now, for introducing priorities, use something like a priority queue. As a simple form, you can utilize a dictionary:

```<code>Dictionary<int,SampleEventHandler> EventDictionary;
</code>```

where the dictionary keys are the different priorities. This dictionary serves as an "event" in the class where you define it. To add some new event handler, you need something like this:

```<code>public void AddNewEvent(int priority,SampleEventHandler handler)
{
     if(!EventDictionary.ContainsKey(priority))
         EventDictionary.Add(priority, null );
     EventDictionary[priority]+=handler;
}
</code>```

To raise the events in low-prio-first order, you need some code like this:

```<code>foreach(int priority in EventDictionary.Keys.OrderBy(p=>p))
     EventDictionary[priority](this,myEventArgs);
</code>```

Title: How to write a custom EventHandler class

Body:
When I started using Java, I created a custom Event class to mimic C#'s implementation of Events. Since then, I've added some other features, like event listener priorities, and cascading events were priorities are respected. Now that I'm using C# again, I wanted to create an eventhandler class, or at least a class that I can use the event keyword with, that incorporated these features. Starting off with, I've hit a bit of a brick wall where I can't subclass EventHandler, being told that it's a sealed class, and I can only use the event keyword with EventHandler/EventHandler and its subclasses.

Is this a fool's errand? And should I just lump for a more straight-forward translation of my event class, ignoring the event system already in-place? Or is there I way I can do this successfully?

Body:
Just leave out the event keyword, instead of

public event SampleEventHandler SampleEvent;

use just standard delegates (in C#, delegates are multicast enabled). Declare it like this:

delegate void SampleEventHandler(object sender, MyEventArgs e);

Now, for introducing priorities, use something like a priority queue. As a simple form, you can utilize a dictionary:

Dictionary<int,SampleEventHandler> EventDictionary;

where the dictionary keys are the different priorities. This dictionary serves as an "event" in the class where you define it. To add some new event handler, you need something like this:

public void AddNewEvent(int priority,SampleEventHandler handler)
{
     if(!EventDictionary.ContainsKey(priority))
         EventDictionary.Add(priority, null );
     EventDictionary[priority]+=handler;
}

To raise the events in low-prio-first order, you need some code like this:

foreach(int priority in EventDictionary.Keys.OrderBy(p=>p))
     EventDictionary[priority](this,myEventArgs);


答:
Have you considered hiring your developers and then letting them decide what architecture to use? By doing this you will ensure that you aren't excluding developers from the selection process simply because they don't like the architecture you've chosen. Getting the best possible team of developers is the most important thing for ensuring your project is successful, and with more to choose from you may find better developers that you're happier working with.

People are more important than architecture.


问:
How to determine which service to call
I have modeled my application using multiple 'user' domain objects.

Candidate

Recruiter

Administrator

Each entity extends from an AbstractUser implementing UserInterface.

```<code>interface UserInterface
{
    public function getEmail();
    public function setEmail($email);
    public function getPassword();
    public function setPassword($password);
}
</code>```

They all have their own a services CandidateService, AdminService etc implementing UserServiceInterface

```<code>interface UserServiceInterface
{
   public function getUserByEmail();
}
</code>```

My problem arises when I need to fetch a user by their email address (say PasswordResetService) I am unable to determine which service I should be calling as the email could be either of them. I am also using mongoDB so I cannot simply run a native query joining the 'tables'.

My current thought is that I have to inject each service into the PasswordResetService and call getUserByEmail() on each of them until I get my desired user.

```<code>class PasswordResetService
{
   public function getUserByEmail($email)
   {
       $user = $this->userService->getUserByEmail($email);
       if ($user) return $user;

       $user = $this->recruiterService->getUserByEmail($email);
       if ($user) return $user;

       // etc ...
   }
}
</code>```

From a design perspective the above seems to me like a poor approach as should I add a new user 'type' I would need to update the password service also.

Is there a better way of achieving this?

答:
I would approach this by having a top level UserService that doesn't care what type of user it's dealing with.

If it gets a request for a user by email, it can look up on the database which type of user is correct and delegate the request to the correct service. This may sound a little inefficient, but used with an appropriate query caching later it's actually fine.

Other methods that are passed a user object could perhaps use double dispatch to forward the request to the correct service object., thus eliminating the need to hit the database for these operations.

Title: How to determine which service to call


问:
Are promises suitable for Model Controller communication?
I have an Express app built with the Yeoman Angular fullstack generator to build an API to send JSON to Angular.

I'm trying to work out the best way to pass back information from my model to my controller, further processing may occur in the controller. I'm currently using promises. I feel uncomfortable about my model returning a promise to the controller. Should I?

Currently my Model looks like this:

```<code>function returnCountries(lim, offset) {
    var promise = new Promise(function(resolve, reject) {
        //SQL is just an example
        connection.query('SELECT * FROM countries LIMIT 1', function(err, result) {
            return resolve(result);
        });
    })
    return promise
}

function returnCountry(countryID) {
  //not yet implemented
}
module.exports = {
    "returnCountries": returnCountries,
    "returnCountry": returnCountry
}
</code>```

And controller looks like this:

```<code>var Country = require('./country.model');

// Get list of countries
exports.index = function(req, res) {
      Country.returnCountries().then(function(countries){
              return res.json(200, countries);
})
</code>```

答:
Wouldn't it be simpler to pass the callback to connection.query around?

```<code>// model

function listCountries(limit, offset, callback) {
    // SQL is just an example
    connection.query('SELECT * FROM countries LIMIT 1', callback);
}

// controller

var Country = require('./country.model');

exports.index = function(req, res) {
    var limit, offset;

    Country.listCountries(limit, offset, function(error, result) {
        // TODO: handle error
        return res.json(200, result);
    });
};
</code>```

Unless your controller has much more async work to do; then promises would probably make sense for avoiding callback hell. Maybe using just one promise is what's making you uncomfortable, it kind of seems like overkill at this point.

OwnerDisplayName: Joseph

Body:
Wouldn't it be simpler to pass the callback to connection.query around?

// model

function listCountries(limit, offset, callback) {
    // SQL is just an example
    connection.query('SELECT * FROM countries LIMIT 1', callback);
}

// controller

var Country = require('./country.model');

exports.index = function(req, res) {
    var limit, offset;

    Country.listCountries(limit, offset, function(error, result) {
        // TODO: handle error
        return res.json(200, result);
    });
};

Unless your controller has much more async work to do; then promises would probably make sense for avoiding callback hell. Maybe using just one promise is what's making you uncomfortable, it kind of seems like overkill at this point.


答:
Call delete method, which sets the delete flag and updates the object.  ORMs have some impressive functionality beyound simple CRUD operations.  And they still allow SQL as a fall back.

If by 'the application' you mean the UI, then the real reason for the middle layer is so you don't have a 'Smart UI' design which becomes harder and harder to maintain and improve.  Instead, you have now seperated out concerns.  The ability to add a new front end is one of the benefits of seperating concerns.

Stick with 3 for starting.  Data persistance (aka, database).  Data model.  User interface.  Going beyond that at the start, unless you really know what you are doing, is overengineering it.

This depends upon why you are telling them.  For a brand new, not yet built system, if you weren't upfront with this and you are laying down the law, it will be taken as micromanagement and will scare off the developers who have other options (and those are the ones you most want to keep).  If you say no ORM because we have a production enterprise application without one and it'll be too much work to add it in, but you remain open to being convinced otherwise down the road, it'll be taken better.

If you plan to hire devs from the start, get them and have them help make these calls.  If you want to get a product out by yourself and are worried about building it up once it becomes a hit and you'll need a bigger team, file that away under 'problems I want to have' and pick what ever you will be most likely to get a working project with.


问:
Reducing dependency cycles and reducing coupling
I'm trying to learn how to produce quality object-oriented code and have been studying concepts like SOLID. I'm currently working on an entity-component-process system for a small game engine.

Currently, I have it implemented as such:

There is a ScreenStack which contains a stack of Screens

Each Screen has an EntityManager and ProcessManager.

The ProcessManager updates all Processes given entities from the EntityManager.

A Process handles all game logic, so it needs to be able to use ScreenStack to possibly push and pop screens. It can also create, remove, and change entities, so it needs the EntityManager from Screen. Basically a Process needs to know everything about the game since it affects so much of it, but it feels wrong.

How do I go about implementing this better? It seems like everything has a clear dependency hierarchy until you get to a process, where it gets thrown out the window.

There also seems to be tight coupling when I would want to push a new screen. Say, for example, I have a process in a MainMenu screen that checks for menu choice. If "New Game" is clicked, I need to push a new screen, which gets created at that moment. I've read that I shouldn't randomly throw in new which this seems to be doing.

答:
I would suggest looking into "Tell Don't Ask" principle. Martin Fowler gives a good introduction here

The common implementation usually is based on messaging/notifications between the classes. Note, TDA is a "pure OOP" that object oriented languages are designed for. Unlike the most common mixed mode of OO+functional code together.

Another advice is to work on separation of queries from updates (do not allow code in same methods or even classes to request data and act on it) - that removes a lot of accidental complexity and greatly improves SRP.


问:
Designing a better performing total permissions setup for multiple permission levels
We've got a global system that we are attempting to solve a permissions issue around.  Currently, our system serves a number of different applications out to our clients and each client has their own list of user types and users for our system.  However, all of these things can be independent of each other.  Such that Client A can have access to Application A and Application B.  Client A might also have give different User Types, all with different Users.  Each 'level' of usage would add a new level of complexity to how we give permissions out to everything.

For example: I might have three permissions, called "Grape", "Purple", and "Car".  At the global level (meaning all applications, clients, user types, and users), I deny the permission on Grape, but allow permission for Purple and Car.  That means that no application, no client, no user type, and no user can access Grape, but they can access Purple and Car.  So now, we have two applications, named "Fruit" and "Auto".  Currently, both Fruit and Auto would look down to the global permissions created for Grape, Purple, and Car and use those.  So, this is how things would look:

Fruit

Grape: Denied

Purple: Granted

Car: Granted

Auto

Grape: Denied

Purple: Granted

Car: Granted

But, for the applications, this might not make sense.  So, I would create an application specific permission to grant permissions for Grape and deny permissions for Car for the application of Fruit.  So now, the permissions would look like this.

Fruit

Grape: Granted (a)

Purple: Granted

Car: Denied (a)

Auto

Grape: Denied

Purple: Granted

Car: Granted

While I still have the global permissions telling Auto what permissions to use, I have specific permissions telling Fruit that it needs to grant and deny different permissions there.  Now, we'd add another level of complexity and add in a client.  Let's say I have a client called "Fox".  Fox uses both applications Fruit and Car.  Right now, without adding anything else, the permissions to the three objects (Grape, Purple, and Car) come from the application wide permission level.  But, maybe Fox wants things different.  So, instead of having Purple granted on application Fruit, it denies that permission specifically.  In terms of data, that would be a specific record you would add just for client Fox that overrides the permissions of Purple to be denied, regardless of what the application or global permissions said for Purple.  So, now you have something that looks like this.

Fruit

Fox

Grape: Granted (a)

Purple: Denied (c)

Car: Denied (a)

Auto

Fox

Grape: Denied

Purple: Granted

Car: Granted

Remember, too, that a lot of this can be pretty abstracted.  You could have clients that only use one application, three applications, or every application you have in your system.  And for those clients, you might have very specific user lists that can only use what the client can use, so they'd be inheriting permissions down from the client unless specifically overwritten.

Obviously, this can get increasingly complex when you add in a level for the user type and then for the user.  Keeping track of it all can be very cumbersome, but it's something we have to account for based on our system's model.  So, we came up with a scoring solution for permissions.

We would query the database and tables and pass in the current application, client, user type, and user to the database for whatever we have current at the time.  The database would then use a scoring solution to determine if we can see or not see a particular permission.  Application would be a score of 1, client would be a score of 2, user type would be 4, user would be 8, ect.  The more defined objects you put in, the higher the maximum score can get.  This gives us a very clear definition of what permissions are active for a given set of objects.

Our real problem now is performance.  We now have 5 levels of objects in our system and might be adding more, if the design of the system requires it.  However, querying all of these levels is a serious performance hit on SQL.  And each object will just make the problem exponentially worse.  My question is this: how can I achieve the same type of permission inheritance system here, and give the ability to add new levels when needed, without exponentially increasing the performance hit.  There has to be other companies and applications that have this same problem and have better solutions to it then we do.

答:
Please take a look at the following thread.

Groups is usually the simplest way to go. Note, you don't necessary have to have 'Group' object and link it to your roles/users, simpler approach is tagging. Having specific set of tags on the objects itself identifies the set of groups it belongs to - hence the permissions.

Fox [tags: auto, fruit]

Auto [tags: purple, car]

Fruit [tags: grape]

something along these lines...