Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# ASP.NET MVC Basics Modeling and Presenting Data Updating the Controller

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,372 Points

Is there a reason for not using a static Repository class?

All my interrogations are in the title. Currently on the course we create a constructor to instantiate a new ComicBookRepository. Isn't it more optimized to use the repository with static methods ?

// Using
var comicBook = ComicBookRepository.GetComicBook(2);
// In place of
var comicBook = _comicBookRepository.GetComicBook(2);

2 Answers

Steven Parker
Steven Parker
220,634 Points

Interesting idea.

But you'd have to redefine the class and its methods to be static before you could call it that way. And since only one instance is being maintained, the "optimization" in speed or size might be hard to detect. But it should be doable if you really wanted.

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,372 Points

Yeah I know but it may be more simple too, no need to remember to instantiate a new repo on the controller. Thanks for the answer