Sunday, July 28, 2013

ASP.NET MVC Tip #34 – Dispose of Your DataContext (or Don’t) and Unit Test

http://msdn.microsoft.com/en-us/library/dd492699(v=vs.100).aspx

ASP.NET MVC Tip #34 – Dispose of Your DataContext (or Don’t)

In this tip, I demonstrate how you can dispose of a DataContext within an ASP.NET MVC controller. Next, I argue that there is no compelling reason to do this.
Several people have emailed me recently with the same question about whether or not it is important to call Dispose() on the DataContext object. In all of the code samples that I write, I create an instance of a DataContext object, but I never properly dispose of it. Is this wrong?
The DataContext class implements the IDisposable interface. In general, if a class implements the IDisposable interface, then that is good evidence that you should call Dispose(). But keep reading.
Classes that implement the IDisposable interface typically use resources that cannot be cleaned up by the .NET framework garbage collector. Calling the IDisposable.Dispose() method executes code that explicitly releases a precious resource back into the world.
A prime example of a class that implements the IDisposable interface is the SqlConnection class. A SqlConnection class uses a Microsoft SQL Server database connection. Because SQL Server supports a limited number of connections, it is important to release a connection as quickly as possible.
Typically, you do not call the Dispose() method directly. Typically, you take advantage of a Using statement in your code like this:
C# Code
using (var con = new SqlConnection(conString))
{
    var cmd = new SqlCommand("SELECT * FROM Products");
    var reader = cmd.ExecuteReader();
}
VB.NET Code
Using con = New SqlConnection(conString)
    Dim cmd = New SqlCommand("SELECT * FROM Products")
    Dim reader = cmd.ExecuteReader()
End Using
A Using statement calls the Dispose() method at the end of the Using block automatically. The Using statement calls the Dispose() method even if there was an error in the code.

Dispose of the DataContext

Because the DataContext implements IDisposable, it would seem like the right way to use the DataContext in a controller is like Listing 1. In Listing 1, a DataContext is used within a Using statement.
Listing 1 – BadController.cs
using System.Linq;
using System.Web.Mvc;
using MvcFakes;
using Tip34.Models;

namespace Tip34.Controllers
{
    public class BadController : Controller
    {
        private IDataContext _dataContext;

        public BadController()
            : this(new DataContextWrapper("dbcon", "~/Models/Movie.xml")){ }

        public BadController(IDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public ActionResult Index()
        {
            using (_dataContext)
            {
                var table = _dataContext.GetTable<Movie>();
                var movies = from m in table select m;
                return View("Index", movies);
            }
        }

    }
}
The controller in Listing 1 is taking advantage of the IDataContext, DataContextWrapper, and FakeDataContext objects discussed in the previous tip:
By taking advantage of these objects, you can easily test your MVC application.
Within the Index() method, the DataContext is used within a Using statement. The Using statement causes the Dispose() method to be called on the DataContext object (The DataContextWrapper.Dispose() method delegates to the DataContext.Dispose() method).
You can use the controller in Listing 1 with a typed view. The typed view casts the ViewData.Model property to an instance of IQueryable. The code-behind for the typed view is contained in Listing 2.
Listing 2 — ViewsBadIndex.aspx.cs
using System.Linq;
using System.Web.Mvc;
using Tip34.Models;

namespace Tip34.Views.Bad
{
    public partial class Index : ViewPage<IQueryable<Movie>>
    {
    }
}
Unfortunately, however, the code in Listing 1 does not work. If you try to execute this code, you get the error displayed in Figure 1. You receive the error: “Cannot access a disposed object”. The problem is that the DataContext has already been disposed before the DataContext can be used in the view.
Figure 1 – Error from early disposal
image
One of the big benefits of using LINQ to SQL is that it supports deferred execution. A LINQ to SQL statement is not actually executed against the database until you start iterating through the results. Because the movie records are not accessed until they are displayed in the Index view, the DataContext is disposed before the records are retrieved from the database and you get the error.
One easy way to fix the problem with the controller in Listing 1 is to pass the movies as a list instead of as an IQueryable. The modified Index() action method in Listing 3 does not cause an error:
Listing 3 – HomeController.cs
using System.Linq;
using System.Web.Mvc;
using MvcFakes;
using Tip34.Models;

namespace Tip34.Controllers
{
    public class HomeController : Controller
    {
        private IDataContext _dataContext;

        public HomeController()
            : this(new DataContextWrapper("dbcon", "~/Models/Movie.xml")){ }

