Showing posts with label Development. Show all posts
Showing posts with label Development. Show all posts

April 21, 2010

The using Statement - C#

Something that I have thoroughly enjoyed when working more with C# and .NET has been data access with LINQ to SQL. It is a nice mix between using an ORM and making direct changes to a database. One issue remains with using LINQ to SQL is handling the database connection properly. I was recently told to wrap data access code in a using statement for better performance.

The using statement guarantees that resources are properly disposed of after their use. This eliminates the need to remember to close database connections and will dispose of objects wrapped in the statement as soon as they leave scope. Even though the .NET Framework has built in memory management, there is still time between when the objects are no longer needed and when their unneeded memory is released to the rest of the program.

The following is an example of data access using the using statement:
using(_ctx = new SampleDBDataContext()){

var mainQuery = (from c in _ctx.GetTable<SampleTable>()
where c.isMain == true select c);
...
}
The using statement defines the scope of _ctx. So as soon as the database operations are complete, _ctx is trashed.

Here is a more in-depth writeup of using using with data access - http://www.w3enterprises.com/articles/using.aspx

April 18, 2010

Listing Files in a Directory - C#

A portion of my senior design project is a media center component, so I needed an easy way to check the contents of multiple directories. From there, I compare the contents with a database that contains more information about the files.

Retrieving the contents of a directory in C# is very easy, and the code is included below.
DirectoryInfo directory = new DirectoryInfo("C:/some/directory");
FileInfo[] files = directory.GetFiles();
foreach (FileInfo onefile in files){
Console.WriteLine(onefile.Name);
}
The DirectoryInfo object points at a directory on the system. From here, the program has access to create and delete other directories, list directories and files, and view last accessed time information. GetFiles() returns an array of FileInfo objects, and from there, the program has access to information about each file in the directory.

April 9, 2010

OData and the NetFlix Catalog API

The Netflix OData Catalog API was announced during the second keynote of Mix10 earlier this year. This announcement also meant that the live preview of the service was now publicly available. This means that all of the movie information built into the Netflix site (movies, actors, genres, release dates, etc.) is now available through an easy to consume service.

I have a very, very unorganized movie collection, and have been trying to write a system to keep track of what I have and where I have it stored. Whenever I tried to write it I always had to stop myself because I could not get the right amount of information about the movies. I even went has far as scraping the information box on movie pages from Wikipedia. This was promising, but I stopped working on the system after a few days.

Now, armed with the NetFlix Catalog API, I can finally get the right amount of information into my system to make it fun and useful. This is a quick demo of how to get information from the NetFlix Catalog Service.

To get going with the NetFlix Catalog API, whip open a new .NET MVC2 Visual Studio Project and get to work.

To access the Catalog API, a Web Reference will need to be added to the project. Right click the project then Add a Service Reference. Complete the dialog box like the picture below:

Once the Service reference is added to the project, we can access the NetFlix Catalog data.

Quick sidebar - If you are using .NET 3.5 SP1, you may need to download an update here - http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=4b710b89-8576-46cf-a4bf-331a9306d555. The DataServiceCollection is not included as a part of System.Data.Services.Client, but the update fixes this.

From here, you can access the NetFlix Catalog in Visual Studio. Define an instance of a NetflixCatalog service as shown below.

        public ActionResult Index()
{
NetflixCatalog _nfCat = new NetflixCatalog(new Uri("http://odata.netflix.com/Catalog/"));
DataServiceCollection<Title> _titles = new DataServiceCollection<Title>();
var query = (from t in _nfCat.Titles where t.ReleaseYear < 1950
orderby t.ReleaseYear select t).Take(50);
try
{
_titles.Load(query);
}
catch (InvalidOperationException excep)
{
Console.WriteLine(excep.Message);
}
var titles = _titles.ToList();
return View("Index",titles);
}


Please note that this code will not run without adding a reference to a WindowsBase dll. Mine was at C:\Windows\winsxs\msil_windowsbase_31bf3856ad364e35_6.0.6000.16708_none _9540b0033275cea4\WindowsBase.dll

Use the service to build a linq query, then load that query into a DataServicesCollection object. Use the following code to display it on a page. Apologies in advance for the terrible way of doing a numbered list.

    <p>
This page will display the 20 earliest movies entered into the Netflix Catalog (Ordered by Movie Title)</p>
<h2>
Titles:</h2>
<ul>
<%int count = 1; foreach (var title in (IEnumerable<NFCatalog.NetFlixService.Title>)Model)
{ %>
<li>
<%=count %>. <%= title.Name%>, <%= title.ReleaseYear%></li>
<%count = count + 1;
}; %>
</ul>

The final page is displayed like this: