As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I wonder why .NET framework doesn't have pair (Observer/Observable) interfaces similar to Java's feature?
EDIT:
yes i know about events and delegates but using those interfaces is a simple and bookish approach of this DP isn't it?
In .NET 4 it does: System.IObservable<T> and System.IObserver<T> (which are a Dual of IEnumerable<T> and IEnumerator<T>). Look at the Reactive Extensions (Rx) project for compositional use of these interfaces with asynchronous events.
More generally, the Observer pattern is better served in .NET with events.
Because .NET has actual events and delegates. Java lacks these basic constructs and has to resort to ugly hacks (your Observable interface) to pass "method pointers" around.
You can achieve the same thing with events and spaghetti code and they will drive you crazy trying to cleanup after them as they lock themselves into your non-disposable memory blocks.
If you don't yet have .Net 4, use the well know observer pattern and type it out the old-fashioned way - by hand.
The observer pattern is not simply writing events longhand, it's the basis for re-usable and extendable code. It is also a more decoupled and elegant approach to your solution.
You can achieve the same sort of thing with Events. Also you can easily implement your own Observer pattern. It has to be one of the easiest patterns to implement: http://en.wikipedia.org/wiki/Observer_pattern
As for the why part. Not sure.
Where have you been. It is called events and delegates. Yes, it is a hack, but it works and more people prefer to use the language built in features over a design pattern you must type in yourself. The language feature is already debugged and ready to go.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
A few months back, we introduce Rx into our codebase, and since then the codebase is getting more and more "reactive". I feel that it is really easy to introduce concurrency into the codebase with Rx, as not a single line of "locking" was used yet.
However, I do not understand why it is easy with Rx when I can't explain it in words.
Is it related to the "Actor Model" and "Functional Reactive Programming" concept?
Can someone kindly enlighten me on this please?
I think the main reason it's "easy" is because of the blood, sweat and tears poured into the Rx library by the very smart Dev team behind it at MS.
Look at the (open) source code to see just how much careful code goes into enforcing the Rx grammar and the parameterisation of when and where things run using Schedulers. That has plenty of defensive concurrent code in it. I suggest it's the grammar and Schedulers that bring the simplicity.
Using the model is quite easy, but achieving that simplicity was not trivial. You are benefiting from standing on the shoulders of giants that have hidden the complexity behind a neat and tidy API :)
Incidentally, there is still the odd trap for you to fall into... I'm sure you'll find one sooner or later! One example is that Subject<T>.OnNext() is not protected from concurrent access in Rx 2.x for performance reasons.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm not a C++ guy, but I'm forced to think about this. Why is multiple inheritance possible in C++, when I'm not able to do it in C#? (I know the diamond problem, but that's not what I'm asking here). How does C++ distinguish the ambiguity of same method signatures inherited from multiple base classes? And why the same design can't be incorporated in C#?
This is a question of choice. Anders Hejlsberg, the C# language designer, chose to leave multiple inheritance out of the language. You may wonder why... My guess would be that (1) multiple inheritance is often not needed, (2) multiple inheritance is often used in the wrong way (like so many object-orientation constructs) and (3) it would make the language and/or the compiler and/or static checking unnecessarily complex.
The CLR itself does not prevent multiple inheritance; hence, it is available in C++.NET.
You can't in Java or C# because it's a design decision built into the language. Whether you agree or not, the language designers decided that the difficulties of multiple inheritance of implementation, as done in C++, wasn't worth the cost.
C++ already allowed multiple inheritance of implementation when I was writing it in 1995.
That choice was made by Java back in 1995. C# followed suit later on for the same reasons.
I'll point out that both Java and C# allow you to implement as many interfaces as you want. It's multiple inheritance of implementation that's the issue.
I'll leave the answer as to how C++ disambiguates multiple inheritance of implementation to others who have used the language more recently than me.
The ambiguity in the "diamond of death" is resolved using virtual inheritance. the wikipedia article is as good an explanation as any.
Virtual Inheritance
It is also potentially resolved by just designing your class hierarchy better. As a rule of thumb, multiple inheritance is ok as long as you're inheriting multiple interfaces (in c++, pure abstract classes).
From msdn.micosoft link
Multiple-inheritance is supported in C++. However, just about all the other modern object-oriented languages, including Java, have chosen not to allow multiple-inheritance. (Some advanced languages, such as Eiffel, have attempted to work out the kinks of multiple inheritance)
The biggest problem with multiple-inheritance is that it allows for ambiguity when the compiler needs to find the correct implementation of a virtual method.
So, in the interest of keeping things simple, the creators of Java and C# decided not to allow multiple inheritance. However, there is an alternative to multiple inheritance, known as interfaces
So keeping all these issues in mind designers doesn't allow multiple-inheritance in language, but still language support it in some other way like interfaces.
In addition of above answers & links you can also look to why doesn't c# support multiple inheritance
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
We are building a .NET application and i'd like to integrate a framework for doing DI and some AOP (injecting some diagnostics/logging code).
I know there's a multitude of frameworks out there, i am not sure which one to select, since each site that reviews them gives out different results and opinions.
I would love to hear some objective information based on real world experience for doing the things we require (listed above).
Short answer: Take a look at PRISM, UNITY and MEF to stay fully in the realm of Microsoft patterns and (best) practices. No reason to divert from that imo, unless you do really small projects (for which Prism may be oversized).
The answer is foremost in the design of your application. If you design your application around the SOLID principles, adding cross-cutting concerns will mostly be as simple as writing a decorator. In other words, when you need code weaving frameworks as Postsharp or need to do interception, you probably need to take a close look at your design again. Take a look for instance at how to model business operations with commands and handlers, or how to model queries as DTOs and handlers.
All containers allow you to wrap services with decorators, simply because you could register a lambda that does something like this:
container.Register<ICommandHandler<ProcessOrderCmd>>(() =>
new DiagnosticsCommandHandlerDecorator<ProcessOrderCmd>(
new ProcessOrderCommandHandler()));
However, when the whole application is designed around SOLID and the application grows big, manually configuring every service like this will become cumbersome and time consuming. So in that case it is very useful to pick a DI framework contains a batch registration feature and has support for registering decorators. Especially support for handling generic decorators (as the DiagnosticsCommandHandler<T> as shown above) will get important.
For instance, when you use the Simple Injector IoC container you can register all command handlers with a decorator in just two lines of code:
// This registers all command handlers in the container.
container.RegisterManyForOpenGeneric(typeof(ICommandHandler<>),
typeof(ICommandHandler<>).Assembly);
// This wraps all command handlers with the given decorator.
container.RegisterDecorator(typeof(ICommandHandler<>),
typeof(DiagnosticsCommandHandlerDecorator<>));
Although some patterns or frameworks might be overkill for small applications, I believe that the SOLID principles are core principles for object oriented design, and every application should be design with those principles in mind.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What would you suggest as a road map for becoming very proficient in writing multithreaded applications - beyond "tinkering"?
I am a C# developer - would branching off into some other languages help this endeavor?
Does the parallel addition to .NET 4.0 hide things that you should know in order to make it easier?
Read Joe Duffy's "Concurrent Programming on Windows". Joe is an amazing expert.
Investigate different approaches to concurrency on different platforms; look at Erlang, Scala etc
Likewise read Java concurrency books, which will have some subtly different details, but often tackle the same core issues and have some useful patterns. "Java Concurrency in Practice" is often recommended.
Look at the various options on .NET, including the Coordination and Concurrency Runtime and F# asynchronous computations
Definitely learn Parallel Extensions - it'll help a lot, and from what I've seen, a lot of very careful design work has gone into it. (It's changing somewhat for 4.0b2 though, so you may want to defer this for now.)
Theres a really good PDF about threading in .NET here the MSDN documentation for the Thread class as well as the threading primitives (Mutex, WaitHandle, ReaderWriterLockSlim et al) is also good reading.
The key things to understand are:
When to use a thread
When not to use threads
How to manage sharing state between threads.
I could go on to explain these here, but I feel the threading PDF linked to above does a far better job than I could in that respect, the key point is that threads are a powerful tool and understanding when and how to use them will make you more proficient in their use than simply reading MSDN, although strategies for using threads effectively are covered there also.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What are some of the open source projects out there that you would hold up as shining examples of projects that correctly and effectively use enterprise sofware patterns and best practices such as Inversion of Control, Model-View-Controller, Unit Testing, etc.?
For purposes of this question the project should:
Include source code that illustrates the pattern in use, and
Be doing something important and useful, i.e. not using the pattern frivolously just because it is flavor of the week. Hence the words, "Correctly and Effectively" in the question
It should be software that you could show to the people who work for you and enthusiastically be able to say, "I want you to do it the way these guys did it."
Most of the GNU project is very very well written, over a very long period of time, with strict guidelines.
Prism is very good for MVVM in WPF and Silverlight
patterns they use Patterns in the Composite Application Library
An objective pick would be the Spring Batch project.
How did I pick it? Judging from the technical debt as seen on the Nemo Sonar instance, Spring Batch has the lowest debt/line ratio for projects larger than 10k lines of code.
When I first looked at the source code for DotNetBlogEngine, I was impressed at how well it was organized. And it didnt couple any of its components to the interface, making it extremely flexible.
It's not a terribly huge project either, not like trying to recompile your own linux kernal or something. So you can dive in quickly and have some fun with it.
Castle project
You can try this book - Beautiful Code
The author has collected some experience-sharing articles of open source projects. e.g. Python's Dictionary Implementation, Subversion's Delta Editor, etc.
You mean Spring? Or projects that use it?
UPDATE: WebWork, Guice, and Spring Security all fit the mold.