答:
My guess is that you can pursue #3 by way of #2 and that it won't require anything close to a total refactor. Often in these situations, you just need to make a few adjustments at the entry points and (sometimes) exit points.

If needed, wrap your current script in a main() function.

```<code>def main(args, stdin = None):
    if stdin is None:
        stdin = sys.stdin

    # full script here

if __name__ == '__main__':
    main(sys.argv[1:])
</code>```

That function should take a list of strings. When you parse command-line options, operate on args, not sys.argv.

Similarly, adjust other parts of your script to avoid direct operations on sys.stdin and related streams. Instead, use the arguments passed to your main() function.

Programatic users (as opposed to command-line users) will pass in a list of strings and any open file handles (or iterables) they want the code to use. Later, if needed, you can make the programatic use more natural by writing a function that knows how to convert typical positional and keyword-style arguments into the list of strings that your option parser needs.

Body:
My guess is that you can pursue #3 by way of #2 and that it won't require anything close to a total refactor. Often in these situations, you just need to make a few adjustments at the entry points and (sometimes) exit points.

If needed, wrap your current script in a main() function.

def main(args, stdin = None):
    if stdin is None:
        stdin = sys.stdin

    # full script here

if __name__ == '__main__':
    main(sys.argv[1:])

That function should take a list of strings. When you parse command-line options, operate on args, not sys.argv.

Similarly, adjust other parts of your script to avoid direct operations on sys.stdin and related streams. Instead, use the arguments passed to your main() function.

Programatic users (as opposed to command-line users) will pass in a list of strings and any open file handles (or iterables) they want the code to use. Later, if needed, you can make the programatic use more natural by writing a function that knows how to convert typical positional and keyword-style arguments into the list of strings that your option parser needs.


问:
How many threads should I use in my NIO server?
I've built a Java NIO TCP server, it currently uses four threads.
One ServerRunnable thread that uses a selector and three worker threads.

I've been looking around for some information about this, as I've read in the past that you should only have one thread per processor core. So that's that.

But that got me wondering recently, and after a little more research I came across this thread.

Where, in the comments on the accepted answer user Donal Fellows points out the following:

Have at most one CPU-bound thread per processor allocated to the application. IO-bound threads aren't a big problem (other than the memory they consume) and it's important to remember that apps can be restricted to only use a subset of the system's CPUs; after all, it's (usually) the user's/admin's computer and not the programmer's.

With that in mind, am I correct in thinking I can safely increase the number of selector threads and worker threads in my thread pool.

My server thread reads input, processes the data into JSONObjects and then pushes them to a queue. The worker threads then take the JSONObjects from the queue, checks what type of objects they are and then pushes them to the database.
So there is very little computational work going on there. Is it safe enough for me to increase the number of threads here, as in use more ServerRunnable threads and more worker threads?
Say to double the amount of each for example?

What do I need to think about when considering something like this?

答:
You have no reason to assume how many CPU cores the users system has. You development machine might run on a 4-core CPU with nothing else to do, but it could also be moved to a single-core virtual machine or a 32-core high-end server.

For that reason you should not hard-code the number of CPUs.

In Java, you can use ThreadPoolExecutor's to delegate the thread management to the JVM. You usually pass smaller work packages to the executor in form of objects which implement Runnable and leave the decision which thread processes each runnable to the thread pool. In your case, the individual JSONObjects would be such work packages.

The ThreadPoolExecutor allows you to set the minimum and maximum number of CPUs it is allowed to use. I would recommend you to make this configurable and default to Runtime.getRuntime().availableProcessors() when no configuration option is provided.

Whether or not your IO thread counts as an actual thread in regards to its CPU load depends on what it is actually doing. When it receives a high IO bandwidth and has considerable parsing work to do, it might. But we can not tell you this without profiling your application under real-world conditions.

ContentLicense: CC BY-SA 3.0


问:
Do designer tools degrade the programming experience?
I've been looking around lately, specifically at some of the MS tools that are available, and I'm noticing a big focus on designer tools and wizards. Not just for UI development but for everything.

Entity Framework has the modeller

RIA Services has the DomainService wizard(s)

Workflow has the whole workflow designer thingy... (I don't know, haven't really used it)

There's more, but I think you get the idea. There's lots of designer tools. Using some of these I find that:

They complicate matters beyond the prescribed use cases

(ie all of the tech demo videos)

I have been evaluating some of these technologies recently, and trying to work with them I end up having to dissect exactly what the designers, modellers and wizards are doing for me... otherwise I'm lost when I actually have to try to do something with whatever was created.

This ends up being a case where I have to fight the tool, or fight its output enough such that I could have just done the whole thing myself without it - and had a much stronger understanding of what's going on. I find this particularly infuriating with the silverlight designer and RIA services domain service wizard.

I find myself asking "What good is this tool if I have to figure out its inner workings or re-write half of its output in order to use it?"

They're not as fast

The selling point of these tools is to increase productivity and this point may change over time using the tool, and doesn't necessarily apply to UI designers (though, in some cases it still does -> I'm looking at you silverlight designer). I find that I can hack some code much faster than I can drag-and-drop, resize, move, whatever in a designer.

The UI gets in the way of the model

Maybe this is just me, but when I am using anything reminiscent of a UML design tool I end up spending more time laying everything out so that my lines don't cross and so that I can see it all on the screen than modelling what I'm trying to achieve.

They're no fun

Half of the reason that I code for a living is that I enjoy it. Clicking checkboxes and selecting comboboxes and then fixing everything that comes out isn't fun.

I don't appear to be alone

The community seems to not want these tools either. The best example I can think of at the moment is Entity Framework Code-First.

So I ask:

Do designer tools actually improve productivity?

Are they fun-killers?

Is 'the community' actually asking for more designer tools, or are vendors just thinking we are?

答:
Same here, no design tool.

I design the database ERD on paper, and write a deployer that issues the "create table" statements, that way I can wipe and rebuild at will. Also helps for database versioning.

I find that 11x17 (A3) paper can be a great place to design very compelex systems. Get a good eraser and pencils (I prefer Staedtler).

ContentLicense: CC BY-SA 2.5


问:
Debugging memory corruption
First off, I do realize this is not a perfect Q&A style question with an absolute answer, but I can't think of any wording to make it work better. I don't think there is an absolute solution to this and this is one of the reasons why I'm posting it here instead of Stack Overflow.

Over the last month I have been rewriting a fairly old piece of server code (mmorpg) to be more modern and easier to extend/mod.
I started with the network portion and implemented a 3rd party library (libevent) to handle stuff for me.
With all the re-factoring and code changes I introduced memory corruption somewhere and I have been struggling to find out where it happens.

I can't seem to reliably reproduce it on my dev/test environment, even when implementing primitive bots to simulate some load I don't get crashes any more (I fixed a libevent issue which caused some stuff)

I have tried so far:

Valgrinding the hell out of it - No invalid writes until the thing crashes (which may take 1+ day in production.. or just an hour) which is really baffling me, surely at some point it would access invalid memory and not overwrite stuff by chance? (Is there a way to "spread out" the address range?)

Code-Analysis tools, namely coverity and cppcheck. While they did point out some.. nastiness and edge cases in the code there was nothing serious.

Recording the process until it crashes with gdb (via undodb) and then working my way backwards. This /sounds/ like it should be doable, but I either end up crashing gdb by using the auto-complete feature or I end up in some internal libevent structure where I get lost since there's too many possible branches (one corruption causing another and so on).
I guess it would be nice if I could see what a pointer originally belongs to/where it was allocated, that would eliminate most of the branching-issues. I cant run valgrind with undodb though, and I the normal gdb record is unusably slow (if that even works in combination with valgrind).

Code review! By myself (thoroughly) and having some friends look over my code, though I doubt it was thorough enough. I was thinking about maybe hiring a dev to do some code review/debugging with me, but I cant afford to put too much money in it and I wouldn't know where to look for someone who'd be willing to work for little-to-no money if he doesn't find the issue or anyone qualified at all.

I should also note: I usually get consistent backtraces.
There are a few places where the crash happens, mostly related to the socket class becoming corrupted somehow. Be it an invalid pointer pointing to something which isn't a socket or the socket class itself becoming overwritten (partially?) with gibberish. Although I suspect it's crashing there the most since that's one of the mostly used parts, so it's the first corrupted memory which gets used.

All in all this issue has had me busy for nearly 2 month (on and off, more of a hobby project) and is really frustrating me to the point where I become grumpy IRL and think about just giving up.
I just can't think about what else I am supposed to do to find the issue.

