2

Is there a generics solution for the following code?

public static int SaveReorder(IList<int> listItems)
    {
        int result = 0;
        int order = 1;
        Entity1 db = null;

        using (ObjectContext context = new ObjectContext())
        {
            foreach (int id in listItems)
            {
                db = Get(context, id);
                db.Order = order;
                context.SaveChanges();
                order += 1;
            }
            result = 1;
        }
        return result;
    }

listItems contains an ordered sequence of identitykeys. Entity1 is one of the EntityObjects from our EDM. Get(...) is a custom method in the same class to get an EntityObject on basis of the current ObjectContext and by Id.

We want a generic solution for this implementation so we can apply this for several EntityObjects, where the property 'Order' is a common property for all the EntityObjects. Is this possible?

3 Answers 3

1

Two options come to mind, as Akash has already suggested:

  • Either let the Entities implement an Interface with an 'Order' Property:

    interface IEntityOrder { int Order { get; set; } }

    partial class Entity1 : EntityObject { }

    partial class Entity1 : IEntityOrder { public int Order { get; set; } }

  • Or use reflection to set the value of the 'Order' Property (or FieldInfo if it's a field):

    PropertyInfo pi = db.GetType().GetProperty("Order");

    pi.SetValue(db, newValue, null);

Sign up to request clarification or add additional context in comments.

Comments

1

No, however in future C# 4.0 using dynamic keywords you can do that.

Implementing Interface with Order Property

Currently you can have an interface with order property implemented by each class, I am not sure how to do this in EDM, but it shouldnt be difficult.

We get this kind of problem a lot, thats why C# is coming with dynamic type, we rely on either interfaces or reflection.

Comments

0

OK. Thanx all for your answers. Here's my solution.

public static int SaveReorder<T>(IList<int> listItems) where T : EntityObject
    {
        int result = 0;
        int volgorde = 1;
        T entityObject = null;

        using (vgplannewEntities objectContext = new vgplannewEntities())
        {
            try
            {
                foreach (int id in listItems)
                {
                    entityObject = objectContext.GetEntityByKey<T>(id, new String[] { });
                    PropertyInfo pi = entityObject.GetType().GetProperty("Volgorde");
                    pi.SetValue(entityObject, volgorde, null);
                    objectContext.SaveChanges();
                    volgorde += 1;
                }
                result = 1;
            }
            catch
            {
                result = 0;
            }
        }
        return result;
    }

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.