Geeks With Blogs
Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C

It was about 2-3 months into working with .NET that I finally realized the amazingness that are class libraries. It was one of those things that I knew would be useful, but I could never really find a great explanation for how to get them to work and how to easily pass them around multiple projects and update them, etc.

I’ll write the post as if I’m working through the process:

So the basic part was easy. I have my web application and it has a ton of files in App_Code folder. I want to move them into their own class library, so that multiple parts of my overall product can share the same files and I don’t need to copy/paste them around (really bad way to maintain your code files, especially if they grow beyond 3-4 files).

I added a class project file to my solution and then physically moved the files over. That part seemed fine. When I built the solution though, of course the web program broke, because none of those references to the methods in the App_Code folder worked anymore.

Fix Issue #1: Add the Class Library Project as a DLL to the Bin folder.

In the solution pane, right click on the website project and go to the last menu item “Property Pages”. A dialog window will appear with something like this:

image

 

Click on "Add…” and you should see a dialog like this:

image

If you click on the Projects Tab, you should see your class library project(s) that you can now add to your website Bin directory. If you click OK, it will add a Bin directory if one hasn’t been added and it will drop in the DLL of your project. Note: Every time you build the solution, it will go ahead and auto update the dll, so you only have to set this up once.

So now the DLL was added to the web site project ,and it now has access to all those methods and class files. The next thing I hit an error with is trouble with namespaces.

Fix #2: Update/Use Namespaces

Unfortunately when I was originally working with this particular project, I hadn’t really seen the need for namespaces, because the size of my project didn’t dictate the need for it. However, as the class files grew and the methods worked their way into the 100’s, it was definitely a requirement. (Any new project I work on now I immediately start organizing the files in namespaces.)

Anyway, so I had added namespaces to the classes and organized them into things such as myCompany.myProduct.DAL (Data Access Layer) and .HelperFuntions, etc. (Whatever works and makes sense for you and your company is fine.)

I went back into my website project and made sure that I was using “using” statements in the code-behind files that were calling methods and variable types.

One final thing that makes working with solutions easier is to set your startup project.

Fix #3: Set your Start up Project

If you’re like me, you build and test your project often (typically with Ctrl + F5). It’s annoying though, to build and “preview” while inside your class library project, because you’ll get an error indicating that you cannot start a class library project on its own (makes sense.)

Go up to the top of your solution, and right click and select “Properties”.

image

In the next dialog, set your startup project from the drop down list:

image

You can also take a look at “Project Dependencies” and check what projects depend on what ,so that they build in the proper order before you preview them in the browser.

image

 

Cheers!

Posted on Saturday, July 25, 2009 2:13 PM Tutorials | Back to top


Comments on this post: Tutorial: Implementing/Working with Class Libraries

# re: Tutorial: Implementing/Working with Class Libraries
Requesting Gravatar...
nice post bro, thanks for informative news like this!!
Left by Stop Dreaming start Action on Jul 25, 2009 6:21 PM

Your comment:
 (will show your gravatar)


Copyright © samerpaul | Powered by: GeeksWithBlogs.net