I've created an ADO.NET model called EF and added a DbContext generator, which populates my /Model folder with an EF.tt and .cs files, one for each entity.
in general the system creates classes with parameterless constructors... for some reason I can't fathom I have an entity that's missing this constructore. It is not an abstract class, has not base type and has public access. I have tons of other such classes but they all have parameterless constructors. I've googled and looked around VS trying to figure what's special about this one, and how I can make it generate the constructor, but find no answer.
I can always create this in a partial definition but I'd rather figure it out. Also, if I right-mouse click over the EF.tt I see a choice in the menu called "Run Custom Tool" but when I select it nothing seems to happen. How does one regenerate the .cs files?
p.s. yes, I have cleaned and rebuilt the solution in case it just got messed up but still problem
In C# (are you using C#?):
When you define no constructor in your class, a parameterless constructor will be created by compiler by default
When you define parameterless constructor (and maybe some others) the parameterless constructor will also be present as you defined it
When you define more than zero constructors, but no parameterless one, the compiler does not create a parameterless constructor for you. In this case it's your responsibility to define it (in partial class or not).
Default constructor exists by default, it is not generated. If class doesn't have any explicitly defined constructor it always have default parameterless constructor. If you specify any constructor elsewhere (partial class) default parameterless constructor doesn't exist any more and you have to create it yourselves if you want to use it (EF always wants to use it).
Related
I would like to write test for a controller class. The controller class takes a service object as constructor parameter. Added the #Inject annotation to the constructor of the service class.
class AssociateService #Inject()(configuration: Configuation){...}
The constructor parameter of the service class is a custom configuration object also created for the application. I added the #Inject to the constructor of the config class as well. Now I'm getting these types of error messages:
No implementation for "className" was bound.
Could not find a suitable constructor in java.lang.Integer. Classes must have either one (and only one) constructor annotated with #Inject or a zero-argument constructor that is not private.
The configuration class has several constructor parameters, those are "basic" types (Int, Boolean) and one parameter is a custom class type (className).
How should I do this binding or is it just enough to annotate something else?
And why it says that constructor error message?
As far as I know, there are two ways with tests and guice, with trade offs:
Don't using field injections, using only constructor injections and fields assignment in constructor for injected parameters. This approach enables very simple solution for testing, just don't use dependency injection in tests. But all your classes must have ability to be created with new operator in test cases...
Ps. You can define optional constructor and use field injections, of course, but it is not very clear solution.
Creating correct module with injectable interfaces binding to its implementations for every test or group of similar tests. Sometimes this approach takes a lot of unnecessary working hours.
You must design your software to maintain testability. Sometimes not every line of code in project need to be tested, sometimes not every code is testable, you must separate it from important parts of your software, that requires testing. If you design your software with single responsibility principe so writing tests is much easer...
I am trying to call the super constructor from a class using a method. The whole setup looks like this:
class Straight(hand: Hand) extends Combination(Straight.makeHandAceLowIfNeeded(hand), 5)
object Straight {
private def makeHandAceLowIfNeeded(hand: Hand): Hand = {
...
}
}
While this does compile, it has some rather odd runtime behaviour. While debugging, I noticed that the Straight instances have the "hand" property defined twice. Can somebody tell me what is going on, and what the proper way is to call the super constructor with different arguments?
In my use case, I want to call the super constructor with a modified hand in which I replaced a card compared to the original constructor argument.
Debugger screenshot with duplicate field:
.
It's a perfectly fine way to call the superclass constructor. These are two private fields and they don't conflict, though you can rename one of them to avoid confusion during debugging (or if you want to access the superclass' value from the subclass). However, the field should only be generated for a class parameter if it's used outside a constructor, and in your case it doesn't appear to be. Did you simplify the definition of Straight?
I have a C# class called MyCustomRuleTemplate which is inherited from Ektron.Cms.Content.Targeting.Rules.RuleTemplate class. In that I have added a constructor such as below
public MyCustomRuleTemplate(): base("someKey")
{
//Some code here
}
Its working fine without any error. If I given it as
public MyCustomRuleTemplate()
{
//Some code here
}
Im getting error like 'Ektron.Cms.Content.Targeting.Rules.RuleTemplate' does not contain a constructor that takes 0 arguments.
Can anybody help me to know why it is?
The reason you are seeing "does not contain a constructor that takes 0 arguments" when instantiating your class object using the second constructor is because when you call your constructor, c# tries to call the constructor on the base class as well, which in this case takes a parameter.
See this post on msdn:
http://msdn.microsoft.com/en-us/library/ms173115%28v=vs.80%29.aspx
Key parts:
"In this example, the constructor for the base class is called before the block for the constructor is executed. The base keyword can be used with or without parameters. Any parameters to the constructor can be used as parameters to base, or as part of an expression. For more information, see base.
In a derived class, if a base-class constructor is not called explicitly using the base keyword, then the default constructor, if there is one, is called implicitly."
And: "If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor using base."
I have use the type dynamic, a new type in .NET 4.0.
I want to use a dynamic type because I want to use some types that in advance I don't know what type is, but I know that all this possible type has some common methods.
In my case, I am using self tracking entities in entity framework 4.0, and I know that all the entities has the methods markedXXX (to set the state of the entity).
Through the dynamic object that I created, I can access and set the properties of one of this entities, but when I try to execute the MarkedAsXXX method I get an exception that says that the object has not definied the method.
I would like to know how to access to this methods. Is it possible?
Because I have a function that can access to the original values and set this values to the current one, but I need to set the entity as Unchenged.
Thanks.
I want to use a dynamic type because I want to use some types that in advance I don't know what type is, but I know that all this possible type has some common methods.
That suggests you should create an interface with those common methods, and make all the relevant types implement the interface.
Through the dynamic object that I created, I can access and set the properties of one of this entities, but when I try to execute the MarkedAsXXX method I get an exception that says that the object has not defined the method.
It's possible that this is due to explicit interface implementation. If the types have those methods declared as public methods in the normal way, it should be fine.
If you really want to use dynamic typing with these types, is there some base interface which declares the MarkedAsXXX methods, which you could cast the objects to before calling those methods? (I'm not familiar with the entity framework, so I don't know the details of those methods.)
Basically, I would try to avoid dynamic typing unless you really need it, partly because of edge cases like this - but if explicit interface implementation is the cause, then casting to that interface should be fine.
If you define an interface to the dynamically generated classes you can call the methods without the hassle of reflection calling.
how can I add a field, getter and setter to all implementations of MyInterface (in the current project or folder?)
Open the source file containing the
interface. Select the interface's
name and hit F4 (Open type
hierarchy).
In the type hierarchy view, select all of the classes that implement your interface. The hierarchy is displayed in a tree-like fashion so selection should be very easy.
Right-click over your selection, select Source, then Override/Implement Methods....
Mission accomplished.
[Edited]
When I wrote these steps, I thought that your intention was to create stubs of newly-introduced interface methods, in all classes that implement that interface.
Now that I am re-reading your request, I'm having a hard time understanding what it is exactly that you want to do. You wrote:
how can I add a field, getter and setter to all implementations of MyInterface
So, you have an interface named MyInterface and 1,000 classes implementing it.
You would like to introduce a new field, a getter and a setter for that field. So I guess my (and perhaps others') first difficulty is that you can't add a field to an interface, unless it's final - so your wish to "add a field to an interface" just doesn't sound right.
I suppose it would help if you give us a 30,000ft high-level diagram / explanation about your hierarchy and exactly what it is that you're trying to accomplish.
I don't think there's a one-step way to go about this. You could add the getter & setter to the interface, then let the compiler tell you where your implementers are, and it should be a simple copy & paste to place the field, getter, and setter into every class after the first.
Alternatively, you could transform your interface into an abstract class, or introduce an abstract class between your interface and your concrete classes, but that's only if your classes don't already extend other classes.