Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 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
231,269 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,373 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