I've created a wrapper in my data access for joins in OrmLite.
I'm now getting the exception:
System.Exception : Expression should have only one column
All of my entities have a base class of BaseEntity.
JoinType is just a facade to contain the column, selection and where of a join.
My wrapper is as follows:
public IEnumerable<TResultEntity> Join<TResultEntity>(IList<JoinType<BaseEntity, BaseEntity>> joins)
{
var result = new List<TResultEntity>();
if (joins != null && joins.Any())
{
var joinBuilder = new JoinSqlBuilder<T, BaseEntity>();
foreach (var join in joins)
{
joinBuilder = joinBuilder.Join(join.LeftColumn, join.RightColumn, join.LeftSelection, join.RightSelection, join.LeftWhere, join.RightWhere);
}
var connection = this.DataConnection;
using (connection)
{
var joinSql = joinBuilder.SelectDistinct().ToSql();
result = connection.SqlList<TResultEntity>(joinSql);
}
}
return result;
}
Doing the same thing, without the list seems to work:
public IEnumerable<TResultEntity> Join<TLeftTable1, TRightTable1, TLeftTable2, TRightTable2, TResultEntity>(
JoinType<TLeftTable1, TRightTable1> join1,
JoinType<TLeftTable2, TRightTable2> join2)
where TLeftTable1 : BaseEntity
where TRightTable1 : BaseEntity
where TLeftTable2 : BaseEntity
where TRightTable2 : BaseEntity
EDIT - I'm testing using the below call:
// Act
var join1 = new JoinType<AnswerEntity, UserSurveyStateEntity>(
l => l.OwnerId,
r => r.UserId,
x => new { UserId = x.OwnerId, x.QuestionId, AnswerId = x.Id, x.AnswerValue });
var join2 = new JoinType<SurveyEntity, UserSurveyStateEntity>(
l => l.Id,
r => r.SurveyInstanceId,
x => new { SurveyId = x.Id, SurveyName = x.Name, x.StatusValue },
null,
null,
x => x.StatusValue == (int)UserSurveyStatus.Complete);
var joins = new List<JoinType<BaseEntity, BaseEntity>>();
joins.Add(join1.As<JoinType<BaseEntity, BaseEntity>>());
joins.Add(join2.As<JoinType<BaseEntity, BaseEntity>>());
var result = dataAccess.Join<AnswerEntity>(joins).ToList();
EDIT - Now seeing the use case, the error is related to casting to the base type and the builder storing more than one column selector for the concrete BaseEntity. Consider adding an abstract JoinType class, and modifying the JoinType class so it will apply the join for the builder.
For example:
public class Entity
{
public string Id { get; set; }
}
public class Foo
: Entity
{
public string Value { get; set; }
}
public class Bar
: Entity
{
public string FooId { get; set; }
public string Value { get; set; }
}
public abstract class JoinType
{
public abstract JoinSqlBuilder<TNew, TBase> ApplyJoin<TNew, TBase>(
JoinSqlBuilder<TNew, TBase> bldr);
}
public class JoinType<TSource, TTarget>
: JoinType
{
private Expression<Func<TSource, object>> _sourceColumn;
private Expression<Func<TTarget, object>> _destinationColumn;
private Expression<Func<TSource, object>> _sourceTableColumnSelection;
private Expression<Func<TTarget, object>> _destinationTableColumnSelection;
private Expression<Func<TSource, bool>> _sourceWhere;
private Expression<Func<TTarget, bool>> _destinationWhere;
public JoinType(Expression<Func<TSource, object>> sourceColumn,
Expression<Func<TTarget, object>> destinationColumn,
Expression<Func<TSource, object>>
sourceTableColumnSelection = null,
Expression<Func<TTarget, object>>
destinationTableColumnSelection = null,
Expression<Func<TSource, bool>> sourceWhere = null,
Expression<Func<TTarget, bool>> destinationWhere =
null)
{
this._sourceColumn = sourceColumn;
this._destinationColumn = destinationColumn;
this._sourceTableColumnSelection = sourceTableColumnSelection;
this._destinationTableColumnSelection =
destinationTableColumnSelection;
this._sourceWhere = sourceWhere;
this._destinationWhere = destinationWhere;
}
public override JoinSqlBuilder<TNew, TBase> ApplyJoin<TNew, TBase>(
JoinSqlBuilder<TNew, TBase> bldr)
{
bldr.Join(_sourceColumn,
_destinationColumn,
_sourceTableColumnSelection,
_destinationTableColumnSelection,
_sourceWhere,
_destinationWhere);
return bldr;
}
}
public class FooBar
{
[References(typeof(Foo))]
public string FooId { get; set; }
[References(typeof(Bar))]
public string BarId { get; set; }
[References(typeof(Foo))]
public string FooValue { get; set; }
[References(typeof(Bar))]
public string BarValue { get; set; }
}
/*
This join accomplishes the same thing, but just returns the SQL as a string.
*/
public string Join<TResultEntity,TBase>(IList<JoinType>joins)
{
var result = new List<TResultEntity>();
if (joins != null && joins.Any())
{
var joinBuilder = new JoinSqlBuilder<TResultEntity, TBase>();
foreach (var joinType in joins)
{
//call the apply join, and the join type will know the valid types
joinBuilder = joinType.ApplyJoin(joinBuilder);
}
return joinBuilder.SelectDistinct().ToSql();
}
return null;
}
[TestMethod]
public void TestMethod1()
{
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider;
var joins = new List<JoinType>();
var jointype1 = new JoinType<Bar, FooBar>(
bar => bar.Id,
bar => bar.BarId,
bar => new { BarId = bar.Id, BarValue = bar.Value }
);
joins.Add(jointype1);
var joinType2 = new JoinType<Foo, FooBar>(
foo => foo.Id,
bar => bar.FooId,
foo => new { FooId = foo.Id, FooValue = foo.Value}
);
joins.Add(joinType2);
var str = Join<FooBar, Bar>(joins);
}
Old Answer - still relevant to the error
This error is caused by your selector join.LeftColumn or your join.RightColumn containing two selectors. Make sure they only contain a single one.
I was able to reproduce the error with the following test:
public class Entity
{
public string Id { get; set; }
}
public class Foo
: Entity
{
public string Value { get; set; }
}
public class Bar
: Entity
{
public string FooId { get; set; }
public string Value { get; set; }
}
public class FooBar
{
[References(typeof(Foo))]
public string FooId { get; set; }
[References(typeof(Bar))]
public string BarId { get; set; }
[References(typeof(Foo))]
public string FooValue { get; set; }
[References(typeof(Bar))]
public string BarValue { get; set; }
}
[TestMethod]
public void TestMethod1()
{
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider;
var bldr = new JoinSqlBuilder<FooBar,Bar>();
bldr = bldr.Join<FooBar, Bar>(
bar => bar.BarId,
bar => new { Id1 = bar.Id, Id2 = bar.Id},//<-- this should only contain a single member
bar => new { BarId =bar.BarId },
bar => new { BarId = bar.Id, BarValue = bar.Value},
bar => bar.BarId != null,
bar => bar.Id != null
);
var str = bldr.SelectDistinct().ToSql();
}
Related
i want register custo generic type like default generic type that register in autoMaper (like List, Array) in AutoMappper.
i have one generic type in project this Code :
class PagedResult<T>
{
public List<T> PageOfResults { get; set; }
public int TotalItems { get; set; }
}
and Dto Class is:
class StudentDto
{
public int ID { get; set; }
public string Name { get; set; }
}
and VM Model is :
class StudentVM
{
public int ID { get; set; }
public string Name { get; set; }
}
and service class:
class MyServie
{
public PagedResult<StudentDto> Swap()
{
var test2 = new PagedResult<StudentDto>();
test2.PageOfResults = new List<StudentDto>();
test2.PageOfResults.Add(new StudentDto() { ID = 10, Name = "Ten" });
test2.TotalItems = 10;
return test2;
}
}
i want use from AutoMapper Object Manager for register PagedResult<> in automapper but i can not do this
var allMappers = MapperRegistry.AllMappers();
MapperRegistry.AllMappers = () => new IObjectMapper[]{
new IdentifiableMapper(),
}.Concat(allMappers);
var service = new MyServie();
PagedResult<StudentDto> pageableStudentDto = service.Swap();
Mapper.CreateMap<StudentDto, StudentVM>();;
PagedResult<StudentVM> vm = Mapper.Map<PagedResult<StudentDto>, PagedResult<StudentVM>>(pageableStudentDto);
and implement of
public class PageOfMapper : IObjectMapper
{
public bool IsMatch(ResolutionContext context)
{
return typeof(PagedResult<>).IsAssignableFrom(context.SourceType.GetGenericTypeDefinition()) &&
typeof(PagedResult<>).IsAssignableFrom(context.DestinationType.GetGenericTypeDefinition());
//return true;
}
public object Map(ResolutionContext context, IMappingEngineRunner mapper)
{
// please help me in this code for map ******************
return null;
}
}
Consider this Example
public class FooWrapper
{
public FooWrapper() { }
public Foo FooObject { get; set; }
public Bar BarObject { get; set; }
}
public IEnumerable<FooWrapper> ListFoosWithBars(int userID)
{
IEnumerable<Bar> tempBar = ListBarsByUserID(userID);
IEnumerable<FooWrapper> results = (
from f in _entities.FooSet
join b in tempBar on f.ID equals b.foos.ID
select new FooWrapper
{
FooObject = f,
BarObject = b
});
return results;
}
what if my Foo type class has Properties like
public class Foo(){
FProperty1{get; set;}
FPorperty2{get; set;}
}
public class Bar(){
BProperty1{get; set;}
BProperty2{get; set;}
}
and now i want to initialize my object in query like this
select new FooWrapper
{
FooObject.FProperty1 = f,
BarObject.BProperty2 = b
});
can I do this?
How will this work?
What you want is:
select new FooWrapper
{
FooObject = new Foo { FProperty1 = f },
BarObject = new Bar { BProperty2 = b }
});
Hello. I have a list that looks like this one:
public class PagedList<T> : List<T>
{
public PagedList(IEnumerable<T> collection) : base(collection)
{ }
public int TotalItems { get; set; }
public int CurrentPage { get; set; }
public int PageSize { get; set; }
//some other properties
}
and used in repository for paging
public PagedList<TEntity> GetPaged(int page)
{
var pagedEntities = some_query;
return pagedEntities.AsPagedList(totalResults, page, pageSize);
}
The same PagedList is also used in asp mvc view models for paging.
Is it possible to map this collections using Automapper with all the properties TotalItems/CurrentPage/... ?
PagedList<DbItem> dbItems = _repository.GetPages(page);
var viewItems = new PagedList<SomeItemView>();
Mapper.Map(dbItems , viewItems);
Tahnk You !
This worked for me. Are you looking for something more generic?
public class DbItem
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ViewItem
{
public int Id { get; set; }
public string Name { get; set; }
}
public class PagedList<T>: List<T>
{
public int TotalItems { get; set; }
public int CurrentPage { get; set; }
public int PageSize { get; set; }
}
class Program
{
static void Main(string[] args)
{
MapItems();
}
public static void MapItems()
{
Mapper.CreateMap<DbItem, ViewItem>();
Mapper.CreateMap<PagedList<DbItem>, PagedList<ViewItem>>()
.AfterMap((s, d) => Mapper.Map<List<DbItem>, List<ViewItem>>(s, d));
var dbList = new PagedList<DbItem>
{
new DbItem {Id = 1, Name = "a"},
new DbItem {Id = 2, Name = "b"}
};
dbList.TotalItems = 2;
dbList.CurrentPage = 1;
dbList.PageSize = 10;
var viewList = Mapper.Map<PagedList<DbItem>, PagedList<ViewItem>>(dbList);
Console.WriteLine(viewList.TotalItems);
Console.WriteLine(viewList.CurrentPage);
Console.WriteLine(viewList.PageSize);
Console.WriteLine(viewList[0].Id + " " + viewList[0].Name);
Console.WriteLine(viewList[1].Id + " " + viewList[1].Name);
Console.ReadLine();
}
}
What you need is a custom type converter
public class PagedListConverter<TIn, TOut> : ITypeConverter<IPagedList<TIn>, IPagedList<TOut>>
{
public IPagedList<TOut> Convert(AutoMapper.ResolutionContext context)
{
var source = (IPagedList<TIn>)context.SourceValue;
var mapped = Mapper.Map<IList<TOut>>(source);
return new StaticPagedList<TOut>(mapped, source.GetMetaData());
}
}
Usage:
Mapper.CreateMap<IPagedList<Company>, IPagedList<CompanyViewModel>>().ConvertUsing<PagedListConverter<Company, CompanyViewModel>>();
For those who have faced the similar problem recently, and as an update to NoPyGod's answer, you can achieve the general mapping using ITypeConverter. According to the official documentation:
AutoMapper also supports open generic type converters with any number of generic arguments:
var configuration = new MapperConfiguration(cfg =>
cfg.CreateMap(typeof(Source<>), typeof(Destination<>)).ConvertUsing(typeof(Converter<,>)));
The closed type from Source will be the first generic argument, and the closed type of Destination will be the second argument to close Converter<,>.
So the custom type converter would be:
private class Converter<TSource, TDestination>
: ITypeConverter<PagedList<TSource>, PagedList<TDestination>>
{
public PagedList<TDestination> Convert(
PagedList<TSource> source,
PagedList<TDestination> destination,
ResolutionContext context) =>
new PagedList<TDestination>(
context.Mapper.Map<List<TSource>, List<TDestination>>(source));
/* Additional settings comes here. */
}
And then register it:
this.CreateMap(typeof(PagedList<>), typeof(PagedList<>)).ConvertUsing(typeof(Converter<,>));
I cannot seem to map a custom type using the latest autoMapper library in c#. I am using the sample from the sample app but added a custom type. Both classes are similar just the code names are different. I do want this to be a "GlobalTypeConverter" as it used a few times.
The error is at validation:
//Error: The following 1 properties on TestApp.Form1+Destination are not mapped:
mycode
Add a custom mapping expression, ignore, or rename the property on TestApp.Form1+Source.
public class Source
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
public standardCoding stdcode { get; set; }
}
public class Destination
{
public int Value1 { get; set; }
public DateTime Value2 { get; set; }
public Type Value3 { get; set; }
public myClass.code mycode { get; set; }
}
public class DateTimeTypeConverter : TypeConverter<string, DateTime>
{
protected override DateTime ConvertCore(string source)
{
return System.Convert.ToDateTime(source);
}
}
public class TypeTypeConverter : TypeConverter<string, Type>
{
protected override Type ConvertCore(string source)
{
Type type = Assembly.GetExecutingAssembly().GetType(source);
return type;
}
}
public class standardCodingConverter : TypeConverter<standardCoding, myClass.code>
{
protected override myClass.code ConvertCore(standardCoding source)
{
var result = new myClass.code();
result.CodingSystem = source.StandardCodingSystem;
result.Description = source.StandardCodeDescription;
result.value = source.StandardCode;
return result;
}
}
public void btnAutoMapperTest_Click(object sender, EventArgs e)
{
Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
Mapper.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
Mapper.CreateMap<standardCoding, myClass.code>().ConvertUsing(new standardCodingConverter());
Mapper.CreateMap<Source, Destination>();
Mapper.AssertConfigurationIsValid();
var newcode = new standardCoding();
newcode.StandardCode = "123";
newcode.StandardCodeDescription = "my desc";
newcode.StandardCodingSystem = "CodeSys";
var source = new Source
{
Value1 = "5",
Value2 = "01/01/2000",
Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination",
stdcode = newcode
};
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "cds_dt")]
public partial class standardCoding
{
private string standardCodingSystemField;
private string standardCodeField;
private string standardCodeDescriptionField;
/// <remarks/>
public string StandardCodingSystem
{
get
{
return this.standardCodingSystemField;
}
set
{
this.standardCodingSystemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "token")]
public string StandardCode
{
get
{
return this.standardCodeField;
}
set
{
this.standardCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "token")]
public string StandardCodeDescription
{
get
{
return this.standardCodeDescriptionField;
}
set
{
this.standardCodeDescriptionField = value;
}
}
}
I figured it out by looking online (sample code "CustomValueResolvers.cs" did not work for me).
My one incorrect line was:
...
.ForMember(src => src.DiagnosisCode, opt => opt.ResolveUsing() .FromMember(r => r.DiagnosisProcedureCode))
After working with AutoMapper I came across ValueInjecter on this site. I am trying it out but I am stuck on what is probably a very simple scenario.
But before I dig into the code sample, does anyone know if ValueInjecter works in a Medium-Trust web environment? (Like Godaddy?)
Ok, onto the code! I have the following models:
public class NameComponent
{
public string First { get; set; }
public string Last { get; set; }
public string MiddleInitial { get; set; }
}
public class Person
{
public NameComponent Name { get; set; }
}
that I want to map to the following DTO:
public class PersonDTO : BaseDTO
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { NotifyPropertyChanged(() => FirstName, ref _firstName, value); }
}
private string _middleInitial;
public string MiddleInitial
{
get { return _middleInitial; }
set { NotifyPropertyChanged(() => MiddleInitial, ref _middleInitial, value); }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { NotifyPropertyChanged(() => LastName, ref _lastName, value); }
}
}
So when I want to Map from Model to DTO I need a Model.Name.First -> DTO.FirstName
and when going from DTO to Model I need FirstName -> Name.First. From my understanding this is not a simple Flatten/UnFlatten, because the words also reverse themselves, ie: FirstName <--> Name.First. So First and Last names could use the same kind of rule, but what about MiddleInitial? Model.Name.MiddleInitial -> DTO.MiddleInitial.
I see there are some plugins, but none of them seem to do what I would want. Has anyone else come across this scenario?
the basic idea is that I match the Name with the FirstName, I take this as a pivot point, and in the method that usually sets the value to just one (FirstName) property I set it to 3 properties - that's for the FromNameComp
in the ToNameComp i match the same properties but I take the value from 3 and create one and set it
public class SimpleTest
{
[Test]
public void Testit()
{
var p = new Person { Name = new NameComponent { First = "first", Last = "last", MiddleInitial = "midd" } };
var dto = new PersonDTO();
dto.InjectFrom<FromNameComp>(p);
Assert.AreEqual(p.Name.First, dto.FirstName);
Assert.AreEqual(p.Name.Last, dto.LastName);
Assert.AreEqual(p.Name.MiddleInitial, dto.MiddleInitial);
var pp = new Person();
pp.InjectFrom<ToNameComponent>(dto);
Assert.AreEqual(dto.LastName, pp.Name.Last);
Assert.AreEqual(dto.FirstName, pp.Name.First);
Assert.AreEqual(dto.MiddleInitial, pp.Name.MiddleInitial);
}
public class FromNameComp : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name == "Name" && c.SourceProp.Type == typeof(NameComponent)
&& c.TargetProp.Name == "FirstName"
&& c.SourceProp.Value != null;
}
protected override object SetValue(ConventionInfo c)
{
dynamic d = c.Target.Value;
var nc = (NameComponent)c.SourceProp.Value;
//d.FirstName = nc.First; return nc.First does this
d.LastName = nc.Last;
d.MiddleInitial = nc.MiddleInitial;
return nc.First;
}
}
public class ToNameComponent : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.TargetProp.Name == "Name" && c.TargetProp.Type == typeof(NameComponent)
&& c.SourceProp.Name == "FirstName";
}
protected override object SetValue(ConventionInfo c)
{
dynamic d = c.Source.Value;
var nc = new NameComponent { First = d.FirstName, Last = d.LastName, MiddleInitial = d.MiddleInitial };
return nc;
}
}
public class NameComponent
{
public string First { get; set; }
public string Last { get; set; }
public string MiddleInitial { get; set; }
}
public class Person
{
public NameComponent Name { get; set; }
}
public class PersonDTO
{
public string FirstName { get; set; }
public string MiddleInitial { get; set; }
public string LastName { get; set; }
}
}
But before I dig into the code sample,
does anyone know if ValueInjecter
works in a Medium-Trust web
environment? (Like Godaddy?)
it doesn't use reflection.emit so it should work