Are there any useful techniques I missed?
How do you  deal with that? (It can't be that common since there isn't much information about this.. or I'm just really blind?)

Edit:

Some specs in case it matters:

Using c++(11) via gcc 4.7 (version supplied by debian wheezy)

The codebase is around 150k lines

Edit in response to david.pfx post: (sorry for the slow response)

Are you keeping careful records of crashes, to look for patterns?

Yes, I still have dumps of the recent crashes lying around

Are the few places really similar? In what way?

Well, in the most recent version (they seem to change whenever I add/remove code or change related structures) it would always get caught in an item timer method. Basically an item has a specific time after which it expires and it sends updated info to the client.
The invalid socket pointer would be in the (still valid as far as I can tell) Player class, mostly related to that.
I am also experiencing loads of crashes in the cleanup phase, after the normal shutdown where it's destroying all the static classes that haven't been explicitly destroyed (__run_exit_handlers in the backtrace). Mostly involving std::map of one class, guessing that's just the first thing that comes up though.

What does the corrupt data look like? Zeros? Ascii? Patterns?

I haven't found any patterns yet, seems somewhat random to me. It's hard to tell since I don't know where the corruption started.

Is it heap-related?

It's entirely heap-related (I enabled gcc's stack guard and that didn't catch anything).

Does the corruption happen after a free()?

You're going to have to elaborate a bit on that one.
Do you mean having pointers of already free'd objects lying around?
I'm setting every reference to null once the object gets destroyed, so unless I missed something somewhere, no. That should show up in valgrind though which it didn't.

Is there something distinctive about the network traffic (buffer size, recovery cycle)?

The network traffic consists of raw data. So char arrays, (u)intX_t or packed (to remove padding) structs for more complex things, each packet has a header consisting of an id and the packet size itself which is validated against the expected size.
They are around 10-60bytes with the biggest (internal 'bootup' packet, fired once at startup) having a size of a few Mb.

Lots and lots of production asserts. Crash early and predictably before the damage propagates.

I once had a crash related to std::map corruption, each entity has a map of it's "view", each entity that can see it and vice versa is in that.
I added a 200byte buffer in front and after, filled it with 0x33 and checked it before each access. The corruption just magically vanished, I must've moved something around which made it corrupt something else.

Strategic logging, so you know accurately what was happening just before. Add to the logging as you get closer to an answer.

It works.. to an extend.

In desperation, can you save state and auto-restart? I can think of a few pieces of production software that do that.

I somewhat do that. The software consists of a main "cache" process and some other worker ones which all access the cache to get and save stuff. So per crash I don't lose much progress, it still disconnects all the users and so on, it's definitely not a solution.

Concurrency: threading, race conditions, etc

There's a mysql thread to do "async" queries, that's all untouched though and only shares information to the database class via functions with all lock.

Interrupts

There's an interrupt timer to prevent it from locking up that just aborts if it didn't complete a cycle for 30 seconds, that code should be safe though:

```<code>if (!tics) {
    abort();
} else
    tics = 0;
</code>```

tics is volatile int tics = 0; which is increased each time a cycle is completed. Old code too.

events/callbacks/exceptions: corrupting state or the stack unpredictably

Lots of callbacks are being used (async network I/O, timers), but they shouldn't do anything bad.

Unusual data: unusual input data/timing/state

I've had a few edge cases related to that. Disconnecting a socket while packets are still being processed resulted in accessing a nullptr and such, but those have been easy to spot so far since every reference gets cleaned up right after telling the class itself it's done. (Destruction itself is handled by a loop deleting all the destroyed objects each cycle)

Dependency on an asynchronous external process.

Care to elaborate? This is somewhat the case, the cache process mentioned above.
Only thing I could imagine off the top of my head would be it not finishing quick enough and using garbage data, but that's not the case since that's using network too. Same packet model.

答:
Everything that was said in the other answers is very relevant. One important thing partially mentioned by ddyer is that wrapping malloc/free has benefits. He mentions a few but I would like to add a very important debugging tool to that: you can log every malloc/free into an external file along with a few lines of callstack (or the full callstack if you care). If you are careful, you can easily make this quite fast and use it in production if it comes to it.

From what you describe, my personal guess is that you might be keeping a reference to a pointer somewhere to freed memory and might end up freeing a pointer that no longer belongs to you or writing to it. If you can infer a size range to monitor with the above technique, you should be able to narrow down the logging considerably. Otherwise, once you find what memory has been corrupted, you can figure out the malloc/free pattern that led to it quite easily from the logs.

An important note is that as you mention, changing the memory layout might hide the issue. It is thus very important that your logging makes no allocations (if you can!) or as few as possible. This will help reproducibility if it is memory related. It will also help if it is as fast as possible if the issue is multi-threading related.

It is also important that you trap allocations from 3rd party libraries so that you may log them properly as well. You never know where it might come from.

As a last alternative, you can also make a custom allocator where you allocate at least 2 page for every allocation and unmap them when you free (align the alloc to a page boundary, alloc a page before and mark it as not accessible or align the alloc at the end of a page and allocate a page after and mark is as not accessible). Make sure not to reuse those virtual memory addresses for new allocations for at least some time. This implies you will need to manage your virtual memory yourself (reserve it and use it as you'd like). Note that this will degrade your performance and might end up using significant amounts of virtual memory depending on how many allocations you feed it. To mitigate this it will help if you can run in 64bit and/or reduce the range of allocations that need this (based on size). Valgrind might very well already do this but it might be too slow for you to catch the issue with it. Doing this only for a few sizes or objects (if you know which, you can use the special allocator only for those objects) will ensure performance is minimally impacted.

Title: Debugging memory corruption


问:
classes/inheritance, how do I handle special cases that require more change
I often find myself in this situation where I have a base class that does a lot of graphics. There are, for example, three strings that are positioned top, middle, bottom of an element. Like a scale or something. Now for every type of scale I make a new class that inherits the base class and simple sets the three strings and everything is fine. But often there is ONE single case, where I need four strings.

Sure, I could just override the method and recalculate for four, BUT if the calculations are based on a lot of offsets etc I would always have to take a look at how the base class does it in the first place and then copy-paste the entire calculation and do it for one more string. This means that every time the base calculation changes, I would need to copy-paste again etc.

On the other hand I could write the base class so super flexible that every tiny step can be overridden and subclasses can do everything they want with ease. But this requires way more work on the base class for ONE special case out of 20.

So either it's more workload in the beginning or the risk of missing a copy paste.

Often I don't even know what will come and you can't write a baseclass that is all-flexible and no one has the time to do so. So overriding and copy-pasting seems the best way to do so, but it still seems way too risky.

How should I approach this? I am looking for solutions that are practical, not a schoolbook approach that is too time consuming in a real work life.

(I mainly work with Java)

Edit:
To get a bit away from my example, which is really just an example.
Let's say i have a class and 20 subclasses and they work. Now along comes a new subclass that needs ONE thing completely different and just can't use the base classes method. If i override i have to copy paste the entire method and adapt it. If i change anything about the method, then i will have to change 20 classes that are already working fine.

"Do it from the start" works with one method, but what if 10 or more methods could be the cause for trouble. I can't write the base class in a way that for every method a variable-method-class can be used. I could, but that's what i meant above with "extreme workload at the beginning"

答:
Another option might be to extract the algorithms in a class that implements an interface or abstract class. Then the calculation can then be very specific to the layout needed. For 19 of the cases pass In a reference to algorithm 1, for the other pass in a reference to algorithm 2. This allows your base class to remain lighter and still allow for the flexibility of having specifics.

Another plus is that if another layout comes along you can add another algorithm 3 class for that case. All while leaving the original base class without all the logic to determine which algorithm to use.

Also if there are methods that are shared you can have algorithm 2 extend algorithm 1 and just modify the methods that are needed.

I suppose theres also the option then of changing the algorithm class thats referenced at run time and swapping out how the layout gets generated, might be helpful.

Where do you decide what algorithm to pass in? Probably when the scale classes are instantiated.


问:
Are there too many parameters in this constructor?
Check this out:

```<code>public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\View\DesignInterface $design,
        \Magento\Framework\Registry $registry,
        \Magento\Store\Model\App\Emulation $appEmulation,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Newsletter\Model\Template\Filter $filter,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Newsletter\Model\TemplateFactory $templateFactory,
        \Magento\Framework\Filter\FilterManager $filterManager,
        array $data = []
    ) {
        parent::__construct($context, $design, $registry, $appEmulation, $storeManager, $data);
        $this->_storeManager = $storeManager;
        $this->_request = $request;
        $this->_filter = $filter;
        $this->_scopeConfig = $scopeConfig;
        $this->_templateFactory = $templateFactory;
        $this->_filterManager = $filterManager;
    }
</code>```

It's from https://github.com/magento/magento2/blob/develop/app/code/Magento/Newsletter/Model/Template.php#L107

The function declaration takes more space than the body.

I think it has something to do with dependency injection.

What's the benefit of structuring your code as above instead of something like this:

```<code>public function __construct(Magento $magento, array $data = []
    ) {
        parent::__construct($magento->context, $magento->design, $magento->registry, $magento->appEmulation, $magento->storeManager, $data);
        $this->_storeManager = $magento->storeManager;
        $this->_request = $magento->request;
        $this->_filter = $magento->filter;
        $this->_scopeConfig = $magento->scopeConfig;
        $this->_templateFactory = $magento->templateFactory;
        $this->_filterManager = $magento->filterManager;
    }
</code>```

Notice that I only need to create the Magento instance once. Then I pass it to all the classes that need stuff.

答:
Having a base class with 6 dependencies is pushing it but acceptable under some circumstances especially if it's rare to have to extend the class.  The PHP Symfony 2 framework has a security class with a bunch of dependencies.  Extending it is painful but you only need to do so when implementing a completely new authentication system.

On the other hand, the Magento example looks more like a constroller class and one assumes that extending it is quite frequent.  Symfony 2 also has a base controller class with useful methods like getUser, createForm, render etc.  The base class would take about 8 dependencies in it's constructor.  It would be very painful and very fragile if a developer had to support several dozen controller classes in their application and keep track of all these dependencies.  There would also be performance issues as all the these dependencies would need to be created (or at least proxies) for each controller.

So Symfony 2 does something that I think works well.  It injects the dependency injection container and uses it to access these helper services as needed.  At the same time, the constructor is reserved for controller specific services.  So you have something like:

```<code>class PersonController extends BaseController
{
    private $container; // Actually in BaseController
    private $personRepository;

    // Also in base controller
    public function setContainer($container) { $this->container = $container; }

    // Nice easy constructor
    public function __construct($personRepository)
    {
        $this->personRepository;
    }

    // An action method
    public function showPersonAction($request)
    {
        $person = $this->personRepository->find($request->get('id'));

        // render uses a templating service from the container
        return $this->render(['person' => $person],'person.template.html');
    }
}
$controller = new PersonController($container->get('person_repository');
$controller->setContainer($container);
$response = $controller->showPersonAction($request);
</code>```

I think this is a good compromise.  No large constructor.  Easy to extend and read.  You do need to remember to inject the controller but this is usually handled by the front controller.

Body:
Having a base class with 6 dependencies is pushing it but acceptable under some circumstances especially if it's rare to have to extend the class.  The PHP Symfony 2 framework has a security class with a bunch of dependencies.  Extending it is painful but you only need to do so when implementing a completely new authentication system.

On the other hand, the Magento example looks more like a constroller class and one assumes that extending it is quite frequent.  Symfony 2 also has a base controller class with useful methods like getUser, createForm, render etc.  The base class would take about 8 dependencies in it's constructor.  It would be very painful and very fragile if a developer had to support several dozen controller classes in their application and keep track of all these dependencies.  There would also be performance issues as all the these dependencies would need to be created (or at least proxies) for each controller.

So Symfony 2 does something that I think works well.  It injects the dependency injection container and uses it to access these helper services as needed.  At the same time, the constructor is reserved for controller specific services.  So you have something like:

class PersonController extends BaseController
{
    private $container; // Actually in BaseController
    private $personRepository;

    // Also in base controller
    public function setContainer($container) { $this->container = $container; }

    // Nice easy constructor
    public function __construct($personRepository)
    {
        $this->personRepository;
    }

    // An action method
    public function showPersonAction($request)
    {
        $person = $this->personRepository->find($request->get('id'));

        // render uses a templating service from the container
        return $this->render(['person' => $person],'person.template.html');
    }
}
$controller = new PersonController($container->get('person_repository');
$controller->setContainer($container);
$response = $controller->showPersonAction($request);

I think this is a good compromise.  No large constructor.  Easy to extend and read.  You do need to remember to inject the controller but this is usually handled by the front controller.


问:
as a team: multiple projects vs single project?
I've been wondering for a while. I guess I have been adapting to the circumstances but I am in desperate need of some clearly defined workflows.

Ok, so say I am working with one other person and I have 2 projects come up both of equally large size with LOOSE deadlines. What should I do:

a) turn down one, work on other
  exclusively

b) take both on and develop both
  simultaneously

c) assign one person to each project

