static-classes

Should I never use static methods and classes and singletons when following the Test Driven Development paradigm

烈酒焚心 提交于 2019-12-04 00:29:55
I've been reading that static methods, static classes, and singletons are evil when you try to implement unit testing in your project. When following the TDD paradigm, should I just forget that they ever existed and never use them again or is it ok to use them sometimes? Phil Sandler Never say never--static classes and methods have their place in your toolbox. That said, if the class you are trying to isolate and test (subject under test or SUT) depends on a static class or method, you will be unable to write a test that isolates the SUT from that static dependency--when your test code runs it

PHP avoid static classes to avoid dependencies, but I need to use global everywhere

三世轮回 提交于 2019-12-03 17:29:28
Many times I heard to avoid static classes because they will insert dependencies that will render your code unusable in other projects, and will not allow to unit test it . Let's say we have a typical class DB to access the Data Base, if such class is static we could call it wherever in our code: DB::execQuery(...); but this creates dependencies, so let's make the DB class NOT static, in such case we would have somewhere in our code: $db = new DB(); and then we could call in our code $db->execQuery(...); But now when using the $db inside a function we need each time to first declare it like

Laravel : Calling functions defined in base_controller from view

吃可爱长大的小学妹 提交于 2019-12-03 11:40:50
问题 In using the laravel framework, how can I call a function defined in base_controller, in a view. For exacmple: class Base_Controller extends Controller { public static function format_something() { return something; } } How can i call format_something() in a view file? Usually the error I get looks something like this: Method [link_to_action] is not defined on the View class. Probably a silly question, but thanks in advance! Edit Okay! First the correct place to do something like this is in

Abstract classes vs Static classes in C# [duplicate]

℡╲_俬逩灬. 提交于 2019-12-03 05:11:30
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What's the difference between an abstract class and a static one? Hello I Would like to know what are all the differences between abstract classes and static classes in C# When do I use what and why? Is it true the abstract class is a class which we cannot create instances of it? Thanks 回答1: I would like to know what are all the differences between abstract classes and static classes in C#. Don't ask questions

Abstract classes vs Static classes in C# [duplicate]

给你一囗甜甜゛ 提交于 2019-12-02 18:26:38
Possible Duplicate: What's the difference between an abstract class and a static one? Hello I Would like to know what are all the differences between abstract classes and static classes in C# When do I use what and why? Is it true the abstract class is a class which we cannot create instances of it? Thanks I would like to know what are all the differences between abstract classes and static classes in C#. Don't ask questions like that. I could spend hours listing hundreds of differences, none of which would be relevant to you. What is the most important difference between abstract classes and

Why are static classes used?

喜夏-厌秋 提交于 2019-12-01 16:12:07
I have doubts on static class and static methods. From MSDN I understood that "Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class." So if we don't want to associate a class over an instance , we will make it as static. Is that the only advantage? Can anyone guide me in which real time scenario we go for static class. Some time in classes(not static) I am finding static methods. What advantage/perfomance advantage do static methods give over instance methods in practical. For utility classes they are great. As

Why are static classes used?

落花浮王杯 提交于 2019-12-01 15:25:29
问题 I have doubts on static class and static methods. From MSDN I understood that "Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class." So if we don't want to associate a class over an instance , we will make it as static. Is that the only advantage? Can anyone guide me in which real time scenario we go for static class. Some time in classes(not static) I am finding static methods. What advantage/perfomance

When to use static classes and methods?

戏子无情 提交于 2019-12-01 04:01:34
I have a general question...when should i be using static classes or static methods?.. I know the idea that static methods can be called without instantiating...and static classes should only be used for static methods?...but are there any performance concerns also with it...and when should they be preferred over instance methods and classes?..If someone could just briefly mention when i should opt for using them and when i should avoid them? Leniel Maccaferri I think the following two links offer a clear answer for what you're looking for. Take a look at them: For static classes: When to Use

When to use static classes and methods?

橙三吉。 提交于 2019-12-01 01:37:27
问题 I have a general question...when should i be using static classes or static methods?.. I know the idea that static methods can be called without instantiating...and static classes should only be used for static methods?...but are there any performance concerns also with it...and when should they be preferred over instance methods and classes?..If someone could just briefly mention when i should opt for using them and when i should avoid them? 回答1: I think the following two links offer a clear

How to serialize non-static child class of static class

时光毁灭记忆、已成空白 提交于 2019-11-30 18:09:02
I want to serialize a pretty ordinary class, but the catch is it's nested in a static class like this: public static class StaticClass { [Serializable] public class SomeType { ... } } This code: StaticClass.SomeType obj = new StaticClass.SomeType(); XmlSerializer mySerializer = new XmlSerializer(typeof(obj)); Produces this error: StaticClass.SomeType cannot be serialized. Static types cannot be used as parameters or return types. That error seems completely irrelevant; StaticClass.SomeType is not a static type. Is there a way around this? Am I wrong to think this error is dumb? As a pragmatic