I am trying to create a generic CSV conversion static method. It has the entity - the extension.. let that be "T" however I also need to include a different context based on the entity. There are two databases each have a suburb entity and the DBs context.
How do I have two generic vars so to speak with one being the extension.. This is what I started with:
public static Suburb FromCsv(string csvLine, CATALOGContext context)
{
if (csvLine == null) throw new ArgumentNullException(nameof(csvLine));
if (context == null) throw new ArgumentNullException(nameof(context));
var values = csvLine.Split(',');
if (context.States == null) return null;
if (values.Length <= 3) return null;
var suburb = new Suburb
{
PostCode = values[0],
SuburbName = values[1],
State = context.States.FirstOrDefault(s => s.StateShortName == values[2]),
Latitude = Convert.ToDouble(values[3], CultureInfo.CurrentCulture),
Longitude = Convert.ToDouble(values[4], CultureInfo.CurrentCulture)
};
return suburb;
}
This is what I had but its clearly not right.
public static T FromCsv<T, I>(this T source, string csvLine, I context)
{
if (csvLine == null) throw new ArgumentNullException(nameof(csvLine));
if (context == null) throw new ArgumentNullException(nameof(context));
var values = csvLine.Split(',');
if (context.States == null) return null;
if (values.Length <= 3) return null;
var suburb = new Suburb
{
PostCode = values[0],
SuburbName = values[1],
State = context.States.FirstOrDefault(s => s.StateShortName == values[2]),
Latitude = Convert.ToDouble(values[3], CultureInfo.CurrentCulture),
Longitude = Convert.ToDouble(values[4], CultureInfo.CurrentCulture)
};
return suburb;
}
It does not know that this is a dbContext (context) so I need to use a where clause and I am unsure how to do that. Further, each dbContext has a "State" but this is error-ing for the above reason -doesnt know its a context.
How can I have multiple generics with one being the extension - the entity and the other being the context (not even sure I can have this) where you can use it with different database contexts etc?
You cannot use specific entity to return T. You have to use T to return T. Why not using this simple one?
public enum DBcon
{
DB1, DB2
}
public static Suburb FromCsv(string csvLine, DBcon con)
{
var context = con == DBcon.DB1 ? new CATALOGContext() : new DB2Context();
if (csvLine == null) throw new ArgumentNullException(nameof(csvLine));
if (context == null) throw new ArgumentNullException(nameof(context));
var values = csvLine.Split(',');
if (context.States == null) return null;
if (values.Length <= 3) return null;
var suburb = new Suburb
{
PostCode = values[0],
SuburbName = values[1],
State = context.States.FirstOrDefault(s => s.StateShortName == values[2]),
Latitude = Convert.ToDouble(values[3], CultureInfo.CurrentCulture),
Longitude = Convert.ToDouble(values[4], CultureInfo.CurrentCulture)
};
return suburb;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.