0

With BLToolkit, it's very easy to map from a stored procedures output to an object, but can it be done the other way? To go from an object to a stored procedures input, such that each of an objects properties becomes a parameter

I'd like to be able to do something like this:

[SprocName("sp_name")]
public abstract void InsertViaSproc(int param1, int param2, 
                                    SomeObject restOfParams);

public class SomeObject
{
    [MapField("param3")] int param3;
    [MapField("param4")] string param4;
}

with a stored procedure

CREATE PROCEDURE sp_name(
    @param1 int,
    @param2 int,
    @param3 int,
    @param4 varchar(50))
AS
--The rest

Is this possible with BLToolkit out of the box? Or would I have to modify the source to achieve this?

Andy Hunt
  • 1,053
  • 1
  • 11
  • 26

1 Answers1

0

Yep, it's possible.

Here is the code from our project:

 public abstract void Save(long userId, [Direction.InputOutput("id")] Email email);

    protected override string GetDefaultSpName(string typeName, string actionName)
    {
        return "fspEmail" + actionName;
    }

And T-SQL...

 CREATE PROCEDURE [fspEmailSave]
   @userId BIGINT,
   @name NVARCHAR(250),
   @subject NVARCHAR(255),
   @from NVARCHAR(255),
   @replyTo NVARCHAR(255),
   @forward NVARCHAR(255),
   @description NVARCHAR(300),
   @id BIGINT OUT
...

Email model in my case doesn't even have a MapField attributes:

public class Email
{
    public long Id { get; set; }

    public string Name { get; set; }        

    public string Description { get; set; }

    public string Subject { get; set; }

    public string From { get; set; }        

    public string ReplyTo { get; set; }

    public string Forward { get; set; }

    public string HtmlContent { get; set; }

    public string TextContent { get; set; }
}

As long as properties have same names as params in stored procedure(case insensitive), all stuff will be working correctly. Sure, you may apply MapField attributes to model properties and SprocName to your abstract method to ensure that if someone renamed them, nothing bad happens. But I personally hate those magic strings.

VirusX
  • 953
  • 1
  • 9
  • 20
  • I hoped something like this was possible! Could you please share your Email model with all of its mapping too? – Andy Hunt Jul 24 '12 at 15:10