3
I have a dictionary that serves as a type mapper with C#.
var Map = new Dictionary<Type, Type>()
{
{typeof(A1), typeof(A2)},
{typeof(B1), typeof(B2)},
{typeof(C1), typeof(C2)},
{typeof(D1), typeof(D2)}
};
I intend to use it as follows:
public virtual T Get<T>(int id) where T : CD.IDomainModel
{
using (var rep = new R.Rep())
{
var t = PegarEquavalencia(typeof(T));
var obj = rep.Get<t>(id);//Aqui aponta um erro no 't'
return (T)Activator.CreateInstance(typeof(T), obj);
}
}
public static Type PegarEquavalencia(Type type)
{
Type tipoEquivalente;
Map.TryGetValue(type, out tipoEquivalente);
return tipoEquivalente;
}
The problem is that I haven’t been able to use this Type Map yet to do this action. Basically what I want to do is pass a type A1, take the equivalent of it in the dictionary, in case A2, and use the type of A2 as a parameter for the generic type of the other method.
I am using Nhibernate, and the Rep class is a generic repository. The method Get<T>(object id)
is has the generic type to return the object I wish to pick up in the bank.
My solution has basically 3 projects:
- One for database mapping, Domain, Mapping (xml) and Repository;
- One to UI;
- And the other for the business logic of the application.
In the project that encapsulates business logic, I own objects that will be used for data exchange with the UI.
I made a dictionary as said above to map objects for UI communication with Mapping objects with ORM.
What I intend to do is ask for an object of type A1, of the interface, then an intermediate method will check its equivalent in the dictionary, will search the object with the equivalent type, A2, in the database, and will return the object A1 that receives with parameter the object A2;
I don’t understand. What is the parameter for
Id
?– Leonel Sanches da Silva
The @Ciganomorrisonmendez is right:" to which the
Id
?" You could use a sample code that works?– Reiksiel