Showing posts with label flickr. Show all posts
Showing posts with label flickr. Show all posts

Friday, April 15, 2011

ASP.NET MVC 3 Sample Project Launched




Okay, after popular demand of I think 0 people, I’ve published my old demo MVC 1 project as an MVC 3 project. If you haven’t read any of the posts on the last demo project, check out this post. This project completely separates UI & C# code, so you only have 2 projects (Core & UI…not counting the Unit Tests project).

Basically, all I did was create a new MVC 3 project using Razor with Microsoft’s default template and deleted everything except the following:

  • Views folder
  • Root Default.aspx
  • global.asax (I did delete the global.asax.cs)
  • both web.configs

I referenced my Core, setup my views that match my Core project, and inherited from my global.cs in the global.asax. That was it!

You can download it via zip here: http://code.google.com/p/derans/downloads/list (the file is called DemoPhotographySite_v3.zip)

I feel like this project will act as a great stepping stone to understanding the Code Camp server, which is much more complex.

The sample project was built with the following tools:

If you downloaded the old one, you’ll notice that I removed the following 3rd parties:

The primary reason I removed these three parties is because MVC 3 and Razor are good enough so you don’t need the 3rd party tools.

The best practices I mentioned above come straight from experience and the following people/resources:

You can see the exact same Core code in use at sweetandhappy.com. If you see any improvements that can be made or you’d just like to comment, please do so!

Also, please note that I basically am even re-posting the exact same blog post I did over a year ago with my original MVC demo project. I hope you don’t mind, but it’s late and I have to work tomorrow :)

Thanks for reading!

Shout it

kick it on DotNetKicks.com

Tuesday, January 12, 2010

ASP.NET MVC Sample Project Launched




Okay, after popular demand of I think 3 people, I’ve published the code for the wife’s website. If you haven’t read any of the posts on my wife’s project, check out this post. This project completely separates UI & C# code, so you only have 2 projects (Core & UI).

Basically, all I did was create a new MVC project with Microsoft’s default template and deleted everything except the following:

  • Views folder
  • Root Default.aspx
  • global.asax (I did delete the global.asax.cs)
  • both web.configs

I referenced my Core, setup my views that match my Core project, and inherited from my global.cs in the global.asax. That was it!

You can check it out via svn here: https://derans.googlecode.com/svn/trunk/

or

download it via zip here: http://code.google.com/p/derans/downloads/list

I feel like this project will act as a great stepping stone to understanding the Code Camp server, which is much more complex.

My unit tests are definitely the weakest part of the project, so there won’t be a need to run any coverage tests or anything like that because this project will not do well :)

The sample project was built with the following tools:

The best practices I mentioned above come straight from experience and the following people/resources:

You can see the exact same Core code in use at sweetandhappy.com. If you see any improvements that can be made or you’d just like to comment, please do so!

Thanks for reading!


Shout it

kick it on DotNetKicks.com

Sunday, December 13, 2009

Beauty of Interfaces

For those of you that don’t believe in interfaces and dependency injection, here’s one of the beautiful things about taking advantage of it.

If you haven’t read my last post, here’s a quick update of the project I’m working on. My wife has a photography business and she is wanting to update her site. Since I wanted to make it fun for me too, I decided to use all the best practices that I’ve learned thus far on an ASP.NET MVC project.

So basically, we started talking about using flickr to load her portfolio gallery and I had already coded it to load from the local machine. Since she wants to use the pics on her Facebook, her blog, and anywhere else that might be needed, we thought flickr was the way to go. Also, she really likes flickr’s upload and tagging system.

So since I was smart in the beginning thanks to all the different developers I follow, it was really simple to switch out all that code. The only thing I had to touch to switch out the entire photo repository was the structuremap code. Let’s get to the code:

Here is my IPhotoRepository interface, which is the only thing my UI knows about:

    public interface IPhotoRepository
{
IList<Photo> GetHomePhotos();
IList<Photo> GetPhotosBy(string portfolio);
}

