0

I have 5 different classes that all inherit from BaseEntity. I would like to create a new model class that will store information needed about one of these 5 classes as well as other identifiers.

When I retrieve the data for this new model from the database, all I get is a string with the class type along with an integer that represents which entry I can reference from the database.

For example, if I retrieve Id = 2, Type = "BaseBall". That means I will have need to use my BaseBallService to fetch the entry where Id == 2. If it happens to be Id = 2, Type = "BasketBall", then I will use BasketBallService.

Currently the only solution I can think of is it to have a bunch of if statements that evaluate the 'type' string. Depending on if the type matches a valid type (BaseBall, FootBall, BasketBall, etc.) then that object is returned.

Is there a way to easily do this without the need to define all 5 types in the model definition and stringing if or statements to identify this?

I hope I have identified the problem clearly enough. Let me know if any additional information is needed. I haven't written any code for this yet. I am merely trying to analyze the problem and form a solution.

4

2 回答 2

1

You may try using Dictionary, e.g.

  Dictionary<String, BaseEntry> types = new Dictionary<String, BaseEntry>() {
    {"BaseBall", new BaseBallService()},
    {"BasketBall", new BasketBallService()},
    ...
  }

  ...
  var value = types["BaseBall"].GetId(2);
于 2013-06-24T16:32:25.463 回答
1

I would just add a global enum at the project or solution level to store types. That way if you wish to add to it later you may without breaking any existing code as it is detached. But this may keep it well typed and thus demand a type that is listed from the end user or application. I did a simple console app to show this. You may apply the enum to any class not just a generic though. I also implement a return method to narrow down the return lists to show how I can get lists of my lists easier.

public enum types
    {
        Type1,
        Type2,
        Type3
    }

    public class GenericListing
    {
        public string Description { get; set; }
        public types Type { get; set; }
    }

    class Program
    {
        public static List<GenericListing> GetTypeListing(List<GenericListing> aListings, types aTypes)
        {
            return aListings.Where(x => x.Type == aTypes).ToList();
        }

        static void Main(string[] args)
        {
            var stuff = new List<GenericListing>
                {
                    new GenericListing {Description = "I am number 1", Type = types.Type1},
                    new GenericListing {Description = "I am number 2", Type = types.Type2},
                    new GenericListing {Description = "I am number 3", Type = types.Type3},
                    new GenericListing {Description = "I am number 1 again", Type = types.Type1},
                };


            string s = "";

            GetTypeListing(stuff, types.Type1)  // Get a specific type but require a well typed input.
                .ForEach(n => s += n.Description + "\tType: " + n.Type + Environment.NewLine);

            Console.WriteLine(s);

            Console.ReadLine();
        }
    }
于 2013-06-24T16:45:51.020 回答