0
votes

Let's say I have the following model:

class Foo
{
   public delegate void Bar();
   public Bar MyBar { get; set; }
}

And I would like to map this to my entity:

class FooEntity
{
   public delegate void Bar();
   public Bar MyBar { get; set; }
}

How is this done? I have tried using Automapper:

void ConfigureMappings()
{
    // how else do I need to configure this?
    Mapper.CreateMap<Foo, FooEntity>();
    Mapper.CreateMap<Foo.Bar, FooEntity.Bar>(); // is this right?
}

It is important for me to have data drive this delegate, since there are hundreds of different methods I need to be able to switch between depending on the value of MyBar. Any thoughts?

Edit: OK, so this no longer throws an exception when mapping from model to entity after moving the delegate out of the scope of each class:

public delegate void Bar(object obj);

public class Foo
{
   public Bar MyBar { get; set; }
}

public class FooEntity
{
   public Bar MyBar { get; set; }
}

Now I receive this: error 3004: Problem in mapping fragments starting at line 6:No mapping specified for properties FooEntity.MyBar in Set Foos. An Entity with Key (PK) will not round-trip when: Entity is type [Project.MyDbContext.FooEntity].

My database context code looks like this:

class MyDbContext : DbContext
{
    DbSet<FooEntity> Foos { get; set; }
}

And I save this to the database:

public static class Test()
{
   var foo = new Foo { MyBar = MyFancyDelegate };
   using (var context = new MyDbContext())
   {
       context.Foos.Add(Mapper.Map<FooEntity>(foo));
       context.SaveChanges();
   }
}

public static void MyFancyDelegate(object obj)
{
   Console.Write("test");
}

Is my problem just in what I am trying to do? Is it not possible to save delegate methods to a SQL table?

2
now you cannot save a function (or a delegate) in s SQL table - at worst this would mean serializing all of your program(code) and memory(state) at this exact point in timeRandom Dev
so the solution is obviously to think what kind of data you need to recreate your Bar and serialize this into your SQL table ;)Random Dev
The problem is that I need the delegate to potentially do a great number of very different things. I'm trying to avoid having a class devoted entirely to storing static methods for this purpose, but I don't see a way around it if I can't store them in SQL.Kjata30
you will have to find a way sorry - as I said: your delegates could be bound to all kinds of closures and stuff and to be honest I have no clue why you even want to save thisRandom Dev
If you're going to downvote the question, please explain why you've done so.Kjata30

2 Answers

1
votes

You could replace your delegate with a string which could contain the code for the delegate method. And then add a "get only property" or a method that converts the string property into a c# method using Roslyn.

1
votes

As mentioned in comments, it's not possible to store a delegate in SQL in the way I am attempting.

Instead, there are two approaches that can be used to solve the problem:

  • Serialize the delegate pointer. The byte[] object can then be converted into a string and stored. When the value is retrieved, it can be converted back into a byte[] object (conversion using Convert.ToBase64String and Convert.FromBase64String). The advantage of this approach is that you don't need any sort of on-the-fly compilation of code; you're just finding a pointer to code you already have access to. The disadvantage of course is that your pointer will break if you change anything about the method's signature.

  • Store the code text itself as a string then compile it on the fly, using System.CodeDom or some other compilation library. The advantage to this approach is that what you see is what you get; you're able to view the code in storage and have the ability to manipulate it outside of the context of your application. The drawbacks: your code cannot reference objects outside if its own scope, and you lose any security in knowing that the code will actually run properly (since it is now stored and accessed outside of your source control).