Generics


S

shapper

Hello,

I have the following class:

public class RoleResolver : ValueResolver<String[], IList<Role>> {
protected override IList<Role> ResolveCore(String[] source) {
IList<Role> roles = new List<Role>();
foreach (String r in source) {
roles.Add(new Role { Id = Int32.Parse(r) });
}
return roles;
} // ResolveCore
} // RoleResolver

How can I generalize this class to return any IList<T>?

I know that all T objects will have a property of type Id.

Thank You,
Miguel
 
Ad

Advertisements

P

Peter Duniho

Hello,

I have the following class:

public class RoleResolver : ValueResolver<String[], IList<Role>> {
protected override IList<Role> ResolveCore(String[] source) {
IList<Role> roles = new List<Role>();
foreach (String r in source) {
roles.Add(new Role { Id = Int32.Parse(r) });
}
return roles;
} // ResolveCore
} // RoleResolver

How can I generalize this class to return any IList<T>?

By replacing your use of "Role" with the type parameter "T", and adding
that to the name of the class:

public class RoleResolver<T> : ValueResolver(string[], IList<T>>
{
// etc.
}
I know that all T objects will have a property of type Id.

You can't declare a constraint for a single property. If you care about
that (and your code example suggests you do), you need two things: for all
types you expect to use to have a common base class with the property "Id"
that you can use in the constraint, as well as a parameterless constructor
so that you can create new instances of each class.

That might look something like this:

public class RoleResolver<T> : ValueResolver(string[], IList<T>> where
T : BaseObject, new()
{
protected override IList<T> ResolveCore(String[] source)
{
IList<T> roles = new List<T>();

foreach (String r in source)
{
roles.Add(new T { Id = Int32.Parse(r) });
}

return roles;
}
}

Pete
 
S

shapper

I have the following class:
  public class RoleResolver : ValueResolver<String[], IList<Role>> {
    protected override IList<Role> ResolveCore(String[] source) {
      IList<Role> roles = new List<Role>();
      foreach (String r in source) {
        roles.Add(new Role { Id = Int32.Parse(r) });
      }
      return roles;
    } // ResolveCore
  } // RoleResolver
How can I generalize this class to return any IList<T>?

By replacing your use of "Role" with the type parameter "T", and adding  
that to the name of the class:

    public class RoleResolver<T> : ValueResolver(string[], IList<T>>
    {
       // etc.
    }
I know that all T objects will have a property of type Id.

You can't declare a constraint for a single property.  If you care about  
that (and your code example suggests you do), you need two things: for all  
types you expect to use to have a common base class with the property "Id"  
that you can use in the constraint, as well as a parameterless constructor  
so that you can create new instances of each class.

That might look something like this:

    public class RoleResolver<T> : ValueResolver(string[], IList<T>> where  
T : BaseObject, new()
    {
       protected override IList<T> ResolveCore(String[] source)
       {
          IList<T> roles = new List<T>();

          foreach (String r in source)
          {
             roles.Add(new T { Id = Int32.Parse(r) });
          }

          return roles;
       }
    }

Pete

Thanks Pete ...

My problem was really the Id part .. I knew I could use T but wasn't
sure if it would be possible the Id part.

In this case I will create various classes because I can't have all
objects based on a common one since they come from the model and other
issues are involved.

Thanks,
Miguel
 
P

Peter Duniho

[...]
In this case I will create various classes because I can't have all
objects based on a common one since they come from the model and other
issues are involved.

Note that if you can declare the "Id" property as part of an interface all
the different classes implement, that would work too. Then the various
object types don't have to be related in type, but can still satisfy a
generic constraint that would let you use the "Id" property (they would
all still also need a parameterless constructor though).

Pete
 
B

Ben Voigt [C++ MVP]

You seem to be jumping through unnecessary hoops because you don't fully
understand the basic principles of OOP(s).

What is Object-oriented-programming?

(OOP) is a programming paradigm that uses "objects" and their interactions
to design applications and computer programs.

The key concepts of OOP are the following:
Class
Object
Instance
Method
Message passing
Inheritance
Abstraction
Encapsulation
Polymorphism
Decoupling

http://en.wikipedia.org/wiki/Object-oriented_programming

No matter what development platform Java, .Net or others OOP is OOP.

http://math.hws.edu/eck/cs124/downloads/OOP2_from_Univ_KwaZulu-Natal.pdf
http://www.blackwasp.co.uk/ObjectOrientedConcepts.aspx

Generics aren't OOP per se, they are actually a newer paradigm (that changes
many of the rules).
 
S

shapper

You know, you should get away from trying to manipulate Model objects so
much.

Maybe I was not clear but this is not a Data Model / Mapping Model
from the database.
The models I am talking about are the ViewModels in an ASP.NET MVC
application, e.g., UI Models.

I am using the class I mentioned in mapper to create this ViewModels.

I am sure I probably can create a mapping resolver common for all
them .. I am just looking at my code.
 
Ad

Advertisements

S

shapper

You know, you should get away from trying to manipulate Model objects so
much. There are other types of objects you can populate like base DTO or
a BO that you populate from a Model object and manipulate the DTO(s),
BO(s) which you can have a base DTO with child DTO()s  or base BO  with
child BO(s) that inherit from the base.

In real world applications using a Model, the DAL with the Model is
invoked to do simple CURD operations based on information passed to the
DAL in  Business Objects or Data Transfer Objects that have been
abstracted away from the Model and return to the Model via the DAL.

You seem to be jumping through unnecessary hoops because you don't fully
understand the basic principles of OOP(s).

What is Object-oriented-programming?

(OOP) is a programming paradigm that uses "objects" and their
interactions to design applications and computer programs.

The key concepts of OOP are the following:
Class
Object
Instance
Method
Message passing
Inheritance
Abstraction
Encapsulation
Polymorphism
Decoupling

http://en.wikipedia.org/wiki/Object-oriented_programming

No matter what development platform Java,  .Net or others  OOP is OOP..

http://math.hws.edu/eck/cs124/downl...w.blackwasp.co.uk/ObjectOrientedConcepts.aspx

What are design patterns?

Design patterns are recurring solutions to software design problems you
find again and again in real-world application development. Patterns are
about design and interaction of objects, as well as providing a
communication platform concerning elegant, reusable solutions to
commonly encountered programming challenges.

http://www.developer.com/design/art...atternsfor.net/Presentations.aspx?tid=3&cid=4

Thank you for the links ... They are useful
 
Ad

Advertisements

P

Paul

Along these lines........Generics has some things in common with C++
templates and these have been incorporated into OO development and OO
patterns for years so ignore some comments on here.

public abstract class ModelBase
{
private int id;
public Id ()
{ get....; set....; }
}


public class Resolver <T>: ValueResolver<String[], IList<T>> where T :
ModelBase, new()
{
protected override IList<T> ResolveCore(String[] source)
{
IList<T> listObject= new List<T>();
foreach (String r in source)
{
T obj = new T();
T.Id = Int32.Parse(r);
listObject.Add(obj); }
return listObject;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top