So, I was challenged with writing a custom company Flickr upload screen. Since I have been working with Silverlight 3 and .net RIA Services a lot in the past few months I decided to go that route.
Initial research revealed that there was a .net library for Flickr stuff. Great...and it's free too.
My next challenge was to figure out how to use .net RIA services without any database stuff. Until this projcet, my .net RIA projects all had database back-ends. At the end it was not complicated and actually the .net RIA Document helped and well as the forums.
So, here are the steps to create a custom DomainService without data model.
1. Create you custom class
2. Create a list class from that
3. Create your metaadata class
4. Create your domain service
Below you will find code to those 4 steps. After that everything the same as if you were to develop a vanilla .net RIA Silverlight application. For more info on that check out silverlight.net video tutorials on .net ria services.
1. Custom Class
using System.ComponentModel.DataAnnotations;
using System;
namespace Flickrv2.Web
{ public partial class FlickrImage
{ [Key]
public string Title { get; set; } [Key]
public string Description { get; set; } [Key]
public string Tags { get; set; } [Key]
public string FileName { get; set; } public Byte[] ImageData { get; set; } public String URL { get; set; } }
}
2. Create a list class from that.
using System.Collections.Generic;
namespace Flickrv2.Web
{ public class FlickrImageList
{ private List<FlickrImage> list = new List<FlickrImage>();
public FlickrImageList()
{ }
public IEnumerable<FlickrImage> GetFlickrImages()
{ return list.ToArray();
}
public void AddFlickrImage(FlickrImage image)
{ list.Add(image);
}
}
}
3. Create the .metadata class
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel;
namespace Flickrv2.Web
{ [MetadataType(typeof(FlickrImage.FlickrImageMeta))]
public partial class FlickrImage
{ internal sealed class FlickrImageMeta
{ [Required(ErrorMessage = "Title is required")]
[Display(Name = "Please enter a title", Description = "Please enter the image title")]
public string Title { get; set; }
[Required(ErrorMessage = "Description is required")]
[Display(Name = "Please enter a description", Description = "Please enter an image description")]
public string Description { get; set; }
[Required(ErrorMessage = "Tags are required")]
[Display(Name = "Please enter tags", Description = "Please enter tags for the image")]
public string Tags { get; set; }
[Required(ErrorMessage = "Image is required")]
[Display(Name = "Please chose an image", Description = "Select an image.")]
public string FileName { get; set; }
public Byte[] ImageData { get; set; }
public String URL { get; set; } }
}
}
4. Finally, create your Domain Serivice class
using System.Web.DomainServices;
using System.Web.Ria;
using System.Linq;
using System.IO;
using System.Configuration;
using FlickrNet;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
namespace Flickrv2.Web
{ [EnableClientAccess()]
public class FlickrImageDomainService : DomainService
{ FlickrImageList Context = new FlickrImageList();
public IQueryable<FlickrImage> GetImages()
{ return Context.GetFlickrImages().AsQueryable();
}
public IQueryable<FlickrImage> GetImage(String Title)
{ return Context.GetFlickrImages().ToList().Where(img => img.Title == Title).AsQueryable();
}
public void InsertImage(FlickrImage image)
{ var title = image.Title;
var description = image.Description;
var tags = image.Tags;
var data = this.ConvertImageDataToStream(image.ImageData);
var flickr = new FlickrNet.Flickr
{ AuthToken = ConfigurationManager.AppSettings["FlickrAuthtoken"]
};
var flickr_id = flickr.UploadPicture(data,
title, description, tags, 0, 0, 0, FlickrNet.ContentType.Photo, SafetyLevel.None, HiddenFromSearch.Visible);
var flickrPhotos = flickr.PhotosGetSizes(flickr_id);
image.Title = title;
if (flickrPhotos.SizeCollection.Count() > 3)
image.URL = flickrPhotos.SizeCollection[3].Source;
else
image.URL = flickrPhotos.SizeCollection[2].Source;
}
public override void Submit(ChangeSet changeSet)
{ base.Submit(changeSet);
}
private Stream ConvertImageDataToStream(Byte[] _data)
{ return new MemoryStream(_data);
}
}
}
Here is some code from the client (Silverlight 3) on how to do the upload...
private FlickrImageDomainContext dc = new FlickrImageDomainContext();
private SubmitOperation soUploadNewImage;
void DoUpload()
{ try
{ img.ImageData = this.m_bImageData;
this.dc.FlickrImages.Add(img);
this.soUploadNewImage = this.dc.SubmitChanges();
this.soUploadNewImage.Completed += new EventHandler(loUploadImage_Completed);
}
catch (Exception ex)
{ }
}