1

Is there a simple way to store methods in an EF table, akin to inheritance?

For example, I have a table representing apps on a portal page (kind of like your basic iphone or android home screen). I'd like to display a number by each app showing how many notifications the app has. However, the method to get the number of notifications varies greatly depending on the app.

My current solution is to just have a class containing all the methods I need, and then switch based on the app name. Is there a better way?

4
  • 2
    You want to store code in a database field???? Commented Aug 22, 2012 at 19:42
  • 3
    I don't really get what the database has to do with it, but in those scenarios you should really create a unified interface. Then each app should implement that interface, possibly via an adapter. Commented Aug 22, 2012 at 19:46
  • @CodingGorilla Not necessarily. This question makes it seem like I might be able to do this with partial classes. Just wondering what the options are. Commented Aug 22, 2012 at 19:47
  • @AlbinSunnanbo I also have a table of users with which I need a many to many relationship with to determine permissions to apps. In addition, I want to be able to add new apps through the UI. Commented Aug 22, 2012 at 19:49

1 Answer 1

1

Assuming that your method to obtain the notifications is parameterless, you could store the method name in a column in that table and invoke it via reflection. For this example we'll call the column/property NotificationMethodName.

//this would go in your entity class 
public int GetNotificationCount()
{    
   MethodInfo mi = typeof(HelperClass).GetMethod(this.NotificationMethodName);    
   return (int)mi.Invoke(this, null); 
}

public class HelperClass
{
  //your class that currently has all the methods to get notification count
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.