I have a class that looks like this
public class TableMapper<TSource> { .... .... public TableMapper(IQueryExtractor queryExtractor, string tableAliasPrefix = null) { QueryExtractor = queryExtractor; TableAliasPrefix = tableAliasPrefix; } .... .... } I create an instance of this class from within another class like so
public class CustomerMapper : ReportTemplate { private readonly IQueryExtractor QueryExtractor; private readonly TableMapper<Customer> customerMapper; private readonly TableMapper<Client> clientMapper; public CustomerMapper(DbContext context) : base(new QueryBuilder(), new QueryExecutor(context.Database.Connection.ConnectionString)) { QueryExtractor = new QueryExtractor(context); customerMapper = new TableMapper<Customer>(QueryExtractor, "Customer"); clientMapper = new TableMapper<Client>(QueryExtractor); } public List<IReportRelation> ReportRelations { return new List<IReportRelation> { new ReportRelation { ForeignColumn = customerMapper.GetReportColumn(x => x.ClientId), LocalColumn = clientMapper.GetReportColumn(x => x.Id), }, }; } } As you can see in the ReportRelations method, I return list of ReportRelation implementations. What I need to do is add 2 more public properies to my IReportRelation interface and ReportRelation implementation to hold instance of my TableMapper<TSource> class
How can I define a public variable in IReportRelation interface that will have a copy/instance of customerMapper property?
Here is how my IReportRelation definition
public interface IReportRelation { //Here I need to add an instance that hold the localTable //something like this TableMapper<TSource> LocalMapper //something like this TableMapper<TSource> ForeignMapper IReportColumn ForeignColumn { get; set; } IReportColumn LocalColumn { get; set; } } UPDATED
After Tim's suggestion below I change my code to the following
public interface IReportRelation<TLocal, TForeign> { TableMapper<TLocal> LocalMapper { get; set;} TableMapper<TForeign> ForeignMapper { get; set; } IReportColumn ForeignColumn { get; set; } IReportColumn LocalColumn { get; set; } } Then in my CustomerMapper class my function will look like this
public List<IReportRelation<Customer,Client> ReportRelations { return new List<IReportRelation<Customer,Client>> { new ReportRelation<Customer,Client> { LocalMapper = this.customerMapper, ForeignMapper = this.clientMapper, ForeignColumn = customerMapper.GetReportColumn(x => x.ClientId), LocalColumn = clientMapper.GetReportColumn(x => x.Id), }, }; } But the problem is that ReportRelations will always return a list of IReportRelation<Customer,Client>. What if I want to return mix list something like this for example
public List<IReportRelation<Customer,Client> ReportRelations { return new List<IReportRelation<Customer,Client>> { new ReportRelation<Customer,Client> { LocalMapper = this.customerMapper, ForeignMapper = this.clientMapper, ForeignColumn = customerMapper.GetReportColumn(x => x.ClientId), LocalColumn = clientMapper.GetReportColumn(x => x.Id), }, new ReportRelation<Customer,Team> { LocalMapper = this.customerMapper, ForeignMapper = this.teamMapper, ForeignColumn = customerMapper.GetReportColumn(x => x.TeamId), LocalColumn = clientMapper.GetReportColumn(x => x.Id), }, }; }
customerMapperTableMapper<TSource> LocalMapper { get; set; }, the same way you define other properties. I'm a little confused as to what the challenge is here.ClientandCustomernot being fixed.