This paper develops a model of the market where the equilibrium lending mechanism, as well as the economy's aggregate investment and output, are endogenously determined. It focuses on two crucial elements. One is the micro theory of optimal lending mechanism. Instead of imposing a particular lending contract form exogenously, we solve for the optimal contract between a borrower and a lender designed to circumvent adverse-selection and moral-hazard problems in the model environment. The other important element is the effect of market condition on the lending mechanism. We embed the micro model of the two-agent contracting problem into a competitive market. Hence, we are able to study the interaction among market tightness, equilibrium financing mechanism and aggregate economic activity. On the optimal contract, the paper provides a formal theory that explains why some firms choose to borrow from banks, while others decide to issue bond to finance their investment. It postulates that the optimal contract is one of two kinds: either with intensive monitoring by the lender to overcome borrower's incentive problems, such as most of intermediated financing (bank or venture-capital financing), or with heavy reliance on the borrower, such as market financing. The model predicts that intermediated financing is optimal when investment returns are high, cost of lender monitoring is low, investment's liquidation value is low, and market is tight for the borrowers. On the general equilibrium effect, we show that the observation that bank lending falls relative to corporate bond issuance during recessions can be explained by movements in the economy's real factors, such as the decline in the average investment returns (which is considered as a contributing factor to the credit crunch occurred during 1990-91 recession), and paradoxically, the increase of investment demand which worsens market condition and hence intensifies the incentive problems. It can also be explained by the drop of supply, possibly brought about by a contractionary monetary policy in the short run.