Tag Info

New answers tagged

1

Try creating a generic interface (INode<out T>). You code will change into something like this: interface INode<out T> { T Parent { get; set; } } class Node : INode<Node> { public Node { get; set; } } class AVLNode : INode<AVLNode> { public AVLNode { get; set; } } Your calling code is simply: AVLNode a = new AVLNode(); ...


1

This can be done using reflection over the assemblies if you use some conventions. Other DI libraries like StructureMap which I use has this feature. A quick search for Unity found this library Unityconfiguration although I haven't used it. I also see that Unity 3.0 supports registration by convention. So the easiest option may be to upgrade.


0

You probably already tried this option, but you don't specifically state so in your question: Can you instantiate the objects and set them to a certain state? Perhaps calling doSomething results in a some new state that is enough to verify it has properly operated on the objects? Of course if one of the objects launches a nuclear missile touches a lot of ...


3

This is exactly what ASP.NET (or the more modern Razor) already does, only in reverse. So, you can't easily have C# with inline JS, but you can have JS/HTML with inline C#, which I think will work just as well.


6

You're clearly not the only one who finds this useful, as it is done all over the place as you've shown above. From putting compiler optimized code into methods for speed, to using scripting languages for their simplicity, people have been co-mingling code for quite a while. The real trick here though, is getting the compiler to know what the heck you're ...


1

There is at least one advantage you didn't mention: If Castle is updated with a bug-fix release on NuGet, the user is going to get that updated version. If you combine all the advantages together, I think they outweigh the disadvantages. So, I think you should just add the reference to the Castle package to your package and be done with it.


0

Would this be a good approach? I think a better approach would be to always load HasNotes together with the rest of the entity. That way, the property is basically free (one more returned column should be much cheaper than making another SQL query for each entity).


3

If it's not too late to change some of your own code you could wrap the InternalClass and InternalElement and provide an interface for the wrapper so that the wrapped object can be easily mocked (essentially the facade pattern). If you don't control these classes it's probably a good idea to wrap them anyway to isolate changes in them from your code.


3

You can never know how users plan to use your code. In your proposal, if a caller first looks up HasNotes and only later Notes, you'll have one SQL call too many as well: first the COUNT, and then the entire SELECT to populate Notes. If you do know that users will never call HasNotes before Notes, then your implementation make sense, but in that case, I'd ...


1

Here is a trick that sometimes works in cases like that: in your testing assembly, do not reference the assembly containing doSomething. Instead, reference the source code file, and provide your own InternalClass / InternalElement implementation (which serves as a mock) in that testing assembly . Of course, your InternalClass must provide exactly the same ...


0