I have done c in the past which works fine. As a manager also, I've simply had to manage that project while I work on another. No big deal. But what is the ideal option above?

I'd hate to do a because I am turning down work either though deadline would permit working on both projects.

Any input?

Following things should be taken into account:

-) all members have same expertise in design & development and can
  effectively function as freelancers

-) timezones may not be the same so they would not be working at the same
  time

答:
Like John said ... this is a matter of preference. If it were me I'd go after both projects because of the loose deadlines, but then again, at the time of this post, I'm in need of extra cash to do some foundation repair on my house so if I have to bust my hump on 2 projects (I.E. working a healthy amount of overtime) then I'll do it for the much needed cash. Sure juggling two projects is a headache but it's worth it in terms of profit and future business.

This can also be a business building opportunity. If you get both projects and work overtime to get both of them done on time and budget, you'll impress the clients which could lead to more business and then you can hire extra devs to handle the increased workload.

Once again, this is a matter of choice but if I were a small business I'd get input from the developers asking if they would mind putting in overtime (for some extra pay) and cranking out these two jobs in an attempt to further the business.


问:
Where is the entry point for ASP.NET MVC's HttpContext?
I've got some custom FormsAuthentication with a custom AuthorizeAttribute where I'm maintaining the cookie manually, but I'm doing it within the filter's context.

What I want to do is move this to the very beginning entry point, at the very beginning of the request's life, and only do it once, and not rely on an attribute kicking it off.

But, I don't know where that would be. MVC's page lifecycle is very different than the WebForms lifecycle, so its not as cut and dry as subscribing to one of WebForm's page lifecycle events.

So in ASP.NET MVC, where does the HttpContext begin?

答:
Update:

With more digging into this I came across Cephas Lin's excellent article and PDF of ASP.NET MVC 5's lifecycle.

The PDF contains two images that clearly portray the flow of the MVC 5's lifecycle.
The first gives a 10,000 foot perspective of it all. The second image is very detailed and much too large to share here.

The first image:

My workaround:

After doing some digging I've pieced together somewhat of a workaround. Two Authorization filters that are executed in different orders.

MVC filters have an order of operation based on Eranga's answer to this question:
https://stackoverflow.com/questions/6561883/in-what-order-are-filters-executed-in-asp-net-mvc

Eranga wrote:

Filters run in the following order:

Authorization filters

Action filters

Response filters

Exception filters

For example, authorization filters run first and exception
  filters run last. Within each filter type, the Order value specifies
  the run order. Within each filter type and order, the Scope
  enumeration value specifies the order for filters. This enumeration
  defines the following filter scope values (in the order in which they
  run):

First

Global

Controller

Action

Last

Extracted from MSDN

With this knowledge, I've implemented a second Authorization attribute LoadAuthCookie which handles loading the cookie, fetching the current user's relevant role/permission data into a custom IPrincipal and IIdentity, and assigning the current HttpContext's user to this IPrincipal.

The LoadAuthCookie attribute only loads the cookie data if it exists and does not handle redirection or any other Authorization logic, and is registered within RegisterGlobalFilters.

Starting from Global.asax:

```<code>public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        /* other global stuff */
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        /* other global stuff */
    }
}
</code>```

And then

```<code>public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new LoadAuthCookie());
}
</code>```

This registers LoadAuthCookie attribute not only before, but also at a higher order of operation than the other Authorization attributes.

Then my other Authorization attribute, CustomAuthorize, can be applied at the Controller/Action level, and always fires after the auth cookie has been loaded, so has access to all of the nice things in the custom IPrincipal.

```<code>[CustomAuthorize(Role=Role.Admin)]
public class AdminController : ControllerBase
{
    public ActionResult Index()
    {
        return View();
    }
}
</code>```

However this probably isn't ideal, and a custom Authentication filter will be the place to load the auth cookie, instead of a custom Authorization filter. Authentication happens before Authorization.

Body:
Update:

With more digging into this I came across Cephas Lin's excellent article and PDF of ASP.NET MVC 5's lifecycle.

The PDF contains two images that clearly portray the flow of the MVC 5's lifecycle.
The first gives a 10,000 foot perspective of it all. The second image is very detailed and much too large to share here.

The first image:

My workaround:

After doing some digging I've pieced together somewhat of a workaround. Two Authorization filters that are executed in different orders.

MVC filters have an order of operation based on Eranga's answer to this question:
https://stackoverflow.com/questions/6561883/in-what-order-are-filters-executed-in-asp-net-mvc

Eranga wrote:

Filters run in the following order:

Authorization filters

Action filters

Response filters

Exception filters

For example, authorization filters run first and exception
  filters run last. Within each filter type, the Order value specifies
  the run order. Within each filter type and order, the Scope
  enumeration value specifies the order for filters. This enumeration
  defines the following filter scope values (in the order in which they
  run):

First

Global

Controller

Action

Last

Extracted from MSDN

With this knowledge, I've implemented a second Authorization attribute LoadAuthCookie which handles loading the cookie, fetching the current user's relevant role/permission data into a custom IPrincipal and IIdentity, and assigning the current HttpContext's user to this IPrincipal.

The LoadAuthCookie attribute only loads the cookie data if it exists and does not handle redirection or any other Authorization logic, and is registered within RegisterGlobalFilters.

Starting from Global.asax:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        /* other global stuff */
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        /* other global stuff */
    }
}

And then

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new LoadAuthCookie());
}

This registers LoadAuthCookie attribute not only before, but also at a higher order of operation than the other Authorization attributes.

Then my other Authorization attribute, CustomAuthorize, can be applied at the Controller/Action level, and always fires after the auth cookie has been loaded, so has access to all of the nice things in the custom IPrincipal.

[CustomAuthorize(Role=Role.Admin)]
public class AdminController : ControllerBase
{
    public ActionResult Index()
    {
        return View();
    }
}

However this probably isn't ideal, and a custom Authentication filter will be the place to load the auth cookie, instead of a custom Authorization filter. Authentication happens before Authorization.


问:
Is it ok to break the "is a" relationship if I just want part of a class' functionality?
I asked a question about extending TreeMaps into "sort by value" TreeMaps on the "code review" site on stackoverflow. Based on the response, I re-wrote the code. I like it. But, it feels like I might be just using tricks. Or, maybe this is a legit way to write software? Can someone please take a look:

```<code>public class ToolBox {
    public interface SortedMapByValue {
        void put(Object key, Integer val);
        Set<Map.Entry<Object, Integer>> entrySet();
    }

    public static SortedMapByValue getInstanceSortedMapByValue() {
        class MyComp implements Comparator {
            Map<Object, Integer> sharedMap;

            public int compare(Object key1, Object key2) {
                if(key1.equals(key2)) { return 0; }

                Integer val1 = sharedMap.get(key1);
                Integer val2 = sharedMap.get(key2);

                if(val1 > val2) return -1;
                return 1;
            }
        }

        class MyMap<K> extends TreeMap<K, Integer> {
            Map<Object, Integer> sharedMap = new HashMap<Object, Integer>();
            MyMap(Comparator comp) {
                super(comp);
            }

            @Override
            public Integer put(K key, Integer val) {
                if(sharedMap.containsKey(key)) {
                    super.remove(key);
                    val += sharedMap.get(key);
                }
                sharedMap.put(key, val);
                super.put(key, val);
                return val;
            }
        }

        class TreeMapByValue implements SortedMapByValue {
            private MyMap<Object> realMap;

            public TreeMapByValue(MyMap myMap) { this.realMap = myMap; }

            public void put(Object key, Integer val) {
                realMap.put(key, val);
            }

            public Set<Map.Entry<Object, Integer>> entrySet() {
                return realMap.entrySet();
            }
        }

        MyComp myComp = new MyComp();
        MyMap<Object> myMap = new MyMap(myComp);
        myComp.sharedMap = myMap.sharedMap;

        SortedMapByValue treeMapByValue = new TreeMapByValue(myMap);
        return treeMapByValue;
    }
}
</code>```

The question is just whether it is ok to write classes that are fragile and that break super class' apis, as long as you wrap them in a static method? The TreeMapByValue does not break the "is a" relationship with TreeMap only because I hide it. That seems like a trick, but maybe that is a standard way to program? I don't know.

btw: the title refers to my wanting the "red-black" sort algorithm of TreeMap, but I didn't want what the api says regarding sort ordering. I kept the algorithm, but broke the api. But then I hid doing this.

Please ignore my complete ignorance of generics. I will study them soon enough.

答:
MyMap only overrides a single method. The interface implementation does nothing besides delegating to the MyMap instance. You could easily just write this in the interface implementation.

```<code>class TreeMapByValue implements SortedMapByValue, Comparator {
    private Map<Object, Integer> sharedMap;
    private TreeMap<Object, Integer> tree;

    public TreeMapByValue() {
        sharedMap = new HashMap<Object, Integer>();
        tree = new TreeMap<Object, Integer>(this);
    }

    public void put(Object key, Integer val) {
        if(sharedMap.containsKey(key)) {
            tree.remove(key);
            val += sharedMap.get(key);
        }
        sharedMap.put(key, val);
        tree.put(key, val);
        return val;
    }

    public Set<Map.Entry<Object, Integer>> entrySet() {
        return realMap.entrySet();
    }

    public int compare(Object key1, Object key2) {
        if(key1.equals(key2)) { return 0; }

        Integer val1 = sharedMap.get(key1);
        Integer val2 = sharedMap.get(key2);

        if(val1 > val2) return -1;
        return 1;
    }
}
</code>```

Now you only have one class. There is no shared instance that needs to be explicitly assigned to a different class instance. There isn't a concern about how base class is being overridden. In addition, the factory method isn't needed any more since the initialization isn't multiple steps. If you don't want to expose compare(), you can move it into a private inner class.

sharedMap and tree are bad variable names. The names should provide a description of what they hold or why they are being used.

Be consistent with your if statements. Sometimes they are one-lined with curly brackets. Sometimes they are one-lined without curly brackets. My preference is to never one-line and always use curly brackets.

Body:
MyMap only overrides a single method. The interface implementation does nothing besides delegating to the MyMap instance. You could easily just write this in the interface implementation.

class TreeMapByValue implements SortedMapByValue, Comparator {
    private Map<Object, Integer> sharedMap;
    private TreeMap<Object, Integer> tree;

    public TreeMapByValue() {
        sharedMap = new HashMap<Object, Integer>();
        tree = new TreeMap<Object, Integer>(this);
    }

    public void put(Object key, Integer val) {
        if(sharedMap.containsKey(key)) {
            tree.remove(key);
            val += sharedMap.get(key);
        }
        sharedMap.put(key, val);
        tree.put(key, val);
        return val;
    }

    public Set<Map.Entry<Object, Integer>> entrySet() {
        return realMap.entrySet();
    }

    public int compare(Object key1, Object key2) {
        if(key1.equals(key2)) { return 0; }

        Integer val1 = sharedMap.get(key1);
        Integer val2 = sharedMap.get(key2);

        if(val1 > val2) return -1;
        return 1;
    }
}

Now you only have one class. There is no shared instance that needs to be explicitly assigned to a different class instance. There isn't a concern about how base class is being overridden. In addition, the factory method isn't needed any more since the initialization isn't multiple steps. If you don't want to expose compare(), you can move it into a private inner class.

sharedMap and tree are bad variable names. The names should provide a description of what they hold or why they are being used.

Be consistent with your if statements. Sometimes they are one-lined with curly brackets. Sometimes they are one-lined without curly brackets. My preference is to never one-line and always use curly brackets.


问:
Is a linked list an actual data structure?
I hear conflicting sides from this topic. I was under the impression that a linked list was a way to implement a data structure (stack, queue), not actually a data structure.</p>

答:
Alas! java.util.LinkedList is not a data structure by itself.

Does this mean that an array isn't a data structure either? or binary tree (because that's used to implement a SortedMap or a Heap)?

Ask yourself: "Is it data? is it structured?" if the answer to both of these is "yes", then, well, you have a data structure of some sort. Give the data structure Wikipedia page a read.

That a given structure is used to implement other things doesn't mean that it isn't a structure itself.

Arrays are used to implement flexible arrays.  Flexible arrays are used to implement the hash table part of a map (or dictionary if your language uses that jargon instead).  Linked lists may be thought of (kind of wrong, but bear with me) as specialized form of a tree (one branch only). Trees are used to implement... well, heaps, sorted maps, b trees (which are often found in indexes in databases and file systems).  The list can can go on and on and on (and that isn't a complete list).

Some of these things that I mentioned are fuzzy in their implementation. One can implement a Map with a hash table (backed by an array) - a HashMap, a red-black tree (a particular implementation of a balanced binary tree - just be glad its not an avl tree) as a SortedMap, or even linked lists as a ConcurrentSkipListMap. The key thing is how they behave - all of these implementations implement a set of the same methods guaranteeing they all have a get(K) and put(K, V) (along with some other things).

The linked list (concrete implementation) is a particular way of implementing a List (an abstract data type). You could also do it with a flexible array (another concrete implementation).

That one structure is used as the foundation for another structure does not make it less of a structure.  The key point in the data structure is that there is data, and it is structured in some way so you know where to look for its components.


问:
Asp.Net MVC from in modal bootstrap
First, some of the technologies in play: Asp.Net Mvc 5, Bootstrap 3, JQuery.

On my view I want to create a bootstrap model dialog, with a simple form in it.
When the user clicks submit, there are two outcomes.

1) Invalid input, should display error message from the server

2) OK input, redirect/refresh the same page, so the dialog is closed and pagedata is updated.

Since the modal dialog is shown using javascript, I guess I have to post to the server using JQuery ajax post? That seems to be the best way to keep the modal dialog visible, if there is an error?

I'm also unsure about where to place the post action. In the same controller or perhaps in a new web api controller (since this I'm doing a ajax post).

答:
Yes, you should use an ajax call to send the request and receive the response, using the response data to determine whether to display an error message or close the modal dialog.

As for where to place the action, while you certainly can create a separate WebAPI controller, and eventually you may decide to do so for architecture reasons, you can easily accomplish what you want by placing the action in your existing controller and designing it to return the appropriate JSON response.

Title: Asp.Net MVC from in modal bootstrap


问:
How to model state of leaving/entering home
I have two sensors: One detecting if the door is open or closed, another if movement has been detected in the corridor. I want to detect if the person has left or entered  home.
Usually the person has left if:

movement detected

door opened

door closed

no movement detected after door closed

Because of the timing problems and the overlapping states (door is open but person also moves) I think the design pattern state is not appropriate here. But I'm struggling to find a better structure.

Any help?

答:
The concept you are looking for is known as "Hysteresis" (Wikipedia). But don't waste too much time reading the article, as your situation is rather simple.

You have a number of inputs, which have states, and you also have a "logical state" (person in or out of home) which you need to compute out of them.  The "logical state" cannot simply be a direct function of the states of the inputs; instead, your software needs to have a concept of time, and it needs to be able to wait for a certain amount of time after a state transition of any of the inputs before computing its new logical state.


答:
You can use a variable with 3 possible states:

someone at home

nobody at home

indeterminate

These states are mutually exclusive, so it will make sense to use them for modeling. Whenever the door gets opened, set the state to "indeterminate". Whenever the door gets closed, start a timer, and when the timer ends, you change the state to "someone at home" or "nobody at home", depending on what the movement sensor told you in the measured time interval. It should be obvious that when the door gets opened again during the time interval, you have to stop the timer and keep the state "indeterminate". You could also consider to set the state to "someone at home" whenever there is movement inside the corridor, even when the door is not opened or closed.

This model takes care of the fact that a person at home might open the door, but does not leave, or vice versa comes from outside, opens the door, changes his mind and does not enter. It will also correct itself when you got the state wrong (maybe because the person left or entered not through the front door), and later measure some movement inside.