        public HomeController(IDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public ActionResult Index()
        {
            using (_dataContext)
            {
                var table = _dataContext.GetTable<Movie>();
                var movies = from m in table select m;
                return View("Index", movies.ToList());
            }
        }

        public ActionResult Details(int id)
        {
            using (_dataContext)
            {
                var table = _dataContext.GetTable<Movie>();
                var movie = table.SingleOrDefault(m=>m.Id == id);
                return View("Details", movie);
            }
        }
    }
}
The only difference between the Index() method in Listing 3 and the Index() method in Listing 4 is that the ToList() method is called on the movies before the movies are passed to the view.
The controller in Listing 3 also contains a Details() action that displays details for a particular movie. You do not need to do anything special when retrieving a single database record. LINQ to SQL does not used deferred execution when you retrieve a single record.
If you change the typed view so that it expects a list instead of an IQueryable, then everything works fine:
using System.Collections.Generic;
using System.Web.Mvc;
using Tip34.Models;

namespace Tip34.Views.Home
{
    public partial class Index : ViewPage<List<Movie>>
    {
    }
}
The modified controller now works because the ToList() method forces the LINQ to SQL statement to be executed against the database within the controller and not within the view.
You can unit test the Home controller in Listing 3 just fine. The class in Listing 4 contains a unit test that verifies that the Index() method returns a set of database records.
Listing 4 — ControllersHomeControllerTest.cs
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcFakes;
using Tip34.Controllers;
using Tip34.Models;

namespace Tip34Tests.Controllers
{
    [TestClass]
    public class HomeControllerTest
    {
        [TestMethod]
        public void Index()
        {
            // Create Fake DataContext
            var context = new FakeDataContext();
            var table = context.GetTable<Movie>();
            table.InsertOnSubmit(new Movie(1, "Star Wars"));
            table.InsertOnSubmit(new Movie(2, "King Kong"));
            context.SubmitChanges();

            // Create Controller
            var controller = new HomeController(context);

            // Act
            ViewResult result = controller.Index() as ViewResult;

            // Assert
            var movies = (List<Movie>)result.ViewData.Model;
            Assert.AreEqual("Star Wars", movies[0].Title);
        }

    }
}
This unit test is taking advantage of the FakeDataContext class described in the previous tip to create an in-memory version of a DataContext. First, the unit test adds some fake database records to the FakeDataContext. Next, the Index() action is invoked and a result is returned. Finally, the ViewData contained in the result is compared against the fake database records. If the ViewData contains the first fake database record then the Index() action works correctly and success is achieved.

Don’t Dispose of the DataContext

In the previous section, I demonstrated how you can Dispose() of a DataContext within an MVC controller action. In this section, I question the necessity of doing this.
When you call the DataContext Dispose() method, the DataContext delegates the call to the SqlProvider class. The SqlProvider class does two things. First, the SqlProvider calls the ConnectionManager.DisposeConnection() method which closes any open database connection associated with the DataContext. Second, the SqlProvider sets several objects to null (including the ConnectionManager).
So, the most important consequence of calling the DataContext.Dispose() method is that any open connections associated with the DataContext get closed. This might seem really important, but it’s not. The reason that it is not important is that the DataContext class already manages its connections. By default, the DataContext class opens and closes a connection automatically.
The DataContext object translates a LINQ to SQL query into a standard SQL database query and executes it. The DataContext object opens a database connection right before executing the SQL database query and closes the connection right after the query is executed (the connection is closed in the Finally clause of a Try..Catch block).
If you prefer, you can manage the opening and closing of the database connection used by a DataContext yourself. You can use the DataContext.Connection property to explicitly open a connection and explicitly close the connection. Normally, however, you don’t do this. You let the DataContext take care of itself.
Most often, when you call the Dispose() method on the DataContext object, any database connection associated with the DataContext object is already closed. The DataContext object has closed the database connection right after executing the database query. So, the Dispose() method really doesn’t have anything to do.
Therefore, you really don’t get any huge benefits from calling Dispose() on the DataContext object. The only slight benefit is that the Dispose() method sets several objects to null so that they can be collected earlier by the garbage collector. Unless you are worried about every byte used by your application, or you are tracking a huge number of objects with your DataContext, I wouldn’t worry too much about the memory overhead of the DataContext object.

Summary

In this tip, I demonstrated how you can call the Dispose() method when working with a DataContext object within an MVC controller. Next, I argued that there really isn’t a very compelling reason to want to call the Dispose() method. I plan to continue to be lazy about disposing any DataContext objects that I used within my MVC controllers.

报税软件及网址

http://www.netfile.gc.ca/sftwr-eng.html

加拿大投诉雇主方法

被不公平/不公正的解雇
Complaint of Unjust Dismissal
如何申诉
http://www.labour.gc.ca/eng/standard...int_unjust.pdf
http://www.labour.gc.ca/eng/standard...t_unjust.shtml

被拖欠工资/无偿加班时间
Hours of Work
Pamphlet 9 - Labour Standards

如何申诉
http://www.labour.gc.ca/eng/standard.../pdf/hours.pdf
http://www.labour.gc.ca/eng/standard...st/hours.shtml[/quote]

Wednesday, July 17, 2013

Good shows

firefly, cheers, scrubs, new girl, suits, (sherlock), breaking bad. firefly, falling skies

Tuesday, July 16, 2013

resolution for FB.getLoginStatus() called before calling FB.init()

Short answer

Add &status=0 to your js.src URL to make the warning disappear, ie:
//connect.facebook.net/fr_FR/all.js#xfbml=1&status=0

Full answer

FB.init() will be called internally by the facebook script upon loading if parameters are provided after the hash (#) sign. Here xfbml is passed, so FB.init() is called.
(source code: http://connect.facebook.net/fr_CA/all/debug.js line 8699 at the time of this post)
The default args for init() are used if not explicitly provided: status arg default is true - which makes the FB script call getLoginStatus() at startup, which complains because an app ID is needed for that function call.
FB social plugins doesn't need an app ID - they are rendered into an iframe originating from facebook.com, so FB login status and cookies as accessible to them.
The "Get the Code" wizards in the FB developers Social Plug-ins section generates a URL with thexfbml param, it should be updated with the status=0 param IMHO.
share|edit

Thursday, July 11, 2013

.net classic open source project

http://orchardproject.net/
and nopcommerce

change query string parameters or change parameters in url by jquery

//to add or update parameters in query string by passing an array like
    //[{ name: 'q', value: search }, { name: 'sort', value: sort}]
    function addQueryParameter(options) {

        /*
        * queryParameters -> handles the query string parameters
        * queryString -> the query string without the fist '?' character
        * re -> the regular expression
        * m -> holds the string matching the regular expression
        */
        var queryParameters = {}, queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;

        // Creates a map with the query string parameters
        while (m = re.exec(queryString)) {
            queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
        }

        $.each(options, function (index, option) {
            queryParameters[option.name] = option.value;
        });

        //    // Add new parameters or update existing ones
        //    queryParameters['newParameter'] = 'new parameter';
        //    queryParameters['existingParameter'] = 'new value';

        /*
        * Replace the query portion of the URL.
        * jQuery.param() -> create a serialized representation of an array or
        *     object, suitable for use in a URL query string or Ajax request.
        */
        return $.param(queryParameters);

    }

learn how to create jquery plugins

http://learn.jquery.com/plugins/

Absinthe

Absinthe

Wednesday, July 10, 2013

安省Prince Edward County薰衣草农场地址、路线

http://blog.51.ca/u-286360/?p=573

@Olivia乌托邦小长假还去了Prince Edward County(PEC),不是东海岸的PEI哦。岛上有超美的薰衣草庄园,虽然很小但是很精致,五点关门要早去,下周末还有薰衣草节。路遇一望无际的油菜花田~最棒的是sandbanks沙滩,可以下水游泳了!@加拿大超级生活sUperLIFE

加拿大老房东亲授十年租房经验3


迄今为止的房东去业主与租客委员会控诉最多的问题就是欠租。"租户相当精明,当谈到他们为什么不支付租金时,他们说钱包被偷了,支票不能用了等等。"Corsetti建议业主坚持原则,不纵容租客的迟缴行为。 房东可以观察所有潜在租户,通过选择租客而减少违约风险。房东也可以联系租户的雇主或者以前的几个房东。不过要小心,因为有些住户只会写下他们朋友和家人的名字,希望你不会越挖越深。为了掩盖自己的坏租约纪录,有的人甚至使用伪造的信用评分和银行对账单影印本。"外面有许多专业的租客,"Mattina说 "一旦他们入住,他们就不付房租。驱逐他们或获得房租也变得十分困难,因为这些租客了解法律,并会竭尽所能留下来。" 如果碰上不支付租金的租户,你可以给他们发书面通知。如果是付第一个月的租金,多数省份的房东都可以依法发第二个通知。(虽然房东和承租人的法律在加拿大各省大致相同,但也请仔细阅读自己所在省的租赁法和业主和租客委员会的规定。)租客必须在发出通知的14天内支付全部房租。如果他们不交,房东可以提交三个星期后的听证申请。Mattina建议:"永远记得在每月的第一天发出通知,因为你通知的时间越延后,你得到房租就越晚。如果他们及时补交了租金,你当然也可以不再计较"。
安大略省的按摩师,同时也是房东的Whitney Wihidal就经历过租客拖欠租金和驱逐租客这些事。他与他的兄弟共同拥有一个有14个房间的公寓,该公寓2009年出售时几乎没有产生盈亏。"我总是要不停地追着一些拖欠房租的租客,每个月甚至会驱逐一个或两个租客。所以知道法律会使你的损失减少"。 即使一切顺利,驱逐一个租客也需要三个月到一年不等的时间。而房东再也不能得到申诉期间的租金收入。这就是为什么现在Wihidal选择出租离家比较近的地下室,因为这方便他掌握并控制事情的发展。Wihidal继续说 "我总是在当地的学校附近做广告,吸引学生租户,因为如果学生父母来看过房且每月付支票,那我就能百分之百得到房租"。
学习税收法规 有效地利用税法,最大化你的利益,并且知道那些项目应该出现在你的保税单上,比方说那些可能会将租金收入完全抵消的支出,通常包括广告费,修理和维护住宅的费用,为征收未付租金的法律费用。如果房东也住在出租的公寓里,那么应该扣除的租房费用将根据的租住空间占整套公寓的面积决定,通常房东和租客会共同分担保险费,抵押保险的利息,房产税,公用设施(水电暖网)和环境美化等费用。 一般来说,如果租房支出超过总租金收入,房东就亏损了。当然,你也可以用其他收入来弥补这方面的损失。举例来说,如果租金收入是10,000元,费用支出为4000元,然后6000元将是你税前个人所得的一部分,以40%的税率计算,你将支付2400加元的税款。相反,如果你的开支超过租金收入达6,000元,这一数额就必须从你的税前个人所得收入的其他来源,如你的薪水中扣除。此时,如果缴纳了收入剩余部分的税负后,你将获得2,400加元的退税。
有些费用可能不会在当年内被完全抵扣:他们可能要以规定的利率在今后几年内摊销。这些被称为资本开支,而将他们在几年内扣除的计算方式与折旧或资本成本津贴(CCA)的计算方式相同。资本开支的显着特点是它是一个持久的价值。 那些将房产恢复到最初状态所产生的费用,例如去掉墙壁绘画和粉刷的费用,应该在当年内扣除。另外,如果你在房产最初状态上有所改进的费用,比方说翻修浴室或换了一个新屋顶,可被视为资本支出,可以在三至五年内折旧。你的会计师可以帮你确定那些是当年扣除,哪些是多年折旧。 最后,不要忽视那些可能导致你未来收益损失成千上万的重要细节,比方说,你将本人目前居住地房子的部分租出所产生的税负。多伦多特许会计师John Mott解释说,"如果你出租了住房的一部分且该出租部分又可视为整体,如整个地下室或整个二楼,看起来你有两套住房,但征税时只会根据占空间的百分比或租用的时间征收部分的资本所得税"。
而且房东每年必须向税务局申报租金收入。如果你不这样做,税务员也能找到它,并要求你补交税款,同时你也会因逃避税收义务而受到严厉的惩罚。"租户们在保税单上会写下他们房东的名字以及每年的租金,以获得某些省级税收抵免和福利。"Mott继续说,"这些信息都可以被加拿大税务局交叉对比,如果发现了有偷税行为,他们会立刻发现并做出处罚"。 决定好何时退出 如果你有一个长期的财务目标,那么决定何时出售出租物业是很容易的。你是否想将出租收入作为退休收入来源的一部分?如果不是,那么你可以选择60岁左右时卖出出租物业,退出租房市场。安大略省哈密尔顿市的理财规划师Thomas Venner建议"如果你接近退休年龄,那么就赶紧利用现在良好的市场优势卖房,拿到利润作为晚年的养老金"。 或者你对你的租金收益有更直接的目标,就像 Gord Radman和他的妻子Rossana那样。八年前,他们夫妻俩在安省的伯灵顿购买了1300平方英尺的一套联排别墅,他们将它租出,该物业也一直呈正现金流。最近Radmans计划在5年内出售,拿到了20万加元。他们为什么这么做?当然是为了支付子女的大学教育费用!Gord说:"孩子的教育基金RESP将资助他们的教育,但我们知道教育基金还是不够. 出售连排别墅获得的收益可以弥补剩下的教育费用。与其得到一辆法拉利或购买更大的房子,这笔钱还是留给孩子们。这就是我们的长期目标,所以我们要坚持下去"。

加拿大老房东亲授十年租房经验2


找到最完美的物业 开始出租物业搜索时,注意寻找那些有良好工作机会和人口增长(可以保证有大量的租客可供选择)的城市。眼下,卡尔加里,埃德蒙顿,哈利法克斯和安大略省的巴里市都有很多机会。例如,巴里被认为是一个良好的租房市场,因为它的人口正在迅速增长,且很多移民三年后仍然租房,所以有大量的潜在租户。 同时请让各方面的专业人士协助你,包括资深的房地产律师,税务会计师和抵押贷款经纪人。阅读如何购买租赁物业的书籍或者向那些有当房东经验的家人和朋友取经。 当然,最适合出租的物业可能是你现在的住宅。Silverstein说:"你可以选择出租地下室,这样你的损失是有限的,并且你能很快知道租户的需求"。
你能负担多贵的房价? 根据你能支付的首付,就可以知道你能承受多贵的房价。加拿大小型的出租物业(1~4室)的最低首付是房价的20%,这样才达到加拿大按揭及房屋公司(CMHC)被保险人的贷款资格。如果你增加你的首付,是否意味着你能承受更贵的房价呢?请不要忘记房地产交易费用和其他必须履行的金融义务,比方说分期所购的新车,医疗费用或日托费用。请你将多余的现金先用于这些方面而不是付首付。 当然,一些投资者热衷于进入房地产市场,即使他们只能支付5%或者他们用信用卡预借现金来凑首付。请千万不这样做!"贷款人希望每个借款人都有进入房地产市场最基本的现金准备,"渥太华瑞安拉蒙塔涅公司的收费顾问Lamontagne说,"所以成功投资物业而首付却低于20%是非常罕见的。如果现金流为负,贷款人是不会轻易贷款,让自己被套牢的"。 对关键数字敏感 在买任何物业之前,先问问自己,即使现在加拿大许多地区的房价都处于7年的高点,你是否仍然可以赚钱。有不止一种方法可以衡量是否购买一个物业,但最理想的方法是确保你个人的现金流一直为正。另外,切忌不要将房价升值当作实际的投资回报,钱没到手一切都是浮云。 一旦你确定了你的首付,就应该考虑该住宅的各项费用开支以及你能得到的租金,并用每年的总租金收入减去所有费用(包括抵押贷款利息,房产税,保险和水电暖等费用)。将所有的信息都计入现金流量表中,若收入减去费用得到的数字为正则现金流为正(即你赚钱),相反为负则现金流为负(你赔钱)。 有些投资者认为即使每月损失点钱也无所谓,因为租户正在支付抵押贷款。但这种思路只会让你不断赔钱,因为你的费用包括了你无法控制且不断上升的抵押贷款利率,维修费用和租金的拖欠风险,这就可能花掉上千加元。"你可以把房屋出租当作经营小型企业,"Silverstein说,"正如一个小企业的现金流为负或正处于亏损不是一件好事,不管租户的租金是否足够支付抵押贷款,若出租物业的现金流为负就不是好事"。 另外一个棘手而高昂的花销就是那些不可预见的开支。为了减少风险,你应该将房价的2%作为房屋保养和维修费用的预算。所以如果你花了三十万加买房,你应该将6000元作为你的年度维修预算并增加至总开支中以得到更准确的现金流预测。 另外,你还需要存一点钱以备后患。亚利桑那州的物业经理Ross McCallister说"许多人最大的错误是没有意识到自己需要储蓄。许多房东将所有钱都用于消费和其他投资了,而当承租人拖欠了一个月或两三个月房租时,对家庭造成的经济和精神的双重影响是巨大的"。 多伦多的销售培训师Alen Majer就知道其中的艰辛。
Majer于2006年首次成为房东,出租了一套在安大略省米西索加600平方英尺一室一厅的公寓,该公寓购入价达165000加元。他说"我喜欢这个别人支付按揭贷款来购买个人物业的想法。刚开始一切正常,物业产生了正现金流,但它很快就变成了负的,因为公寓管理人员出人意料地将每月的维修费上调了15%至每月$ 428"。而当他的现金流开始由正转负时,他竟然没有任何可以应急的现金。 所以,在拥有该物业五年后,Majer把它卖了,税后所得48000元。 "基于25,000美元的初始投资,我们这五年的投资回报不错。但我们也见证了每月$ 50的损失可以迅速变成$ 100甚至$ 150。作为一名房东,你必须对现金流的正负保持警觉"。Majer说他已经接受教训并积极准备第二次当一套多伦多公寓的房东。"我已经用几千元设为我现金流量计算的金融安全气垫,即使抵押贷款利率上浮,我依然可以保持金融安全"。
掌握一定的法律知识 学习你所在省关于房东和租客的法律,并且做好事情会变坏的准备。从房东的角度出发,三个最常见的问题是租客不支付租金,长期的逾期付款,违规行为和房屋损害。每个人都可以提交申请到业主与租客委员会要求审理你的案件。多伦多律师助理Cathy Corsetti说:"我建议所有房东,要实事求是,不要感情用事。许多复杂的情况都是简单的问题不断恶化形成的"。

加拿大老房东亲授十年租房经验1


1957年,我的妈妈和爸爸从意大利来到加拿大,那时他们只有一个梦想,买一套属于自己的房子。但当他们利用了几个星期天下午到多伦多到处找房子时,他们才意识到自己微薄的积蓄根本就不够买房。某个周末,他们偶然发现一幢位于bustling Claremont街小小的有些破旧且急需修理的两层联排别墅在出售。该住宅的第一层有两间卧室和第二层有一间卧室。他们很快意识到,只要租出一间房间获得的收益就可以买这间小小的房子了。因此,在支付了很少的首付并开始长期还贷之后,他们租出了房间,成为了房东。
后来的10年里,他们遇到过各种各样的租户,包括一个社会关系很复杂的年轻护士,一个常开摩托车的木匠和一个奇怪的大学生,当他洗衣物时,他养的蜥蜴常常爬到他肩膀上。我爸妈最终还是卖掉了这套房子并获得了一点点收益,之后他们用这些钱在郊区买了一套单层小屋,而今天,他们依然当房东,继续走在实现他们的财务目标的道路上。
大多数想当房东的人都会从我父母故事得到启发。为什么不租房呢?加拿大的低利率和不断上涨房地产价格都鼓励加拿大人购房,然后出租地下室,第二套公寓,或那些作投资用途的小公寓楼。但残酷的事实是,一些小小的失误或许就能把你的房东美梦变成一场噩梦。"当朋友告诉我,他们已经买了一些可出租的物业,我总是说,'恭喜你,你刚刚为自己买了一份新的事业'"。安大略省房东和租客委员会的原审判员Deb Mattina说,"你应该把租房当成一份事业。"
而作一个成功的房东就意味着你首先要确保你可以获得收益,并且了解更多的游戏规则,包括业主和租户的法律。"良好的租户总是求之不易,"多伦多房地产律师Alan Silverstein在亲自经历了自己的房东美梦变成泡沫之后感叹道。"随着时间流逝,法律对业主的要求也越来越高。所以房主必须要积极地了解最新的法律"。

【挑选一条正确的道路】

这里有许多种成为房东的方法。你可以租出住房的一部分,如地下室的房间;或者你可以购买第二套住房然后整套租出,之后,你需要决定你是想与你的租户直接交涉,还是借助物业管理公司。如果你亲自与租户打交道,那么所有的租房收入都归你所有,不过,你会发现你也投入了很多的时间和精力来维护房产。
居住在温哥华的31岁小学辅导员Milo Wu以及他31岁的妻子Erica于2005年买了一间单层住宅。那时Wu刚读完大卫巴赫的书《主动成为百万富翁》,他下定决心将房地产成为他终身的储蓄罐。但短短三年后夫妻俩就把这套住宅卖了,原因是接二连三的房屋维修,既使他们在经济上惨遭损失又严重影响了他们的生活品质。"租房可能是一个定时炸弹,"Wu说。"如果你亲自和租客打交道,电话总是不停地响,租客总是抱怨水龙头漏水或厕所又堵塞了。我们最后只希望再也不用听租客抱怨了"。
这是一个血的教训吗?"当房东并不是一个坐享其成的投资,"房地产律师Silverstein说;"你需要不断改善它并使它能为你带来收益,如果你做不到,就不要成为房东"。
成为房东也可能让你的家庭关系紧张,总部在维多利亚市的付费顾问Lenore Davis解释道,"如果你打算成为房东,就必须与你的伴侣统一战线。并不是金融问题破坏了夫妻关系,而是情感方面的问题。老公总是质问,这就是你的解决方案吗?"他进一步分析说:"我让一些夫妇分别写下自己的长期财务目标。然后再一起讨论。通常20%的夫妇对成为房东持不同意见,那么他们最好再仔细协商或者考虑其他的投资选择"。
夫妇意见不合有很多原因。丈夫可能对肩负大量债务不满,或者妻子不希望自己的空闲时间都要用来解决租客的问题。不管是什么原因,在你和你的配偶准备实施租房计划之前,你们都必须先找到一个折中的大家都满意的办法。
当然,如果由物业管理公司来代表房东与租客协商租房,自然会减少大量的工作。你就不必再处理租户的问题或收取租金,但缺点是管理费是你房租收入的10%,如果加上维护成本,或许你的租金就所剩无几了。
另一种选择是专为了租房而购买一套公寓。这时,公寓的维修费承担了房屋的保养,房东的责任也相应地减少。这对Wu那样刚刚拥有可出租的公寓来说也很好,他说很多事都不用亲自动手。
一旦你找到适合的房屋,关键是长期持有该房产(至少10年)并设法增加财务上的盈利。"最终,房地产的盈利并不是由进入市场的时间,而是你在市场中的时间决定的。"多伦多的房地产经纪人Tom Karadza解释道。因此,建议你持有到你的出租物业直到还清所有贷款。

出租房屋是否合法


“以房养房”出租房屋是否合法?

加拿大中文在线  ccol.ca  发布者:[本站编辑]  来源:[转载]  
    合法出租,犹抱琵琶半遮面.
    一位来自四川的太太,因为一房多租被人告了,市政府给她来了一封信,通知她要派人来检查,她一时不知所措,打电话到编辑部来求救,询问我们应该怎么办。
    由于越来越多的中国移民加入“以房养房”的行列,我们遇到的此类咨询电话也越来越多。笔者曾经碰到这样一个例子,一个六口之家,一对夫妇,两个小孩,加上一对老人,超出自己能力买了一幢4000多尺的大屋用于自住加出租,他家将一层隔成3个睡房,外加客厅、厨房和洗手间;二层隔成5个睡房和3个洗手间;地下隔成3个睡房,外加洗衣房和一个洗手间。除了他们一家人,二楼又住了3个单身,一楼住了6个女孩,地下还住了两家。十几口人每天进进出出,有时还带朋友来,人来人往,车到处乱停,严重影响了邻里的正常生活。他家的邻居是西人,对此烦不胜烦,在多次警告无效后,一纸告到了市政府。市政府接到举报后,立即给他家来了一封信,要派Inspector来检查,检查人员一来,看到这么多人同居一室,明显违反规定。接下来,Fire Marshal也来了。人一多,火灾隐患就大了,消防局要来看看你家是否符合消防标准。再接下来,税务部门的人也来了。既然你出租,那就有收入,出租多长时间了,是否按时上税了…。当然,这些检查都不是免费的。
    这一通查下来,市政府给你两条路:一,一个月内清空所有住户,变成自住房,不再出租;二,限期进行整改,达到出租房的一切标准。最后,这家人考虑,要达到市政府的标准,还要投入一大笔钱,买这么个大房子,已经倾其所有了;再说这房子已经被市政府盯上了,今后肯定会经常来检查,只好选择不出租;但不出租,自己又养不起这么大的房子,于是只好卖掉了。
    合法出租有哪些标准
    我们来假设一下,如果这家人选择第二条路,按照市政府的要求进行整改,那他需要做哪些工作呢?
    对于自住房屋,根据市政府有关规定,你最多只能出租给一户人家,也就是所谓的“第二寓所”原则。“第二寓所”指的是设置在独立屋或半独立屋内的一套自给自足的出租房间。所谓自给自足,是指必须有单独的厨房和卫生间,必须分门出入,也就是说,必须是一个完全独立的单位。
    除此之外,市政府对“第二寓所”还有许多具体的规定,比如说,设置第二寓所的房屋必须至少有五年房龄,也就是说,五年以下的新房是不允许设立“第二寓所”的。之所以这样规定,是因为建筑商对新房一般都有7年的维修期,如果新房结构有什么问题,五年之内应该都表现出来了。还有就是,第二寓所的面积要小于自住部分的面积,设有第二寓所的房屋必须至少有两个停车位。
    另外, 第二寓所还必须符合安省的building code 及fire code的一些具体规定。比如每人最小生活空间不得少于9平方米或100平方英尺,壁橱和卫生间除外;室内天花板的最低高度应不少于6英尺5英寸;房门应该是实木或金属的,厚度不少于45mm;第二寓所必须有进口和出口两个通道,以防失火时一个通道被阻,还可以从另一个通道逃生,如果没有直接通向外面的出口,窗户必须至少有0.38m2大;每一层楼都需要安装烟雾报警器,卧室外必须安装一氧化碳报警器;第二寓所与房屋其它部分之间应有防火隔离,如30 分钟等级的防火墙等。虽然第二寓所可以设置在屋内的任何一层,但大多数都设置在地下室和阁楼上,一旦有火灾,撤离需要花费较长时间,所以,一般自住屋只需要15分钟等级的防火墙,但第二寓所防火墙的等级要求要高很多,必须达到30分种才能烧透的标准?
    我们上面讲到的那家人,由于是自住房,按规定只能有一套“第二寓所”用于出租。如果要出租给一户以上,那就不能用于自住,而是整幢房子纯粹用于出租。这种出租房,规定又不一样。买房出租,首先要考虑的是Zoning问题。Zoning分很多等级,从Single family到Dual Plex,Tri Plex,Four Plex,Five Plex等等。出租给两家,为Dual Plex ;出租给三家,为Tri Plex,依此类推。一般来说,Four Plex以下的,属于Residential;Four Plex以上的,就属于Commercial商业出租了。如果打算投资买房出租,首先要向政府申请Zoning。政府经过检查,觉得你买的这幢房从结构到面积都适合你申请的Zoning,才可以进行下一步,如消防检查等,全部检查合格,才能发给你准租证。
    侥幸心理不可有
    目前多伦多华人房屋出租市场,真正达到标准的并不多,存在很多“灰色地带”,大家都抱侥幸心理,抓不到算我走运,抓到算我倒霉。毕竟抓到的还是极少数嘛。问题在于,什么事都有可能发生,让你防不胜防。笔者曾接触到这样一个例子,某房东一屋多租,其中的一对小留学生情侣闹矛盾,男的拿水果刀把女方扎得满身是血,被人报警,结果警察一到,把一屋子人都带去询问,一来二去,全露馅了。百利置业地产经纪袁浩彬先生还讲了一个例子,他有一位熟人,把地下室用来出租,女租客和邻居西人老太太混得很熟。碰巧这位房东因为院子围墙的事和邻居老太太发生了纠纷,于是趁女租客邀她进屋坐坐的机会,将地下室的格局都画了图,然后举报到市政府。这位房东倒是限期进行了整改,达到了要求,不过是被迫的,据说后悔得直打自己的嘴,怎么就一时没管住,跟邻居吵起来了呢!
    哪些装修需要申请Permit
    上诉例子讲的都是出租以后被人举报。也有的房东在装修刚开始、装修过程中、或刚装修完就被查了。碰到这种情况,检查员一般都会先让你停止施工,然后去申请Permit。
    经常有读者问,到底哪些装修需要申请Permit?我换个地板,难道也要去申请许可?
    一般来说,如果你需要动房子的结构和水电气管道,那就需要申请装修许可。打个比方,如果你换瓦,不需要许可;如果你要动椽子,那就是动结构了,需要申请许可。同样,如果你要换Furnace、空调,不需要许可,因为你没动管道;如果你要加个卫生间、厨房,那就动结构了,需要申请许可。当然,并不是说你一旦动管道,都得申请许可,如果管子烂了,你换一截,或补一下,就不用申请许可。
    被查后怎么办?不用惊慌。这时候你还没有出租行为,所以检查官也不会假定你这个房子就是用来出租的。如果是在装修刚开始就被查了,你去跟检查官说明白,你要做哪些装修,他会告诉你哪些是需要申请许可的,许可下来后你就可以施工了。装修中或装修完被查,需要许可的,补办一个许可就可以了。但是,如果你加了一个厨房,那就比较麻烦了。原因在于,你加厨房,明显就是要把房子作为“第二寓所”用于出租,那你就得符合上面所说的关于“第二寓所”的所有规定,甚至包括墙体的厚度。这个标准就很难达到了。加几个卫生间都没事,想自己图方便嘛,我在地下室尿急了,等我跑到楼上,早尿裤子了。所以加卫生间不能断定你是要出租,但加厨房就是在为出租做准备。所以一般的做法,就是把新加的厨房拆掉,其它动了结构管道的地方,按一般房屋装修申请或补办许可就行了。当然,如果你就是要合法地变成“第二寓所”,那就不用拆厨房了,把所有的标准到达到就行了。
    “灰色地带”还要存在多久?
    在一个法制社会里,法律出台却不能贯彻执行,听起来似乎很不可思议。但我们也不能说这全是房东的觉悟问题,目前租房市场的现状与政府的容忍和默许也有很大关系。一旦有人举报,政府追究还是要追究,但力度不大,最多派人来检查,一般不会罚款,只是要求你限期整改,主要是为了住户的安全考虑。很多房东也就是利用了政府的暧昧态度,反正抓住了也没什么大不了的处罚,最多一个限期整改,愿意整就整,不愿意整,大不了不租了。
    政府也有政府的难处,街头已经有那么多无家可归的人了,一旦痛下杀手,真正规范出租市场,那么多低收入穷人怎么办?几十万人将流落街头啊,这些人怎么安置?政府还得再建多少庇护所?任何一个政府都不得不考虑这些社会现实。
    目前的房屋出租市场,就是这样一个两难的境界。
    不管政府的态度如何,有一点房东们必须心中有数:合法出租,对你及租客的安全,都是一种保障;即使今后要卖房,这也是一个绝好的卖点;最重要的是,依法办事,能让你有一份坦然的心境。(地产周刊/南音)

Changing URL parameters with jQuery

Changing URL parameters with jQuery

September 20th, 2011 by Samuel SantosLeave a reply »
2saves
You can find plenty of resources about this topic just by googling the web, most of which will point to jQuery plugins.
But the fact is that it’s so easy to achieve this by simply using jQuery that you do not need a plugin.
The code is pretty much self explanatory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * queryParameters -> handles the query string parameters
 * queryString -> the query string without the fist '?' character
 * re -> the regular expression
 * m -> holds the string matching the regular expression
 */
var queryParameters = {}, queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;
 
// Creates a map with the query string parameters
while (m = re.exec(queryString)) {
    queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
 
// Add new parameters or update existing ones
queryParameters['newParameter'] = 'new parameter';
queryParameters['existingParameter'] = 'new value';
 
/*
 * Replace the query portion of the URL.
 * jQuery.param() -> create a serialized representation of an array or
 *     object, suitable for use in a URL query string or Ajax request.
 */
location.search = $.param(queryParameters); // Causes page to reload
You can clearly improve the regular expression, but the one above meet my needs.