Anupam- / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

Request to add support for ISupportInitialize Interface or similar #178

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Would it be possible to add support for something along the lines of the 
ISupportInitialize Interface 
(http://msdn.microsoft.com/en-us/library/system.componentmodel.isupportinitializ
e(v=vs.110).aspx)?

When mapping our existing business entities with Dapper, we need a way to 
perform an action on our entity object when the object is created prior to any 
of the data being populated.

In other words, something along the lines of:

1. Existing: Dapper news up an instance of an object.
2. New: Perform action against new object, i.e., ISupportInitialize.BeginInit.
3. Existing: Fill the object with data.
4. New: Perform action against populated object, i.e., 
ISupportInitialize.EndInit.

So, if the object implements ISupportInitialize, then steps 2 and 4 would be 
performed, otherwise they'd be ignored.

Thanks!

Original issue reported on code.google.com by kind...@gmail.com on 9 Jun 2014 at 9:32

GoogleCodeExporter commented 8 years ago
Standard before-deserialize callbacks are supported, as are constructors and 
custom factories. Would any of those suffice?

Original comment by marc.gravell on 9 Jun 2014 at 9:35

GoogleCodeExporter commented 8 years ago
Can you point me to a sample of using the before-deserialize callback? And/or a 
custom factory? One of those may work. Thanks!

Original comment by kind...@gmail.com on 9 Jun 2014 at 10:26

GoogleCodeExporter commented 8 years ago
Callbacks are basically the same as WCF, although alternative signatures are 
also supported. But: 
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeseriali
zingattribute(v=vs.110).aspx

Original comment by marc.gravell on 9 Jun 2014 at 10:34

GoogleCodeExporter commented 8 years ago
Hi Mark, thanks for the quick response. I'm not entirely sure how to set up the 
callbacks with Dapper. Does there need to be some setup for Dapper before the 
serialization attributes are recognized? 

In the following test, the 4 strings are never populated in the methods 
decorated with the serialize attributes:

public class TypeWithSeralizationCallbacks
{
    public int X { get; set; }
    public int Y { get; set; }
    public string OnSerialize { get; set; }
    public string OnSerialized { get; set; }
    public string OnDeSerialize { get; set; }
    public string OnDeSerialized { get; set; }

    [OnSerializing]
    internal void OnSerializingMethod( StreamingContext context )
    {
        OnSerialize = "OnSerialize";
    }

    [OnSerialized]
    internal void OnSerializedMethod( StreamingContext context )
    {
        OnSerialized = "OnSerialized";
    }

    [OnDeserializing]
    internal void OnDeserializingMethod( StreamingContext context )
    {
        OnDeSerialize = "OnDeSerialize";
    }

    [OnDeserialized]
    internal void OnDeserializedMethod( StreamingContext context )
    {
        OnDeSerialized = "OnDeSerialized";
    }

    public void Print()
    {
        Console.WriteLine( "X: {0}", X );
        Console.WriteLine( "Y: {0}", Y );
        Console.WriteLine( "OnSerialize: {0}", OnSerialize ?? "null" );
        Console.WriteLine( "OnSerialized: {0}", OnSerialized ?? "null" );
        Console.WriteLine( "OnDeSerialize: {0}", OnDeSerialize ?? "null" );
        Console.WriteLine( "OnDeSerialized: {0}", OnDeSerialized ?? "null" );

    }
}

static void Main()
{
    var conn = GetOpenConnection();

    var obj = conn.Query<TypeWithSeralizationCallbacks>( "select 11 X, 22 Y" ).Single();

    obj.Print();

    Console.WriteLine( "\n\n--done--" );
    Console.ReadLine();
}

Original comment by kind...@gmail.com on 10 Jun 2014 at 12:01

GoogleCodeExporter commented 8 years ago
Ah, dammit - my bad. Sorry, I misread the email title, and was confused that we 
were talking about a different library. Entirely my fault - sorry. Let me 
investigate. No, dapper doesn't offer anything here.

Original comment by marc.gravell on 10 Jun 2014 at 7:20

GoogleCodeExporter commented 8 years ago
Hi Mark, bummer. What are your thoughts on extending dapper to support this 
type of scenario? For us, this is a must-have to get dapper integrated into our 
application. Hey, no pressure :)

Original comment by kind...@gmail.com on 10 Jun 2014 at 1:10

GoogleCodeExporter commented 8 years ago
That's about this likely: 
https://github.com/StackExchange/dapper-dot-net/commit/88766056c8788190fcb5ad5b0
fc525005d70e281

Original comment by marc.gravell on 10 Jun 2014 at 1:43

GoogleCodeExporter commented 8 years ago
Marc, (not Mark, sorry about that earlier)... That's awesome! We'll dive into 
this today. Thanks!

Original comment by kind...@gmail.com on 10 Jun 2014 at 3:04

GoogleCodeExporter commented 8 years ago

Original comment by marc.gravell on 6 Aug 2014 at 3:40