public abstract class Wrapper { private A theA; private B theB; // these are private on purpose. // add properties and methods for A and B stuff that you // want to expose. Make sure the names look like they're // all from one coherent class. // make them `protected` as needed for use by only the subclasses. // do not give a ...


6

Stop! You have a bigger problem than the readability. It looks like you don't understand how object initializers work. Let's create a disposable class which traces its execution: public class Demo : IDisposable { private string hello; public Demo() { Debug.WriteLine("The parameterless constructor was called."); } public ...


8

What is Link? If you have a base class Base with A : Base and B : Base, do you need to know that a.Link is of type A and b.Link is of type B? If not, it is as easy as: public abstract class Base { public Base Link { get; set; } } If, on the other hand, you need to preserve the type of Link, then what about generics? public class Common<T> { ...


1

The first snippet you posted isn't less readable because it uses initialization - it's less readable because it's poorly written. Each initialization assignment should have its own line (unless there's only one assignment, or the initialization block is very short.) As for performance, I would be very surprised if the first snippet compiled differently than ...


0

For the condition stated in the text this implementation is good, I'd even call it first choice (except for an extra tab needed for 3 rows)


1

I would go for the database. Quick, easy and known. As per your issues, I do not know why you mix application servers with database server. After all, you can have your database server in a machine with no application server at all. Having a "conceptually" separate database server allows your fellow SysAdmin to optimize that server for databases engines. ...


0

> "bussiness role engine". Is it a right way to manage these condition? It depends. "bussiness role engine" can make sense if you have several instances with different definitions of "canSaveOrder" (i.e. different internetshops that use the same software) the meaning of "canSaveOrder" often changes. Otherwhise the "bussiness role engine" may ...


1

If you're just asking whether the same code can be formulated in a better way, that is indeed a duplicate of the question pointed out above. Read the answers to that question (and to its duplicates). However, a business rules engine is something quite different. It means that you specify the conditions in a separate repository from the code - maybe a data ...


0

You can use a Mediator pattern. You would have a master list of all available tests in the master workflow. A user won't talk to this workflow directly, but instead to the Mediator. The Mediator will keep track of which tasks are actually selected (in its own list), and expose those to the user.


0

Not sure how I missed this question but I'll add my two cents two years later. Client MVC vs. server MVC? My project is already a server-side MVC structure, so is there still a need for client MVC like Backbone.js provides? MVC and MV? even before it got pushed to the client-side has basically de-evolved into a marketing term that really only ...


0

The class you posted could indeed be replaced by an enum. However, there are some situations a developer might want to use a class of static properties, for example if they needed to execute code in get{} (do some calculations based on other variables, etc.) and/or they wanted the value to be changeable via set{}.


0

I think the main reason in this case is to add semantics to the code. It's about code readability. Instead of code looking like: CashRegister.Add(1 + 10) It now looks like: CashRegister.Add(OneDollarBill + TenDollarBill) (not saying this has high value, just thinking this might have been the reasoning).


0

The only thing that occurs to me is reflection. If you have a function somewhere that gets values using reflection, maybe a serilization function, it might make sense to do this. While you can use reflection on a static class, you can't get an instance of it to pass around. Note that while I am tossing this out as an answer, chances are that it is not ...


3

Funny, I can't imagine why you'd use an enum if it's just human-readable representations of a variety of numbers. Enums are good for a bunch of text values that don't have numeric representations. public enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } They're also good for enumeration maths (State ...


0

It rather depends on what behaviour you're trying to access. Expounding on your previous example: if (Animal is Dog) { Dog d = (Dog)Animal; d.BuryBone(); } else if (Animal is Cat) { Cat c = (Cat)Animal; c.DrinkSaucerOfMilk(); } This is perfect if you're after ...


2

As you said, this kind of a class can act like an enum, see the int enum pattern(seen at least in java before java introduced enums as their own class). Another reason would be to group up all kinds of small utility methods, such as handling xml, constants and the like, especially if handling them in their languange of choice is verbose. It could also just ...


8

tdammers answer is the correct way to do it, and 99% of the time you should do it that way. very occasionally there maybe situations when you don't want to do it that way. In these circumstances I'd use the 'as and check for null' approach. Indeed FxCop will actually warn you that you are casting twice (once with is and once with an actual cast) in your ...


26

Ideally, you should aim for just: Animal.DoWhateverYouNeedDone(); That is, implement the polymorphic behavior (if it's a dog, do a dog thing, if it's a cat, do a cat thing, etc.) in the classes themselves. So instead of this: if (Animal is Dog) { var d = Animal as Dog; d.Bark(); } if (Animal is Cat) { var c = Animal as Cat; c.Meow(); } ...


0

This problem doesn't necessarily need to be solved with design patterns but, to answer your question, you could arrange the tests you need using a simple list or use a composite pattern if you want to make a little more complicated. You could then use different strategies to iterate over the list of tests, each strategy reacting to success or failure ...


-1

.equals() compares variables by their contents. instead of == that compares the objects by their contents... using objects is more accurate tu use .equals()


7

The short answer: Consistency To answer your question properly, though, I suggest we take a step backwards and look to the issue of what equality means in a programming language. There are at least THREE different possibilities, which are used in various languages: Reference equality: means that a = b is true if a and b refer to the same object. It would ...


2

Semantic Merge is a commercial product under development which uses Roslyn for, erm, semantic merging and diff with C#. (Full disclosure - I have no connection with them, I just saw their product mentioned on Jon Skeet's blog)


0

I agree that reusing variable is more often than not a code smell. Probably such code should be refactored in smaller, self-contained, blocks. There is one particular scenario, OTOH, in C#, when they tend to popup - that is when using switch-case constructs. Situation is very nicely explained in: ...


1

To respond to the comments that you received, I think this was a useful experiment. To answer your question, the approach that I take is to do the easiest thing to make the test pass. This can have one of two outcomes. The first outcome is that another test is needed. This happens when the code you've written can't possibly be good enough. The example I ...


0

If the order of the result set is undefined by the implementation, then you'll need to write an assertion that's not dependent on the order of the results. CollectionAssert.AreEquivalent is one such method. Here's an example: // I'm assuming that you've written a helper method to get each fund's name var actualNames = GetFundNamesFrom(obj); var ...


4

If you enjoy programming c#, then that is what you should do. You could go through your whole life and never need to learn c++. That wouldn't make you a bad programmer, just a programmer that doesn't write c++ If you want to become a software developer, you are already in a very positive and lucky position. To have found a career choice you enjoy and feel ...


3

Your 14, do what you enjoy, the rest will follow in time. If you enjoy coding in general just go in the direction that you find yourself pulled to. If Java and C# do that for you than continue with that. If you see you interest dropping, then pick a new language or framework to work with. Maybe c++, or maybe python or ruby. The point is that the language ...


-1

If what you want is to rasterize a polygon, you can find quite a few answers on 2D rasterization on StackOverflow. The following resources might be helpful: Efficient Polygon Fill Algorithm With C Code Sample Filling Polygons (PDF)


-1

The first thing to spring to mind is implementing Bresenham's line algorithm on each edge in your polygon.


0

I think using some kind of web-API proxy would be your best guess if you are concerned about the security of the app. It has been proven that Windows 8 apps can be decompiled easily when they are not obfuscated (just like any other Java or .NET based program without obfuscation), and even with obfuscation, one could argue that an eventual hacker would be ...


9

When naming classes it's often tempting to include words that describe usage in programming terms. These words might include abstract, entity, and/or base. So you end up creating classes like AbstractOffice or BaseOffice. The name of the class implies two meanings what it does Office and how it's used Abstract. I don't recommend doing this. It adds extra ...


0

I'm sure this has been done before. Here is a Math parser done in C# http://www.codeproject.com/Articles/462180/Yet-Another-Math-Parser-YAMP Here is a list of parser projects on codeproject.com http://www.codeproject.com/KB/recipes/#Parsers


-1

Have you taken a look at Math.NET It does numerical/signal calculations and many more There is also a stackoverflow tag for Math.NET These is also a good list of potentials on stackoverflow


1

Overview It sounds similar to CommonClosurePrinciple, but happens on a class-and-methods level instead of a package-and-classes level. Namely, --Classes-- (methods) within a released --component-- (a class) should share common closure. If one (method) needs to be changed, they all (methods) are likely to need to be changed. What affects one, affects ...


-1

For Java and C# the benefit lies in their being object oriented. From a performance point of view - the easier to write code should also be quicker: since OOP intends for logically distinct elements to be represented by different objects, checking reference equality would be quicker, taking into consideration that objects can become quite large. From a ...


17

C# does it because Java did. Java did because Java does not support operator overloading. Since value equality must be redefined for each class, it could not be an operator, but instead had to be a method. IMO this was a poor decision. It is much easier to both write and read a == b than a.equals(b), and much more natural for programmers with C or C++ ...


0

It's generally a good idea to separate the aspects of your product in separate components and extract them to independent modules as much as possible (or projects, if you want). However, it's not necessarily as easy to do in all languages, as the level of tooling at hand may vary greatly and a lower-tech solution might be a good compromise between a decent ...


1

I would most definitely create 3 separate projects for each layer you hope to create. By using separate projects it makes your solution much more discoverable and maintainable. To read a bit more on when to divide up projects see this answer In the solution that you create you can make solution folders that nicely segregate the projects into more ...


1

Using an iPad mini as a stand-in for testing all different versions of iPads and iPhones is questionable. I would not want to build an app for both platforms without constant testing on both an iPhone and iPad--and then I would want to have at least a quick round of testing on as many different versions of each as I could find as well. Though you could ...


2

It is far easier for a beginner (and any programmer) to think of: not using a variable that is not relevant than to think of: adding code structures aiming at preventing himself from not using a variable that is not relevant. Moreover, from a reader's point of view, such blocks have no apparent role: removing them has no impact on the execution. The ...



Top 50 recent answers are included