How would I?

Share with us how you use PostSharp, or how you would like to use it.

How would I?

Postby mrpmorris on Sat Aug 23, 2008 9:43 am

I really like the look of PostSharp and have been trying to think of a way to use it, and now I believe I have a requirement! However, I think that having no experience the requirement is currently beyond me.

In my library I have a number of classes descended from Signal. Each descendant needs a corresponding signal factory (implements ISignalFactory). In 99% of cases the code will be exactly the same so I would like to replace the following code....

Code: Select all
public class LoginSignal : Signal
{
   public static readonly Guid ID = new Guid("{3B08476E-073D-4425-B066-D1799DEFD507}");

   public LoginSignal(SignalConstructorParameters parameters)
      : base(parameters)
   { }
}

public class LoginSignalFactory : ISignalFactory
{
   public void GetSupportedSignalIDs(List<Guid> signalIDs)
   {
      signalIDs.Add(LoginSignal.ID);
   }

   public List<SignalConstructorParameters> GetSignalConstructorParameters(ISignalTarget target)
   {
      return new List<SignalConstructorParameters>();
   }

   public Signal CreateSignal(SignalConstructorParameters parameters)
   {
      return new LoginSignal(parameters);
   }
}


With something more like this

Code: Select all
[SimpleSignal(LoginSignal.ID)]
public class LoginSignal : Signal
{
   public static readonly Guid ID = new Guid("{3B08476E-073D-4425-B066-D1799DEFD507}");

   public LoginSignal(SignalConstructorParameters parameters)
      : base(parameters)
   { }
}


This would require me to create a new class, implement ISignalFactory, and then implement its methods. Unfortunately I am currently clueless :-) Is it difficult to write something like this?

Another question along the same lines.....

Code: Select all
public class Person : ISignalTarget
{
  public void AcceptSignal(ISignal signal)
  {
    if (signal is LoginSignal)
      AcceptLoginSignal(signal as LoginSignal);
    if (signal is CancelSignal)
      AcceptCancelSignal(signal as CancelSignal);
  }

  private void AcceptLoginSignal(LoginSignal signal) { }
  private void AcceptCancelSignal(CancelSignal signal) { }
}


I would prefer

Code: Select all
[SignalTarget(new Type[] { typeof(LoginSignal), typeof(CancelSignal) })]
public class Person
{
  private void AcceptLoginSignal(LoginSignal signal) { }
  private void AcceptCancelSignal(CancelSignal signal) { }
}


I really have absolutely no idea how I would accomplish this task due to the variable list of types the developer may specify. Also the Accept.... signal might actually look something like this

private void AcceptLoginSignal(string userName, string password)

in which case I would want to do this

Code: Select all
if (signal is LoginSignal)
{
  AcceptLoginSignal(
    (string)loginSignal.MetaData["UserName"],
    (string)loginSignal.MetaData["Password"]);
}


or in the case of nullable parameters I would want to check if the key exists, and pass NULL if not

private void AcceptSomeSignal(int? someValue) { }

Sorry for so many questions in one go, just trying to think of everything before I start :-)


Regards

Pete
mrpmorris
 
Posts: 8
Joined: Sun Jun 08, 2008 7:27 pm
First Name: Peter
Last Name: Morris

Re: How would I?

Postby gfraiteur on Sat Aug 23, 2008 11:48 pm

I think code generation would be a better solution.
It will be cheaper to develop and more comfortable to use.
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org


Return to What To Do With PostSharp?