4

I have an abstract class that other classes are inheriting this.

public abstract class GenericAccess<TEntity>
{
    public static IList<TEntity> GetObjectListFromArray(int[] IDs)
    {
        //Your help need is here.
    }
}

public class Doors : GenericAccess<DoorEntity>
{

}

public class DoorEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I need to create a generic method so when I can for example

IList<DoorEntity> Doors.GetObjectListFromArray(int[] {1,2,3,4,5});

it will return an IList having all the objects in it with the property Id loaded with the value passed. In the above example it will return a list with 5 items in the list with the Id of DoorEntity loaded.

3
  • You could use a Dictionary<int, TEntity> to do this. Commented Jun 1, 2011 at 19:01
  • 1
    where are the DoorEntity objects getting materialized from? Are they stored somewhere within the GenericAccess object? Are they being pulled from a database? Commented Jun 1, 2011 at 19:06
  • The DoorEntity is loaded from database using Nhibernate Fluent. Commented Jun 1, 2011 at 19:07

3 Answers 3

2

Use an interface or a base class...

With interface:

public abstract class GenericAccess<TEntity> where TEntity : IEntity, new()
{
    public static IList<TEntity> GetObjectListFromArray(int[] IDs)
    {
        return IDs.Select(id => new TEntity { Id = id }).ToList();
    }
}

public class Doors : GenericAccess<DoorEntity>
{

}

public class DoorEntity : IEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public interface IEntity
{
    int Id { get; set; }
    string Name { get; set; }
}

With a base class:

public abstract class GenericAccess<TEntity> where TEntity : Entity, new()
{
    public static IList<TEntity> GetObjectListFromArray(int[] IDs)
    {
        return IDs.Select(id => new TEntity { Id = id }).ToList();
    }
}

public class Doors : GenericAccess<DoorEntity>
{

}

public class DoorEntity : Entity
{

}

public abstract class Entity
{
    public int Id { get; set; }
    public string Name { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Please put your example in the answer, instead of behind a link!
That's worked great! Perfect! - Really interesting seeing your solution!
Thanks. I have inserted my example into the answer, instead of a Gist link, sorry about that.
1

I'm not sure if that's what you want but here goes:

return from int id in Ids
       select new TEntity (id);

You'll have to correct the definition of the GenericAccess class to add a constraint to the generic parameter as follow:

public abstract class GenericAccess<TEntity> where TEntity : class, new

Ok, based on your comments...

Use LINQ with NHibernate to get the entities something along the lines of:

return from int id in Ids
       select Session.Query (...).Where (x => x.Id === id);

Comments

0

your function can be like this

public static IList<TEntity> GetObjectListFromArray(int[] IDs)
{
    var r = new List<TEntity>();
    foreach (var item in IDs)
    {
        var obj = typeof(TEntity).Assembly.CreateInstance(typeof(TEntity).FullName);
        var p = obj.GetType().GetProperty("Id", System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
        if (p != null)
        {
            p.SetValue(obj, item, null);
            var m = r.GetType().GetMethod("Add");
            m.Invoke(r, new object[] { obj });
        }
    }
    return r;
}

}

and

    IList<DoorEntity> r = Doors.GetObjectListFromArray(new int[] {1,2,3,4,5});

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.