0 Comments

Hello my english speaking visitors! As you maybe noticed, this blog is on croatian language, but just for your reading pleasure, this post is readable for more than 100 croatian speaking asp.net devsWinking smile

Now, back to the subject! Today is found nice link on www.asp.net home page, about using more than one model for MVC view, check it out here: http://honestillusion.com/blog/2013/11/11/Using-a-second-model-object-in-an-aspnet-mvc-view/

This will be my (short) take on this subject, offering yet another approach. We all know that MVC view can receive just one model type, that’s later available with @Model, and stuffing additional data to ViewBag is, or lets say, feels just wrong. And we’re to lazy to write a class that encapsulates all the types we need, as properties.

We can use (drum roll), yes, you are right my dear reader, TUPLE data structure! Someone may ask, what the heck are you talking about!? And you would give him some link like this one; http://www.dotnetperls.com/tuple or this one http://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx and he would see how Tuple is nice and useful data structure, present in .NET from version 4 (I think, or 3. Or maybe 3.5. I don’t know)

So, without further ado, let’s see some code. Like controller:

And the view:

I’ll not show you my model code, because those are just plain old classes. Nothing interesting there.

And that’s the whole science with this technique! Just encapsulate as many types you want inside Tuple data structure, specify Tuple signature in view, and you get all the intellisense beauty in your Razor view!

Downside of this approach would be, let me think, maybe this: you can’t really tell what’s Model.Item1 and Model.Item2 just by looking at it, if you send tuple with two items of the same class it’s hard to tell which one you need to use, you brake concept ‘one view one model’ (I just invented this concept, but you get the point), etc. So, use it wisely, because with great power comes great responsibility (think about poor junior dev who will need to maintain your app few years from now)!

0 Comments

Nekidan na Twitter feedu vidio cvrkut da je Infinium napravio Ruby utility za prikaz upozorenja o korištenju cookia na webu. I pomislih, srijeda je ionako najdepresivniji dan u tjednu, ajmo se zabavit i napraviti .NET port tog utilitya! Dva sata kasnije, source je bio na Gitubu, a nedugo zatim objavljen je i PHP port!

Prema europskom zakonu web site mora prikazati obavijest da se cookiji koriste za praćenje korisnika, što je lijepo objašnjeno na Infinium blogu.

Poanta ovog utilitya (class library dll) je omogućiti dodavanje obavijesti jednostavnim umetanjem jedne linije u vaš HTML kod ASP.NET MVC aplikacije (Razor sintaksa):

@Html.Raw(Consent.Instance.Install())

ili dodavanjem opcionalnih parametara:

@Html.Raw(Consent.Instance.Install(linkUrl:"/kolaciciinfo.html"
    messageText:"ovaj web site koristi kolačiće",
    okkText:"okej",
    learnMoreText:"želim znati više!"))

za prikaz se koristi jQuery  i jQuery cookies plugin. Instalacija je putem Nuget-a. Unutar biblioteke se nalaze lokalizacije za engleski i hrvatski jezik, iako je u source dodan i klingonski, ali kod kompajliranja potrebni resource dll-ovi se ne kopiraju u Bin/Release i Debug direktorije, pa se klingonska lokalizacija nažalost ne nalazi u Nuget paketu Tužni smiješak Ako netko zna zašto .NET ne kompajlira tlh-KX kulturu, bio bih jako zahvalan na toj informaciji!
Source sadrži i potrebne unit testove, a da bi mockali (podvalili lažni) HttpContext koji nije dostupan u unit test projektu, Consent klasa prima ICookieService interface preko kojega čita i piše kolačiće. Dependency injection za sirotinju, dakle.

Par savjeta oko izrade biblioteke i distribucije putem Nugeta:
-  ako dodajete neke txt, html, … datoteke u vaš class library, označite ih kao “Embedded Resource”. Primjer čitanja takvih datoteka možete pronaći u sourceu
- ako niste vični radu u komandnoj liniji i Gitu, možete koristiti GUI alate Github Windows i Sourcetree (ja koristim Sourcetree)
- ne šaljite DLL-ove u source kontrolu! Koristite Nuget package restore funkcionalnost, i dodajte Bin/ i Debug/ foldere u .gitignore listu. Isto vrijedi za sve ostale source kontrole.
- dodajte readme.md i opišite vašu biblioteku. Github koristi markdown sintaksu pisanja
- Nuspec datoteka je XML opis vašeg paketa kojega želite objaviti na Nugetu. Za kreiranje i objavu se može koristiti Nuget Package Explorer. Nuspec također držite u Githubu.
- izrada open source softwarea je najbolji način dobivanja (novog) posla i zapošljavanja: na interview-u ili na natječaj samo pošaljite vašu Github/bibucket stranicu uz standardni CV/Linkedin profil, i dobroj firmi odnosno poslodavcu to govori jako puno o vama kao programeru!

https://github.com/hudo/EUCookiesNET