GetHomePhotos is a method that returns the photos I loop through on the homepage with jQuery. The GetPhotosBy method is the meat of the project.


Here is my initial implementation of the IPhotoRepository, which loops through a particular directory.


public class PhotoRepository : IPhotoRepository
{
private readonly IDirectorySearcher _directorysearcher;
public PhotoRepository(IDirectorySearcher directorySearcher)
{
_directorysearcher = directorySearcher;
}

public IList<Photo> GetHomePhotos()
{
return GetPhotosBy("home");
}

public IList<Photo> GetPhotosBy(string portfolio)
{
portfolio = portfolio.Replace(
" ", "");
var photos = new List<Photo>();
foreach (var f in _directorysearcher.FindFilesStartingWith(portfolio))
if (!f.Name.Contains("_t"))
photos.Add(
new Photo() { Name = f.Name });

return photos;
}
}

Basically the way we planned on it working was through a naming convention and the wife would’ve had to create the thumbnails as well…now we just use flickr’s.


My new implementation takes advantage of flickr.net, so the calls to flickr’s APIs are really simplified. Here is the new implementation:


    public class FlickrPhotoRepository : IPhotoRepository
{
public IList<Core.Domain.Model.Photo> GetHomePhotos()
{
return GetPhotosBy("home");
}

public IList<Core.Domain.Model.Photo> GetPhotosBy(string portfolio)
{
var flickr = new Flickr("yourapikey");
var ps = flickr.PhotosSearch("yourflickruserid", portfolio, PhotoSearchExtras.OriginalFormat);
return Mapper.Map(ps.PhotoCollection, new List<Core.Domain.Model.Photo>());
}
}

There are a couple of things that are interesting about the above code. One is the PhotoSearchExtras.OriginalFormat, which returns a null for the original picture unless you upgrade to flickr pro. It took me about an hour to realize that that was the problem because it doesn’t really tell you to make sure you upgrade to pro in the error message. You just have to go read and research to figure it out.


The other interesting thing about the code above is the Mapper.Map, which is an automapper function. Here is my create map code, which resides in my automapper profile class. For more information about automapper, you can visit the automapper site or read my post on it.


            Mapper.CreateMap<FlickrNet.Photo, Photo>()
.ForMember(dest => dest.ImageUrl, opt => opt.MapFrom(src => src.OriginalUrl))
.ForMember(dest => dest.ThumbnailUrl, opt => opt.MapFrom(src => src.SquareThumbnailUrl))
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Title));

This is the beauty of interfaces because here is my PortfolioController:


    public class PortfolioController : SmartController
{
private readonly IPhotoRepository _photorepository;
public PortfolioController(IPhotoRepository photoRepository)
{
_photorepository = photoRepository;
}

public ActionResult Index(string name)
{
var pv = new PortfolioView {Name = name, Photos = _photorepository.GetPhotosBy(name)};
return View(pv);
}
}

See it has no idea that I completely changed the implementation of the photo repository. Another cool thing to mention I think is that the wife can add how many ever different portfolios that she wants. All she needs to do is create a tag and then go to /portfolio/newtagname and her site will load that particular photo list with that tag. So if you want to go to her weddings gallery, you’d go to /portfolio/weddings, if she had a preview for a client, she could do /portfolio/lastnameofclient and just not link that on her site. Anyhow, I thought it was cool. I don’t remember where I heard this, but some developer at some point told me it’s important to create hackable URLs so your end-users can guess at the URL.


So the only code I had to change to swap out the repository was this line:


            ForRequestedType<IPhotoRepository>().TheDefault.IsThis(new FlickrPhotoRepository());

That’s it! As always, please comment if you see anywhere that I could improve or you just have a comment. Thanks for reading!

kick it on DotNetKicks.com

Related Posts Plugin for WordPress, Blogger...