2

I do have experience with Ruby on Rails, and am programming a project in ASP.NET currently. So having found ASP.NET MVC was awesome for me, since it seems to be a verbatim copy of Ruby on Rails in many respects. However, there are differences, and I have to re-learn quite some things.

One such thing is the way additional (library) functionality is handled. I want to add an encryption utility functionality, and in Rails, I would just add a class to the /lib directory and know it would be available in my controllers. How do I do that in ASP.NET MVC?

I thought about creating a model class for this, but I'm not sure if that's the right way to go. All I really want is an encrypt(plain) and a decrypt(encrypted) function that return a string, I will use the .NET encryption libraries for the actual encryption and decryption, but want to encapsulate and proxy their functionality with easy-to-use functions available across multiple controllers.

Any suggestions?

Yuck
  • 49,664
  • 13
  • 105
  • 135
haslo
  • 1,030
  • 2
  • 12
  • 23

4 Answers4

7

The best thing to do would be to create a second project that is simply a class library. Then reference this class library in your MVC application.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • Thanks, I'd do that (and make this the accepted solution) if it would be more than a single class. I might refactor it to an external project later on if things get more complex. – haslo Sep 02 '09 at 12:29
4

Within your project you could add a folder called lib, or whatever, and put the code in there.

Fermin
  • 34,961
  • 21
  • 83
  • 129
  • 2
    http://en.wikipedia.org/wiki/Lib - For "Lying in bed"? Or "One of two Jaredite kings in the Book of Mormon"? Bad practice. There are no bonus points for shortening down words. – Seb Nilsson Sep 02 '09 at 10:40
  • eh? "I would just add a class to the /lib directory" is why I suggested adding a folder called lib. – Fermin Sep 02 '09 at 11:12
  • 1
    I dont see the problem with using calling a folder lib. This is not bad practice and a lot of the alt.net and agile community use this type of naming convention. – skyfoot Sep 02 '09 at 11:32
  • It's bad practice. You understand your own code, but it will waste a lot of time for someone else to try to get into it. Readability is much more important than saving a couple of bits. Thanks for pointing out one of the problems with ALT.NET, by the way. – Seb Nilsson Sep 02 '09 at 11:35
  • Jeez, I don't know many programmers who'd have no idea what a "lib" folder meant. Call it lib, the world will go on. There are more important things. – Gavin Sep 02 '09 at 12:10
  • It's not about the word "Lib" explicitly, but the concept. "Everyone knows what x means" is the problem here. Inside baseball ftw... – Seb Nilsson Sep 02 '09 at 12:26
  • 4
    Well, going with the ASP.NET naming conventions, I think calling it Libraries to keep things consistent makes sense, too, I'll do that. That's the sole reason why I will not call it lib however, since lib really is a well-established convention for decades. Programmers who do not know that seem to have skipped some of their education, to be perfectly honest. – haslo Sep 02 '09 at 12:27
  • I would not call a project, file, class or variable lib. I would call it Library but for folde names there is a limit in windows for the path lenght wich I believe is around 260 chars. This is one of the reasons I use short folder names. – skyfoot Sep 02 '09 at 12:36
  • Seriously? This is an argument? – Matthew Groves Sep 02 '09 at 12:42
3

Asp.net MVC still has references so add your libraries to a folder of your choice and reference that library through the add reference option.

I like the following dev tree structure.

/docs
/lib
/src
/tools

the lib folder is for 3rd party libraries, src is the source of my mvc and tools is for tools like nunit etc.

for your own libraries add a new project for them just like you would in normal asp.net. I also like to add a new project for my model, bll, and dal.

skyfoot
  • 20,629
  • 8
  • 49
  • 71
0

This may not be a direct answer to your question but I have the following structure for an opensoure project which I am developing using asp.net mvc..

* Gol.Core.Session (contains session manager both real and fake)
* Gol.Core.Caching (contains caching including velocity, enterprise lib. provider)
* Gol.Core.Logging (contains logging components and providers)
* Gol.Core.Instrumentation (contains instrumentation related items)
* Gol.Core.UI (UI helpers, and other things related to UI)
* Gol.Core.Security (security,authentication related things)
* Gol.Core.Utilities (common utility functions like encryption, helper methods, etc).
* Gol.Core.Metadata (metadata manager).
*
  Gol.Web.Controllers (all controllers goes here).
*
  Gol.Cms.Contracts (contains the service contracts)
* Gol.Cms.Model (contains the service model)
*
  Gol.Cms.Services (contains the service implementation).
*
  Gol.Web (the web project that contains the views)
* Gol.Test (or Gol.Specification)

Have a related question with respect to this at Project structure recommendation for asp.net mvc

Community
  • 1
  • 1
rajesh pillai
  • 8,102
  • 7
  • 43
  • 60