Of course, when there is more than one person at home, and only one leaves, this model will get the state wrong, but that is nothing which can be solved by a better model, only by more sensors ;-)

Title: How to model state of leaving/entering home

Body:
I have two sensors: One detecting if the door is open or closed, another if movement has been detected in the corridor. I want to detect if the person has left or entered  home.
Usually the person has left if:

movement detected

door opened

door closed

no movement detected after door closed

Because of the timing problems and the overlapping states (door is open but person also moves) I think the design pattern state is not appropriate here. But I'm struggling to find a better structure.

Any help?


问:
How to store data in memory(RAM) using php without using Memcache/Redis or any other software?
I want to implement a cache system in PHP consisting of following features

1) It should be able to store any(large or small) amount of data as long as the memory serves.(key value pair i.e. get & set)

2) It should have TTL

3) It should have LRU

4) Complexity should be O(1)

Constraints:
1) I am not allowed to use any middleware i.e. memcache or redis or any other in memory DB - so essentially implement memcache

Now problem here is

How can I interact with RAM using php?

PHP variables can be stored in memory but as soon as thread ends all variables are garbage collected. So you loose data?

May be use in memory mysql db engine? Can I?

Is it even possible to build such system using PHP?

答:
The easiest way I can think of to get access to a chunk of  memory from php would be via tmpfs.

Then you write a hash table  implementation (+your cache logic) in terms of operations on file handles. This might be fun as an exercise but if you do this on someone else's dime you are not doing your job properly imo.


答:
Your first example sounds like it could be solved by designing things always with "n elements" in mind instead of having individual derived classes for one, two and three strings. Having just one class where the list of strings is a parameter which can be of arbitrary length n will probably reduce some duplicate code in your already existing cases, and eliminiate the need for using inheritance at all.

Your second example is more abstract, so it is hard to give you any good advice without seeing the real case. When there are already working 20 subclasses, the fact you cannot easily add one more is often a code or design smell. Maybe the scope of your classes is too big, maybe there is too much inheritance involved, maybe there is something wrong with "case 21". Most of this problems can be solved by thinking twice about it (and sometimes the best option is to live with a pragmatic "duct tape" solution for case 21).

Title: classes/inheritance, how do I handle special cases that require more change

Body:
I often find myself in this situation where I have a base class that does a lot of graphics. There are, for example, three strings that are positioned top, middle, bottom of an element. Like a scale or something. Now for every type of scale I make a new class that inherits the base class and simple sets the three strings and everything is fine. But often there is ONE single case, where I need four strings.

Sure, I could just override the method and recalculate for four, BUT if the calculations are based on a lot of offsets etc I would always have to take a look at how the base class does it in the first place and then copy-paste the entire calculation and do it for one more string. This means that every time the base calculation changes, I would need to copy-paste again etc.

On the other hand I could write the base class so super flexible that every tiny step can be overridden and subclasses can do everything they want with ease. But this requires way more work on the base class for ONE special case out of 20.

So either it's more workload in the beginning or the risk of missing a copy paste.

Often I don't even know what will come and you can't write a baseclass that is all-flexible and no one has the time to do so. So overriding and copy-pasting seems the best way to do so, but it still seems way too risky.

How should I approach this? I am looking for solutions that are practical, not a schoolbook approach that is too time consuming in a real work life.

(I mainly work with Java)

Edit:
To get a bit away from my example, which is really just an example.
Let's say i have a class and 20 subclasses and they work. Now along comes a new subclass that needs ONE thing completely different and just can't use the base classes method. If i override i have to copy paste the entire method and adapt it. If i change anything about the method, then i will have to change 20 classes that are already working fine.

"Do it from the start" works with one method, but what if 10 or more methods could be the cause for trouble. I can't write the base class in a way that for every method a variable-method-class can be used. I could, but that's what i meant above with "extreme workload at the beginning"


问:
Concurrency: How do you approach the design and debug the implementation?
I've been developing concurrent systems for several years now, and I have a pretty good grasp on the subject despite my lack of formal training (i.e. no degree).  There's a few new languages that have become popular to at least talk about lately that are designed to make concurrency easier such as Erlang and Go.  It appears that their approach to concurrency echoes my own experience as to how to make systems scalable and take advantage of multiple cores/processors/machines.

However, I find that there are very few tools to help visualize what you intend to do, and verify that you are at least close to your original vision.  Debugging concurrent code can be a nightmare with languages that are not designed for concurrency (like C/C++, C#, Java, etc.).  In particular, it can be near impossible to recreate conditions that happen readily on one system in your development environment.

So, what are your approaches to designing a system to deal with concurrency and parallel processing?  Examples:

How do you figure out what can be made concurrent vs. what has to be sequential?

How do you reproduce error conditions and view what is happening as the application executes?

How do you visualize the interactions between the different concurrent parts of the application?

I have my own answers for some of these, but I'd also like to learn a bit more.

Edit

So far we have a lot of good input.  Many of the articles linked to are very good, and I've already read some of them.

My personal experience with concurrent programming leads me to believe you need a different mindset than you do with sequential programming.  The mental divide is probably as wide as the difference between object oriented programming and procedural programming.  I'd like this set of questions to focus more on the thought processes necessary (i.e. theory) to systematically approach the answers.  When providing more concrete answers, it helps to provide an example--something you went through personally.

Goal for the Bounty

Don't tell me what I should do.  I already have that under control.  Tell me what you do.  Tell me how you solve these problems.

答:
My answer for this questions are:

How do you figure out what can be
made concurrent vs. what has to be
sequential?

First i need to know why should i use concurrency, because i have find out that people gets exited with the idea behind concurrency but not always think about the problem they are trying to solve.

If you have to simulate a real life situation like queues, workflows, etc, you most likely will need to use a concurrent approach.

Now that i know that i should use it, its time to analyze the trade off, if you have lots of proccesses, you may think about communication overhead, but if you have to new, may end up with no concurrent solution ( reanalize problem if so.)

How do you reproduce error conditions
and view what is happening as the
application executes?

I'm no expert on this matter but i think that for concurrent systems this is not the correct approach. A theoretical approach should be choosen, looking for  the 4 deadlock requirements on critical areas:

Non preemptiveness

Hold and wait

Motual exclusion

Circular chain

How do you visualize the interactions
between the different concurrent
parts of the application?

I try to first identify who are the participants in the interactions, then how do they communicate and with whom. Finally, graphs and interaction diagrams help me visualize. My good old whiteboard can not be beaten by any other kind of media.


问:
Aggregate documentation comments from multiple scripts into README?
After having written some python scripts with comments for documentation inside, is it a good idea and possible to aggregate the documentation comments from multiple scripts into some standalone documentation file such as README?

Furthermore, is it possible to aggregate them in some markup format such as Markdown?

Thanks.

答:
I would highly recommend looking into Epydoc and also Sphinx-Doc.

Epydoc especially does exactly what you are asking for as stated on their website and as per my experience working with it:

Epydoc is a tool for generating API documentation for Python modules, based on their docstrings.


答:
State diagram with timing is great way to model your situation.

This is both understandable and easy to implement. The "movement detected" could be understood as transition from movement being detected to no movement being detected. You could make it better if you had "movement started", "movement ended" and "movement continues" transitions.


答:
It's not a PHP specific problem. Whenever you want to share memory with multiple processes (PHP scripts terminate after they finish), use Shared Memory.
In PHP, shm_attach() should get you started.

Implementing cache on top of shm should be straight forward.

Title: How to store data in memory(RAM) using php without using Memcache/Redis or any other software?

Body:
I want to implement a cache system in PHP consisting of following features

1) It should be able to store any(large or small) amount of data as long as the memory serves.(key value pair i.e. get & set)

2) It should have TTL

3) It should have LRU

4) Complexity should be O(1)

Constraints:
1) I am not allowed to use any middleware i.e. memcache or redis or any other in memory DB - so essentially implement memcache

Now problem here is

How can I interact with RAM using php?

PHP variables can be stored in memory but as soon as thread ends all variables are garbage collected. So you loose data?

May be use in memory mysql db engine? Can I?

Is it even possible to build such system using PHP?


问:
Are there any problems with implementing deleted usernames this way?
I have a requirement to implement usernames for an application for login purposes. The requirement specifies that the registry associated with the user/username in the database must not be deleted. The requirement specifies that the username once marked "deleted", must be available for reuse.

So, currently, the implementation changes a flag boolean "deleted" to true, so when login is triggered, the query verifies that the username exist and the boolean "deleted" is false.

Are there any problems with implementing deleted usernames this way? Are there other solutions that avoid these problems?

答:
I wouldn't implement deleting usernames in this fashion by using a boolean flag. You are correct that there are problems with implementing it this way; specifically, what happens when another user registers that username? Do you simply set the deleted flag back to true? What happens if that user then deletes the account? Your historical record has vanished!

Note that you have two requirements:

Provide username functionality for your application

Provide a historical record for username additions and deletions

It is not necessary to attempt to solve both problems in the same table! What you should do instead is archive the registration upon deletion, instead of having a boolean flag. This way you:

Don't clutter the active username table with lots of irrelevant data

Have a good system for when the same username is deleted over and over again (just have multiple entries in the archive table)

This solution should solve both of your implementation concerns.


问:
Can your staffs distibute your company non-distirbuted GPL licesned code?
Let say you use libmysqlclient to connect to MySQL, and since the client is GPL licensed and so your program. That's fine since you are the only one working on the project.

Then, you have a staff to join and contribute to the program, and he left. He own a copy of the program, can he freely distribute it because it is GPL lichened?

答:
There are a number of things to take into consideration:

In most jurisdictions, if you are hired to write code, or if you write code for your employer, then the copyright on the code you write is owned by your employer/the company that hired you.

As long as code is not made available outside the company that owns the copyright, it is not considered to have been distributed under copyright law.

If code isn't distributed, it doesn't matter what licenses are used on the code. Even if the combination of licenses is incompatible with each other. Having incompatible licenses just means that the resulting work can't legally be distributed.

So, if you are writing a closed-source program for company-internal use, that program can use GPL libraries and you still don't have the rights to the closed-source company code.


答:
Edit: To get a bit away from my example, which is really just an example. Let's say i have a class and 20 subclasses and they work. Now along comes a new subclass that needs ONE thing completely different and just can't use the base classes method. If i override i have to copy paste the entire method and adapt it. If i change anything about the method, then i will have to change 20 classes that are already working fine.

The simplest solution may be to rename the existing method and adapt it so that it handles the new special case correctly.

Then create a new method with the old name, which acts as a wrapper to the renamed function, and provides exactly the same interface as before.  That way all the existing code in other classes can carry on as before.

As an example, if your existing method takes three strings as parameters, but you need to handle four.  Re-name that method, and add an optional fourth string.  Create a new method with the old name that accepts three strings, as before, and which calls the re-named method with an extra blank string added on.


问:
When is it a good idea to force garbage collection?
So I was reading a question about forcing the C# garbage collector to run where almost every single answer is the same: you can do it, but you shouldn't - except for some very rare cases. Sadly, nobody there elaborates on what are such cases.

Can you tell me in what sort of scenario it is actually a good or reasonable idea to force garbage collection?

I'm not asking for C# specific cases but rather, all programing languages that have a garbage collector. I know that you can't force GC on all languages, like Java, but let's suppose you can.

答:
I have a usage for garbage disposal which is somewhat unorthodox.

There is this misguided practice which is unfortunately very prevalent in the C# world, of implementing object disposal using the ugly, clunky, inelegant, and error prone idiom known as IDisposable-disposing.  MSDN describes it in length, and lots of people swear by it, follow it religiously, spend hours upon hours discussing precisely how it should be done, etc.

(Please note that what I am calling ugly here is not the object disposal pattern itself; what I am calling ugly is the particular IDisposable.Dispose( bool disposing ) idiom.)

This idiom was invented because it is supposedly impossible to guarantee that your objects' destructor will always be invoked by the garbage collector to clean up resources, so people perform resource cleanup within IDisposable.Dispose(), and in case they forget, they also give it one more try from within the destructor.  You know, just in case.

But then your IDisposable.Dispose() might have both managed and unmanaged objects to clean up, but the managed ones cannot be cleaned up when IDisposable.Dispose() is invoked from within the destructor, because they have already been taken care of by the garbage collector at that point in time, so there is this need for a separate Dispose() method that accepts a bool disposing flag to know if both managed and unmanaged objects should be cleaned up, or only unmanaged ones.

Excuse me, but this is just insane.

I go by Einstein's axiom, which says that things should be as simple as possible, but not simpler.  Clearly, we cannot omit the cleaning up of resources, so the simplest possible solution has to include at least that.  The next simplest solution involves always disposing everything at the precise time that it is supposed to be disposed, without complicating things by relying on the destructor as an alternative fall back.

Now, strictly speaking, it is of course impossible to guarantee that no programmer will ever make the mistake of forgetting to invoke IDisposable.Dispose(), but what we can do is use the destructor to catch this mistake.  It is very simple, really: all the destructor has to do is generate a log entry if it detects that the disposed flag of the disposable object was never set to true. So, the use of the destructor is not an integral part of our disposal strategy, but it is our quality assurance mechanism. And since this is a debug-mode only test, we can place our entire destructor inside an #if DEBUG block, so we never incur any destruction penalty in a production environment. (The IDisposable.Dispose( bool disposing ) idiom prescribes that GC.SuppressFinalize() should be invoked precisely in order to lessen the overhead of finalization, but with my mechanism it is possible to completely avoid that overhead on the production environment.)

What it boils down to is the eternal hard error vs. soft error argument: the IDisposable.Dispose( bool disposing ) idiom is a soft error approach, and it represents an attempt to allow the programmer to forget to invoke Dispose() without the system failing, if possible.  The hard error approach says that the programmer must always make sure that Dispose() will be invoked. The penalty usually prescribed by the hard error approach in most cases is assertion failure, but for this particular case we make an exception and lessen the penalty to a simple issuance of an error log entry.

So, in order for this mechanism to work, the DEBUG version of our application must perform a full garbage disposal before quitting, so as to guarantee that all destructors will be invoked, and thus catch any IDisposable objects that we forgot to dispose.


答:
I know you don't want a "textbook" solution, but this is an ideal problem for the Strategy pattern.

http://en.wikipedia.org/wiki/Strategy_pattern

Implement behaviors for your different algorithms and use them in the classes that require them. This way if something new comes up, you won't have to change what you have, only add a new behavior.

If you don't want to make a big change in code, you can come up with a mixed solution:

Extract the code of your current overridable method into a different class (this will be a behavior), and used it there (this will leave your current method as a sort of wrapper).

Only override your method to call a different behavior.

This way you are making your code more readable, easier to maintain, your behaviors are reusable and are less likely to introduce bugs due to some code being uses somewhere else: you know where your behaviors are used.


问:
How do you deal with clients who cannot wait for a feature to exist?
I'm maintaining a legacy application that interacts with an ERP. The ERP has a lot of cool features. One goal of the legacy application is to store preparatory data to be sent to the ERP, but also keep the data up-to-date based on what happens in the ERP. A two-way synchronization.

Sadly, the legacy application does not implement all the features of the ERP for the bits it needs to synchronize. But that did/does not prevent the business from using all the ERP features and then demanding that the legacy application remain synchronized.

The differences are structural and not simple to implement in the legacy application. The business does not care about the issue because it's dealt with by the developers who have to cram data in a structure not meant for it, leading to many bugs and being forced to break data integrity rules in the DB (which impacts the business in return but in such indirect ways that they don't get it, no matter how many times it's explained). The business has better priorities.

In the given situation, would it be a moral and/or professional duty to prevent this from happening further to preserve what little data integrity is left and start going down a cleaner road, or should the business's decision be the end all be all?

In case stopping the madness is the right thing to do, how would one go about it?
Knowing that in the current context, it seems impossibly hard to say "no, the software cannot deal with this, stop trying to fit square pegs in round holes". The business is extremely frigid at the idea of developers spending time on making things structured and safe since it doesn't give them anything new (functionality-wise) to chew on. It's interpreted as wasted time basically.

答:
This is a classic "you're making bad decisions with your life that effect my life" situation.

You can (and probably should) surface the problem to business managers at every opportunity. Express how difficult further feature-adds are, and the additional cost/risk associated with continuing with the broken underpinnings. When features are requested that strictly cannot be added because of the problem, highlight those as a motivation to spend more energy and attention on the underpinnings.

But... There is no magic here. You cannot get people to want to eat only healthy foods, exercise frequently, or pay attention to the plumbing of their infrastructure if they uniformly refuse to take those requirement seriously. The best that you can do is keep the issue alive: raise awareness, explain the implications, and correlate the increased time/cost/risks they're facing because of the failure to improve underlying infrastructure. Look for allies and receptive ears. Individuals who are newly hired or appointed to influential positions are good candidates as change agents, if you can get them on-side. Lather, rinse, repeat.

At the end of the day, it is their system, their costs, their risks to manage. You can advocate, but technical staff are rarely in a position to make major invest/no-invest decisions. So keep fighting the good fight; keep working to sway those who can start to shift process and investment priorities to address this apparently large and rising tide of technical debt.


问:
Filling the gap between Conception and Unit Test
I am stuck trying to do TDD. I'd rather seek the counsel of others who have gone before rather than waste time with trial and error.

QUESTION:

What diagrams / exercises / development processes can I insert between "foggy notion of what system is supposed to do" (aka Conception) and "Write Test"?

Anything else included in this question is simply additional information to help communicate / clarify my question; not to change the question in any way. I do not care what diagrams / exercises / development processes I use, as long as they get me unstuck.

When I say Conception I just mean a foggy informal mental-only notion of what a system should / can / will / might do.

FORM OF ANSWER:

I'm trying to navigate from conception to Unit Test in my development process. I'm looking for a way, a path, a road map, a bridge. An answer might look something like this:

do a xyxyx type diagram to get the aaaas, then take all the
  aaaas and do a wywywyw diagram, then all the ccccs on the
  wywywyw diagram will be the classes you need to unit test and the
  lllllls will be the scenarios of the tests.

PROBLEM BACKGROUND:

I found a TDD flowchart and converted it to a Google Drawing. I'm using NUnit. I have studied TDD, know what it is and practiced it somewhat. This flow chart is for TDD only; no steps before Unit Testing are included.

The flowchart starts out with "Write Test". I had my concept, nothing I had read said I needed anything else first, so I tried following the flow chart. It was too much of a leap to go from "foggy notion" to Unit Test. This caused me "designer's block". Then I read someone say "just do it!" I tried that, but the resulting tests and ensuing code wandered somewhat aimlessly since there was nothing to guide what tests to write.

ATTEMPTS AT SOLUTION:

Some ideas I have explored already that seem helpful (but the question is not about these things):

BDD (I used SpecFlow)

use case diagrams

SpecFlow starts with a verbal description of a "feature" and creates tests that NUnit can run; they don't look like the unit tests I created manually but maybe I'll figure out how they relate to one another.

Use cases helped me get convert the initial foggy notion into a formal description, which was a bit of sunshine. I used Visual Studio 2013 for that. I don't see any facility (in Visual Studio at least) to convert my use case into Unit Tests, although I found some evidence that this is one purpose of a use case. Quoting from the UML User Guide, 2nd ed., p.246:

A use case diagram can be forward engineered to form tests for the
  element to which it applies.

And it looks like Visual Studio maybe wants to convert it because it can store template data on the use case and has "Code Generation Settings"

Here is a picture of what I have tried so far

答:
Writing use cases with (say a Product owner) could perhaps get you started. The problem for me is that "conception" in your graph and unit tests are on different conceptual levels. Unit tests are on a very low level (and are best viewed as a code design tool), they do not dictate or guarantee system level features.

I would start by writing automated high level acceptance tests (and you could do that in NUnit if you want to).

EDIT: I think it is hard to say exactly what you should replace ??? in the diagram. If the concept is defined and well know for the developer then she would be able to start writing the unit test directly. Ff the concept is not well defined then I would say "collaboration" (as in: you as a developer need to talk with someone go get more information so that you can write the tests and the code). Perhaps this could be formalized in "create Use cases" - since you need to gather information in order to accomplish that.

For me that kind of process should be separated from the TDD process so perhaps it's best to remove the ??? box - and simply treat the problem "I don't know how this feature works, so I can't build it?" as a different process.

OwnerDisplayName: toddmo

OwnerDisplayName: Jocke


问:
Suggestions for Programs to develop for learning
i wonder apart from those very simple hello world apps, what other programs should i try developing for learning. i am entering uni next year and wonder what kind of programs will be developed in that environment.

this is not really a language specific thing, but if you want to know what i use currently mainly

windows: C#/WPF - i studied java/vb etc in poly but i think C# is the way to go, maybe even F#

web: PHP/MySQL, Zend Framework/Doctrine, + HTML/CSS/JS(jQuery) of course. looking to try google app engine with python too.

for now, i am thinking of

todo lists

apps that integrate with api eg. twitter/tumblr which i use

text editor - i am currently trying to develop a text editor that uses markdown and generates html files for viewing (link to blog entry). not very pratical after i started developing it. cos when editing theres no formatting and in HTML format, i cannot edit directly

blog software (for web)

答: Have a look at Project Euler.  There's nothing else like it for sharpening your foundational programming skills.</p>


问:
Documenting intent Vs knowledge
It is said you should document the intent of your function or class which I agree with. However recently this line became a little bit blurred when I was wanting someone to document why this particular class is being created.

Specific example:

```<code>public class SMSException extends Exception{.....}
</code>```

In the above case there is no added code, each constructor is called with super(...)

To those familiar with throwing exceptions, creating a specific exception to throw makes perfect sense. (which is why we can throw exceptions like IllegalArgumentException, NumberFormatException etc... instead of just Exception)

Documenting the intent here seems overboard as the documentation will be the same as the class it is extending... But at times it seems appropriate to document this knowledge purely so that when new developers still finding their feet come across this can understand why this is done and not just wonder why people are doing this or what the advantage of doing this is.

So the question: Is it good practise to document that bit of knowledge here or should that knowledge remain in the lectures halls, training manuals, google, colleagues, etc... ?

答:
I would say documenting knowledge is much more prone to change (especially if you have an urge to document it) than documenting intent. So documenting intent is more effective if comparing the two.

Here is what I mean:

When you are talking about how something is done it has two downsides:

1) It is not clear how you are doing something and so you are feeling compelled to explain it which in and of itself might be pointing you to a bigger problem.

2) When you decide to refactor how you are doing something your knowledge more often than not will become obsolete.

On the other hand.

When documenting intent there is only one downside that at times can be excused.

1) Documenting intent shows that your code is not clear about its own intention and so you are feeling compelled to explain what it is doing. There are however times when it is okay to leave comments because you just have to solve it in a weird way and explain the intent.

However if you decide to refactor it later - you are not changing your intent you are only changing how your intention gets done. Hence your comment doesn't become obsolete.

I would recommend documenting things on a high-level from an architectural point of view by drawing diagrams (draw.io is an amazing tool that you can also integrate with JIRA if you are using it in your projects).

The reason it is better to document things from a high-level perspective is because details often times get refactored and your explanation constantly needs updates and amendments.

Once the architecture is understood the rest is a lot easier to get a grasp on along with figuring out the details.

Title: Documenting intent Vs knowledge


答:
I find it good practice to add a sentence or two to document the intended usage of the exception class.  For example, the documentation of java.lang.ArrayIndexOutOfBoundesException states:

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

Such a short explanation is quickly written and it greatly reduces the risk of your exception class being thrown inconsistently.  It also helps me to write correct catch blocks for your exception class.

For the delegating constructors, it should be pretty obvious what

```<code>public SMSException(String message);
</code>```

does so explicitly documenting this might not deserve the highest priority and could even lead to less readable code due to a lower signal / noise ratio.

Title: Documenting intent Vs knowledge

Body:
It is said you should document the intent of your function or class which I agree with. However recently this line became a little bit blurred when I was wanting someone to document why this particular class is being created.

Specific example:

```<code>public class SMSException extends Exception{.....}
</code>```

In the above case there is no added code, each constructor is called with super(...)

To those familiar with throwing exceptions, creating a specific exception to throw makes perfect sense. (which is why we can throw exceptions like IllegalArgumentException, NumberFormatException etc... instead of just Exception)

Documenting the intent here seems overboard as the documentation will be the same as the class it is extending... But at times it seems appropriate to document this knowledge purely so that when new developers still finding their feet come across this can understand why this is done and not just wonder why people are doing this or what the advantage of doing this is.

So the question: Is it good practise to document that bit of knowledge here or should that knowledge remain in the lectures halls, training manuals, google, colleagues, etc... ?

Body:
I find it good practice to add a sentence or two to document the intended usage of the exception class.  For example, the documentation of java.lang.ArrayIndexOutOfBoundesException states:

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

Such a short explanation is quickly written and it greatly reduces the risk of your exception class being thrown inconsistently.  It also helps me to write correct catch blocks for your exception class.

For the delegating constructors, it should be pretty obvious what

public SMSException(String message);

does so explicitly documenting this might not deserve the highest priority and could even lead to less readable code due to a lower signal / noise ratio.


问:
Efficient datastructure to create size-limited dictionary
I need a class that acts like a dictionary but will constrain the total number of key/value pairs it contains. For instance, let's say the maximum number of entries is 1000 and the class already contains 1000 key/value pairs. If I add an additional key/value pair, the class should remove the key-value pair that was updated least recently.

Here's my current implementation in python:

```<code>class SizeLimitedDefaultDict(defaultdict):
    last_changed = []

    def __init__(self, default, max_size, *args, **kwargs):
        max_size = 0
        super(SizeLimitedDict, self).__init__(default, *args, **kwargs)

    def __setitem__(self, key, val):
        if len(self) >= self.max_size:
            remove_oldest_entry()
        super.__setitem__(self, key, val)
        update_newest_entry(key)

    def update_newest_entry(self, key):
        key_index = last_changed.index(key) #will slow it down
        last_changed.insert(0, last_changed.pop(key_index))
</code>```

This clearly isn't a viable solution. All the performance gains of the dictionary are lost. I'm having trouble figuring out a better solution though. Is there a data structure that can easily maintain which keys have been most recently updated.

答:
Use a queue to manage the keys by recency:

```<code>from collections import deque

class SizeLimitedDefaultDict(defaultdict):
    last_changed = deque()

    def __setitem__(self, key, val):
        if len(self) >= self.max_size:
            del self[last_changed.popleft()]

        super.__setitem__(self, key, val)
        last_changed.append(key)
</code>```

Roughly speaking, when the dictionary gets "full", you'll pay for one dictionary lookup for every insert.

Body:
Use a queue to manage the keys by recency:

from collections import deque

class SizeLimitedDefaultDict(defaultdict):
    last_changed = deque()

    def __setitem__(self, key, val):
        if len(self) >= self.max_size:
            del self[last_changed.popleft()]

        super.__setitem__(self, key, val)
        last_changed.append(key)

Roughly speaking, when the dictionary gets "full", you'll pay for one dictionary lookup for every insert.


问:
Translation Lookaside buffer - Lookup By Page Size
I am having a hard time finding documentation that explains precisely how the various TLB caches are used in modern processors.
Most modern processors have separate TLBs for code/data. That in itself is fairly obvious since we know the type from the instruction that initiates the query.
However, most modern processors also have separate TLBs for different page sizes. How does the CPU know in which to look up for a particular virtual address?

From what I could gather, some Power processors offer a way to partition the address space with regions dedicated to certain page sizes (either hardcoded in the chip or presumably tweakable from a dedicated register). More recent AMD/Intel x64 processors do not appear to mention anything like this.

Does the CPU perform the lookup in all (4kb/2mb/1gb) TLBs in parallel? How would this work with L1/L2 TLBs? Is the TLB not associating the input virtual address to the final translation result and instead storing results from the Page Directory Entries/Page Table Entries/etc (which would be segmented in page size already)? Some Power processors have an ERAT cache for the input virtual address -> final translated physical address + protection attributes which seems to hint that the TLB stores PDE/PTE. Could someone elaborate a bit on this?

