define an marker interface which will work like Serializable interface without inheritance - serializable

I want to create a marker interface which will behave the same way as Serializable interface . But I don't want my interface to extend Serializable.
Ex.
Interface definition
interface MyInterface
{
}
class MyClass implements MyInterface
{
}
Currently Serialization and deserialization is not possible for MyClass .
I want to know what I should do to achieve the same functionality.
Thanks

Related

When should I use/examples of nested classes?

Please retag this question to include languages to which it is relevant
So my java book had a whole chapter on nested classes, but ended on the note that you should only really use them when it comes to "modeling composition relationships and implementing internals of a class you want to hide". So lets discuss when you would want to use nested classes and some examples.
A nested/inner class is just a class that's only ever used specifically in the context of another class, which doesn't have it's own class file. If it's linked to an instance, it can only be instantiated in the context of a parent class instance; it can see private data, or only private static data if it's a static class.
The java developer site has a nested classes tutorial with one example:
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
A couple examples of usage:
Hide a concrete implementation of an
interface:
(Thinking of a database session for a tool like Hibernate): Suppose you have a Session interface, and a SessionFactory which returns an instance of a Session. The SessionImpl concrete class that implements the Session interface could be an innner class of the factory that knows how to construct and initialize it.
Supply information by implementing an
interface:
In the Wicket web framework, each GUI component has an associated "model", whose job is to wire data to the component. The interface looks something like:
public interface IModel extends IDetachable {
public Object getObject();
public Object setObject();
}
Suppose you have some special logic to retrieve data for a custom GUI component that you've written. Since no other component retrieves data the same way, you could use an anonymous class at the point where the IModel is supplied to take care of the data retrieval. If you have another point in the same class where you need to reuse your IModel implementation, you could make it an inner class. Later, if you need the model elsewhere, you could convert it to a top-level class.
Generally you use an inner class in a situation where you need a class definition, but that class is only usable or only makes sense in the context of the parent class.
A real life usage i had with nested classes, was in a global settings object.
The parent class was a Singleton, with nested classes as settings categories.
Settings
File settings
Print settings
Etc.
There was no real point in making the inner object as separate classes, as their would be no use for them outside the settings class scope.
I use nested classes for encapsulating algorithms that would be usually done as a method with lots of arguments. I use class that has raw data and I put algorithms into separate file in nested class (using partial keyword). That way I can put properties for that algorithm and its (working) data lives after algorithm is done.
I know that can be easily done without nested classes but this feels right because algorithm is purposely built for parent class.
public partial class Network
{
partial void initFDLF()
{
fdlf=new FDLF(this);
}
public FDLF fdlf;
public class FDLF
{
internal bool changed=true;
internal bool pvchange=true;
public double epsilon = 0.001;
public bool fdlfOk=false;
public void init(){...}
public void run(){...}
...

MEF: Mark interface for export

Is it possible to mark an interface for export, so that all derived classes will be available for import?
[Export( typeof( IMyInterface ) )]
public interface IMyInterface { ... }
[Import( typeof( IMyInterface ) )]
private readonly ICollection<IMyInterface> m_Concretes = new Collection<IPlugin>();
I don't know which classes are implementing IMyInterface in this example. The classes themselves do not know anything about MEF - and do not use the [Export] attribute.
As long as I do not mark every single class with [Export] it doesn't seem to work for me.
In the current preview, you can try putting a [PartExportsInherited] attribute on the interface (along with the Export attribute). I'm not sure whether this will work for interfaces or not, though.
We do plan to add support to putting exports on interfaces.
Yes in the current preview on codeplex you can mark the interface with both PartExportsInherited and Export to get have all implementers automatically be exported. In a up comming preview release we will likely be streamlining this to simply place a single attribute, perhaps something like [InheritedExport].
Edit: With MEF preview 6 this can now be done by placing the InheritedExport attribute on the interface.
Update: Using MEF v4.
[InheritedExport(typeof(IMyInterface))]
public interface IMyInterface
{
}
As expected, anything that inherits from IMyInterface will be exported as one.
Use [ImportMany] to have them all injected:
[ImportingConstructor]
public void MyClass([ImportMany] IEnumerable<IMyInterface> myDerivedObjects)

Generic App service Interface with multiple implementations

I want to implement a generic service interface into multiple classes. Is this possible to do? Seems that it just picks one of the classes to inject into. I'm likely missing something with the dependency injection itself. I would like to stay away from creating a separate interface for each class if possible.
Assume that you have an interface like that:
public interface IMyService<TEntity> { ... }
and you have 2 implementations:
public class MyUserService : IMyService<User> { ... }
public class MyRoleService : IMyService<Role> { ... }
To be able to inject IMyService<User> or IMyService<Role> you should register them to DI in your module's Initialize method:
IocManager.Register<IMyService<User>, MyUserService>(DependencyLifeStyle.Transient);
IocManager.Register<IMyService<Role>, MyRoleService>(DependencyLifeStyle.Transient);

Two Level class and interface extension throwing dart2js error because of different interfaces

I am having the following situation which is a bit hard to explain, but I will try anyway to make it as clear as I can.
I am working on a package for WebRTC that should be building dart applications. It was already done but I decided to rewrite the package with the following concepts:
Provide only interfaces to the user and one public class that can be instantiated
Leave implementation open for within the package
Thus we have to implement interfaces at some point in the private implementations
I rewrote most of my classes (added an _) and created interfaces for all of the classes and let the original classes implement these interfaces.
The problem is now, that I have a basic implementation that I want to extend internally and also make available through extended interfaces. The code looks something like this:
// --- Basic implementation
abstract class Peer { }
abstract class P2PClient<P extends Peer> { }
// --- Protocol extension
abstract class ProtocolPeer
extends Peer {
}
abstract class ProtocolP2PClient<P extends ProtocolPeer>
extends P2PClient<P> {
}
/**
* Implementations
*/
class _Peer implements Peer {
}
class _P2PClient<P extends _Peer>
implements P2PClient<Peer> {
}
/**
* -----
*/
class _ProtocolP2PClient
extends _P2PClient<_ProtocolPeer>
implements ProtocolP2PClient<ProtocolPeer> {
}
class _ProtocolPeer
extends _Peer
implements ProtocolPeer {
}
When I now try to compile this to JavaScript with dart2js it gives me the following errors:
Error: Dart2js does not currently support inheritance of the same class with different type arguments: Both P2PClient<Peer> and P2PClient<ProtocolPeer> are supertypes of _ProtocolP2PClient.
class _ProtocolP2PClient
^^^^^
I kind of know what the problem is:
_P2PClient implements P2PClient<Peer>
_ProtocolP2PClient implements ProtocolP2PClient<ProtocolPeer>
But I think that it's kind of the same if you look at it in detail:
ProtocolP2PClient will extend P2PClient with ProtocolPeer and because ProtocolPeer extends Peer, the interfaces actually don't differ. Is this just a limitation of dart2js and I should file a feature request or is there any other implementation I could come up with?

Abstract interface pattern

Can any one provide explanation of Abstract interface pattern.
The book "N-Layered Domain-Oriented Architecture Guide with .NET 4.0" has reference of this pattern, but no explanation.
I believe it means having an interface, an abstract class that implements that interface and then several non-abstract classes that inherit from the abstract class. In C# code:
interface IFoo
{
// interface members
}
abstract class FooBase : IFoo
{
// implementation of IFoo and potentially some helper methods
// some methods can be abstract, some virtual
}
class ConcreteFoo : FooBase
{
// overrides abstract members of FooBase and potentially some virtual ones
}
The advantage of using this pattern is that it combines benefits of interfaces (flexibility) with benefits of abstract base classes (sharing implementation).
Abstract Interface doesn't really exist in C#. In C++ you have the notion of a "pure abstract" class, in which all methods are abstract, hence it's an abstract class that only defines an interface.
In C# we have the 'interface' keyword instead, which does the exact same thing.

Resources