Singleton

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

Singleton

Postby cocotapioca on Thu Dec 06, 2007 7:42 pm


Hi! I wanna do something like this:

[Singleton]
public class Something
{
    public Something()
    {
       ... initialization ...
    }
... blah blah ...
}

And when i wrote "new Something()" always get the same instance.
Is this possible? Im thinking on doing a replacement of the .ctor il to get the same instance, stored in a static field created in post compilation. Or trying to do it with Laos is possible or easier?

Ignacio Vivona
cocotapioca
 
Posts: 19
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Ignacio Vivona

Re: Singleton

Postby cocotapioca on Wed Jan 30, 2008 9:17 pm

After some time taking exams and on vacation i wanna continue with this singleton attribute. How can i add a new field (static) to a Type? There is some example a TypeAdvice? I need to treat constructors like methods (actually they are, but are called with newobject)?
Ill appreciate any help
Ignacio Vivona
cocotapioca
 
Posts: 19
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Ignacio Vivona

Re: Singleton

Postby gfraiteur on Thu Jan 31, 2008 5:53 am

Actually, every time you add an aspect to a type (or a field or method of a type), you add a static field to this type: the aspect itself.

Therefore, every instance field of an aspect can be considered as a static field of the declaring type.

If you don't need to "remember" the value of that field between compile- and run-time, you should mark the field as [NonSerialized].

It is not currently possible to intercept calls to constructors, so I don't see how you could implement the Singleton pattern.

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 699
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Singleton

Postby cocotapioca on Thu Jan 31, 2008 1:56 pm

Ok, thanks for the reply, but how oyu use the JointPointind.InsteadOfNewObject?

Im thinking of doing something like this:
*mark the singleton type
*create a Intance getter and a field to store the sole instance
*scan all methods/constructors for the use of newobject+ctor for singleton types
*replace that by a call to get_Instance
Ignacio Vivona
cocotapioca
 
Posts: 19
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Ignacio Vivona

Re: Singleton

Postby gfraiteur on Thu Jan 31, 2008 2:44 pm

Ok, thanks for the reply, but how oyu use the JointPointind.InsteadOfNewObject?


It is available only from PostSharp Core, not from PostSharp Laos. So you should write your own abstract aspect.
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 699
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Singleton

Postby cocotapioca on Thu Jan 31, 2008 3:32 pm

Yes im using Postsharp.core directly, not laos. So you say i need to write my own abstract aspect, i assume that ther is not example about that. Ill dive into postsharp code.
When you use JointPointKind.InsteadOfNewObject? Is a Type o Method kind of joint point?
Have you use this feature? Can you show me some code?

Thanks for the quick reply.
Ignacio Vivona
cocotapioca
 
Posts: 19
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Ignacio Vivona

Re: Singleton

Postby gfraiteur on Thu Jan 31, 2008 3:40 pm

It is a method-level advice. You should specify the constructors to intercept in the operand, and null (=all methods) for the set of targets.

There is an example of how to create an aspect based on Laos: PostSharp.Samples.Librarian.

Good luck - you will have good time :geek:.
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 699
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Singleton

Postby cocotapioca on Thu Feb 07, 2008 2:32 am

Thanks for the reply.
I have a question, how do i get a MetadataDeclaration for the constructor .ctor from TypeDefDeclaration?
Ignacio Vivona
cocotapioca
 
Posts: 19
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Ignacio Vivona

Re: Singleton

Postby gfraiteur on Thu Feb 07, 2008 9:16 am

Something like;

typeDef.Methods.GetByName(".ctor")

Gel
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 699
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Singleton

Postby cocotapioca on Thu Feb 07, 2008 4:45 pm

That was my first try but typeDef.Methods.GetByName return an IEnumerable<MethodDefDelcaration> and operands is a IEnumarable<MetadataDeclaration>
And cant be casted.

Cheers.
Ignacio
Ignacio Vivona
cocotapioca
 
Posts: 19
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Ignacio Vivona

Next

Return to What To Do With PostSharp?


cron