答:
After several hours of research I was able to find the following information:

Newer intel/amd processors that have separate TLBs per page size will lookup in both at the same time in hope that one will hit. When an L2 TLB cache is present, it will be searched afterwards before finally attempting a page table walk.

Older Power processors and ARM processors have a single TLB with mixed page sizes. This implies multiple comparisons in case of overlap.

Some processors have a cache above the TLB with fewer supported page granularities (ERAT for Power and microTLB for ARM).

Some processors have an internal page walking cache (ARM) to cache intermediate page walking data.

In all processors, the TLB stores the virtual address tag used for the lookup, the physical frame number and access information (r/w/e) and possibly a process ID and VM ID or similar to avoid invalidating the TLB on process/VM context switch.


问:
How can I define shared text entities in XHTML files?
I have an XHTML document that defines several text entities in its internal DTD. I want these entities to be available in other XHTML documents without having to cut and paste the internal DTD everywhere.

The answer that seems to keep coming up for other XML files (not specifically XHTML) -- for example, here: https://stackoverflow.com/questions/13567930/how-to-use-multiple-dtd-files-in-a-single-xul -- suggests that the XHTML file I want to write would look something like this:

```<code><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
          "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" [
<!ENTITY % mydtd SYSTEM "x.ent">
%mydtd;
]>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>Example</title>
</head>
<body>
<p>“&x; &y;”</p>
</body>
</html>
</code>```

... where the file x.ent has these contents:

```<code><!ENTITY x "Hello">
<!ENTITY y "world!">
</code>```

But no.  At any rate, it doesn't show what I expect in either of the browsers I need to work with.  I expect to see "Hello world!" enclosed in left-hand and right-hand double quotes.  But Firefox says “ is an undefined entity, and Opera shows the text between
 and
 verbatim (displaying ampersands instead of quotation marks).

I also tried inserting the public DTD within my internal DTD, so:

```<code><!DOCTYPE html [
<!ENTITY % publicdtd PUBLIC "-//W3C//DTD XHTML 1.1//EN"
          "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
%publicdtd;
<!ENTITY % mydtd SYSTEM "x.dtd">
%mydtd;
]>
</code>```

Then Firefox complained that &x; was an undefined entity.  Opera displayed the quotation marks I expected, but still displayed &x; and &y; verbatim; no hello, no world.

I thought x.ent didn't need an XML tag, but someone put one in their example of a file of entity definitions, so I tried it.  That didn't work either.

Is there some reason that this can't work in XHTML?  Or is this a defect in both of these browsers?  Or is there another way to share the entities among multiple files?  Unfortunately, I need something that works in these two browsers, or I might as well not do it at all.  But if it's a flaw in the XHTML 1.1 DTD, I can use a different version.

答:
The most plausible solution to this problem so far seems to be to use software
to generate the files that will actually be loaded by the browser.

For example, a page on Using Entities to Include Files
shows examples of entities defined in a file which is then included in a document
so that the document can use those entities.
Looking at the exercises in the lower half of the page, however,
there is an important step that was not part of the process I was following:

```<code>Produce example.html by normalizing example.xml.
% xmllint --dropdtd --noent example.xml > example.html
</code>```

In other words, while the file you write (example.xml) bears some syntactic resemblance
to the file that you load into your browser (example.html), it is not the same file.
The xmllint utility has resolved all the named entity references that you created,
producing a file that depends only on the public DTD.

Body:
The most plausible solution to this problem so far seems to be to use software
to generate the files that will actually be loaded by the browser.

For example, a page on Using Entities to Include Files
shows examples of entities defined in a file which is then included in a document
so that the document can use those entities.
Looking at the exercises in the lower half of the page, however,
there is an important step that was not part of the process I was following:

Produce example.html by normalizing example.xml.
% xmllint --dropdtd --noent example.xml > example.html

In other words, while the file you write (example.xml) bears some syntactic resemblance
to the file that you load into your browser (example.html), it is not the same file.
The xmllint utility has resolved all the named entity references that you created,
producing a file that depends only on the public DTD.


问:
Using NLP can we create a summary of functional specification
Just thinking that is it somehow possible by using Language processing APIs to extract a must have points or an intelligent summary from documents like functional specifications ..</p>

答:
Natural language processing is able to process human documents and do some interesting things, like judge the "sentiment" of the document or note similarities with other documents in a corpus. There have been a number of attempts to automagically summarize documents.

However, the quality of those summaries varies from "Wow! That was pretty good!" to "This is complete rubbish." Unfortunately, the curve is biased toward the less-good end of the spectrum. For applications where you have a truly overwhelming number of input documents, and where some amount of erroneous summarization is not a show-stopper, maybe. But with product specifications, you're talking about very technical documents for which missing a few points could be missing the entire point of the spec, and for which getting a few details wrong can mean not just a useless, but a counter-productive, summary.

NLP is best left to applications where errors and omissions carry lower risks.


问:
Laravel 5 Multi-App
So my work has a dashboard built in codeigniter where every app on the dashboard has its own subfolder in controllers / models / views and nothing is shared. I have been tasked with upgrading this to Laravel 5 and I would like to take this chance to do it the "right way."

I am curious how I would do this in Laravel 5. I would consider myself an intermediate Laravel user and would like to find a way to have each of the apps "seperated" somehow but also share common models / views / configs etc.

I can think of a few hacky ways off the top of my head but was interested to see what you guys think. Any ideas?

答:
Conceptually, it would be ideal to have a centralized API for each "system" (But not the same for all systems).

I use the term system, and quoted, because you may have many apps in a single system, as well as an app that works with more than one system. A nice example would be a WebApp and a Mobile Application, being two diferent "applications" that work over the same data. On the other side you may have a dashboard that controls your company's CRM as well as shows statistics for your website, which is basically one application interacting with two different "systems".

Having, for instance, website and sales data on the same database could result in big problems if you wanna change your website, as it may lead to issues with the sales data, and would be frowned upon by any sysadmin, as well as most if not all seasoned developers. Having two databases on the same application bring a whole new set of problems to deal with. And that is without considering possible permission issues and/or risk of an exploit or hack affecting "more sensitive data" than it should.

I would thus suggest you build a separate API for each system to have Models, Relationships and behaviors well separated and well-documented and working to your advantage. This means whenever there's a question or change in business logic, you have to look at it in one single place.

Over that, for each "app" I would have a separate codebase, but that is more of a personal opinion.

The idea now is that you must pull your data from the API, and after consuming the API you should only do formatting for presentation reasons. i.e.: If you have an eCommerce and have a cart total that is the sum of all products' values, this value comes from the API as FLOAT or DECIMAL, already making sure you have only 2 decimal digits, and the consuming part formats (i.e. adds currency etc.) the value.

This is to simplify possible different versions of apps, as well as ensure consistency (The cart total may never be wrong in one app but not the other, because it comes from the same API).

The main reason for the API is the consistency. Bigger codebases are bad for maintenance and performance reasons. Smaller codebases on the other hand may cause code to be duplicated, which may lead to consistency issues as well as weird bugs when one of the system changes their behavior.

Also, this decision comes BEFORE deciding the framework. Choosing a specific framework or language before deciding what you're trying to build carries a big risk of you making a bad decision. Consuming an API and working with presentation will be easier from any JavaScript front-end framework than from Laravel. Serving an API is probably way easier from Rails (Way better Models) or Node (Simplicity and performance, less stuff you never use lying around) than Laravel. Also, if you decide to consider using a different Database for whatever reasons (Be it NoSQL or Postgres or whatever), picking a framework before looking at your options may be a deal breaker.

All those decisions have to take one another in account. You need the most advantageous option you can feel comfortable working with. And while for a weekend project it usually means "whatever you want", for projects that may last months or years picking a different language or framework may have a big impact on the speed and quality of what you build, which is the reason for this being such a difficult and important decision.

Title: Laravel 5 Multi-App


问:
Representing a "Belongs to" relationship in an API endpoint
Consider the following pseudo code models:

```<code>class Post
    int Id
    string Title
    int CategoryId
    Category Category

class Category
    int Id
    string Name
</code>```

Note that Post belongs to Category. This is achieved via a CategoryId foreign key on Post. In the post model, there's also a Category object.

So my questions are:

1. On a GET request for a post, what would your JSON look like?

A - Just the foreign key (this one seems a little inefficient if the value for the Category name is commonly needed)

```<code>{
  Id: 1,
  Title: 'My Title',
  CategoryId: 1
}
</code>```

B - Just the nested object

```<code>{
  Id: 1,
  Title: 'My Title',
  Category: {
    Id: 1,
    Name: 'General'
  }
}
</code>```

C - Both (same as the actual model)

```<code>{
  Id: 1,
  Title: 'My Title',
  CategoryId: 1,
  Category: {
    Id: 1,
    Name: 'General'
  }
}
</code>```

2. Considering the above, what do you do for a PUT/POST?

I can't seem to find any concrete examples of the best practice to represent this case. Any thoughts, guidance or discussion welcome!

答:
I think you are starting from the wrong end. API design is about what the client/requester requires not about how you represent the data internally.

If the client need the category name then just provide it along with the id (if its useful to the client). As a Post belongs to only one category there is no reason to have anything other than a flat structure.