Convert DataRow to object - c#

I've created generic List and populate with some objects. Then List I mentioned before converted into DataTable to use in DataGridView. Problem is when I want get Row from this grid I have DataRow. I wanted to convert this to my object againt but not sure how to do it. Maybe you could give some example?
Thanks

Well, if you can't or won't use an "ORM" (object-relational mapper, like Linq-to-SQL or NHibernate - that's exactly what these tools do, and do quite well for you), you'll have to do this yourself.
Converting a DataRow into a domain object model is pretty boring code, really:
public Customer ConvertRowToCustomer(DataRow row)
{
Customer result = new Customer();
result.ID = row.Field<int>("ID");
result.Name = row.Field<string>("CustomerName");
..... // and so on
return result;
}
The biggest challenge here is making this rock-solid and handling (or avoiding) all possible errors (like a field being NULL etc.).
Another possibility would be to have a constructor on your domain model object type that would take a DataRow as parameter and construct a new object from it.
Marc

Assuming you're using a class MyObject, defined as follows :
class MyObject
{
public string Foo { get; set; }
public int Foo { get; set; }
}
You could do something like that :
using System.Data.DataSetExtensions;
...
List<MyObject> list = (from row in table.AsEnumerable()
select new MyObject
{
Foo = row.Field<string>("foo"),
Bar = row.Field<int>("bar")
}).ToList();

Why not just put your objects into a BindingList<> rather than a List<>? Then you can skip the converting to DataTable and back again exercise. You may need to implement INotifyPropertyChanged on your objects, but once they are inside a BindingList, changes in the datagrid will automatically be applied to your underlying objects.
Sorting can be handled by either sorting the list manually on column header click, or by inheriting from BindingList<> and implementing the sorting functionality inside it - then clicking on a header automatically sorts the list - no code required.

Well nowadays it is easier using ORMs of course. But if still you're using the old fashion you can go with a pretty easy Extension Class to do the job for you using a little bit of reflection and generic methods and lambda as follows:
public static class MapperExtensionClass
{
public static IEnumerable<MyClassType> ToMyClassTypeEnumerable(this DataTable table)
{
return table.AsEnumerable().Select(r => r.ToMyClassType());
}
public static MyClassType ToMyClassType(this DataRow row)
{
return row.ToObject<MyClassType>();
}
public static T ToObject<T>(this DataRow row) where T: new()
{
T obj = new T();
foreach (PropertyInfo property in typeof(T).GetProperties())
{
if (row.Table.Columns.Contains(property.Name))
{
property.SetValue(obj, property.PropertyType.ToDefault(row[property.Name]));
}
}
return obj;
}
public static object ToDefault(this Type type, object obj)
{
if (type == null)
throw new Exception("Customized exception message");
var method = typeof(MapperExtensionClass)
.GetMethod("ToDefaultGeneric", BindingFlags.Static | BindingFlags.Public);
var generic = method.MakeGenericMethod(type);
return generic.Invoke(null, new object[] { obj });
}
public static T ToDefaultGeneric<T>(object obj)
{
if (obj == null || obj == DBNull.Value)
{
return default(T);
}
else
{
return (T)obj;
}
}
}
You should also remember GridView objects can bind a lot of data source types. So it is your decision from a design point about what you should go with.

Related

Generic method cannot be accessed

I have currently been learning and researching Generic's inside C# but am struggling with actually using the method once created.
I have tried:
public class myTestClass
{
class example
{
public static DataTable LINQtoDataTable<T>(IEnumerable<T> data)
{
DataTable dt = new DataTable();
PropertyInfo[] objectProps = null; // Reflection
if (data == null) return null;
foreach (T record in data)
{
if (objectProps == null) objectProps = ((Type)data.GetType()).GetProperties();
foreach (PropertyInfo pi in objectProps)
{
Type columnType = pi.PropertyType;
if ((columnType.IsGenericType) && (columnType.GetGenericTypeDefinition() == typeof(Nullable<>))) columnType = columnType.GetGenericArguments()[0];
dt.Columns.Add(new DataColumn(pi.Name, columnType));
}
}
return dt;
}
}
example ex { get; set; }
public myTestClass()
{
this.ex = new example();
}
}
But when I do (In a C# Form):
// Namespace area
myTestClass test;
// Main Method
test = new myTestClass();
test.LINQtoDataTable() doesn't come up or exist. Can anyone please help me out? I am confused to why this won't appear since I public'd the method and instanced the class it is inside :(
Greatly appreciated & thanks in advance.
You are trying to create extension method, and for extension method there are some pre-requisites for the method to have, it should be static and in a static class, the one you are missing is this keyword and your class is not static in start of it:
public static DataTable LINQtoDataTable<T>(this IEnumerable<T> data)
{
DataTable dt = new DataTable();
PropertyInfo[] objectProps = null; // Reflection
if (data == null) return null;
foreach (T record in data)
{
if (objectProps == null) objectProps = ((Type)data.GetType()).GetProperties();
foreach (PropertyInfo pi in objectProps)
{
Type columnType = pi.PropertyType;
if ((columnType.IsGenericType) && (columnType.GetGenericTypeDefinition() == typeof(Nullable<>))) columnType = columnType.GetGenericArguments()[0];
dt.Columns.Add(new DataColumn(pi.Name, columnType));
}
}
return dt;
}
and you will still now see it in intellisense, because you are creating extension method for IEnumerable<T> while you are trying to call it on just T.
For able to call it, you have to create a List<T> :
List<myTestClass> listTestClass = new List<myTestClass>();
listTestClass.Add(new myTestClass());
listTestClass.LINQtoDataTable();
I once wrote a blog post on extension methods topic, you may want to read about extension method with a simple examplehere
An extension-method exists in a static public class whilst your current code has only a static method within a private class. So you need this:
public static class MyTestClass {
public static DataTable LINQtoDataTable<T>(this IEnumerable<T> data) { ... }
}
Furthermore you need the this-keyword on the param you want to be that extension-method be bound to.
Last an extension-method can´t stay in a nested class, which you apparently don´t really need at all. Delete the nested class and make MyTestClass (also consider the naming-conventions for classes) publc and static and put the method there. Thus you won´t need any instance of this class. Simpy call myEnumerable.LINQtoDataTable().
becuse the LINQtoDataTable metode it static, is not need an example instance. which locat in the ex propery, and accessible via its name.
myTestClass.example.LINQtoDataTable(...)

Get list of property names using interface method

I have two custom types Customer and Employee which implement the interface ITablefy. This interface has only one method, GetPropertyList which returns a list of strings of the property names of the object that implements it. I have a web service which looks like:
public string ReturnPropertyNames(ITablefy i)
{
List<string> propList = new List<string>();
TableFactory factory = new TableFactory();
ITablefy table = factory.CreateTable(i);
propList = table.GetPropertyList(table);
return propList[1];
}
so in this example the Factory creates a concrete type that implements ITablefy
I realized when I had a problem when both of my classes Customer and Employee implemented their GetPropertyList methods exactly the same:
//property list is a private member variable in each class
public List<string> GetPropertyList(ITablefy i)
{
TableFactory factory = new TableFactory();
ITablefy table = factory.CreateTable(i);
foreach (var propInfo in table.GetType().GetProperties())
{
propertyList.Add(propInfo.Name);
}
return propertyList;
}
Rather than copy and paste that code I'm looking for a better solution to what I have currently. If I only want certain types to use the GetPropertyList method how can I control that without having to copy and paste this same code? Harcoding the type to create in each class doesn't seem like a good solution to me. Employee and Customer don't logically make sense to use inheritance either. What's a proper solution for something like this?
factory:
public class TableFactory
{
public ITablefy CreateTable(ITablefy i)
{
if (i is Employee)
{
return new Employee();
}
else if (i is Customer)
{
return new Customer();
}
else
{
return null;
}
}
}
public static List<string> GetPropertyNames(this Object o)
{
List<string> names = new List<string>
foreach (PropertyInfo prop in o.GetType().GetProperties())
names.Add(prop.Name);
return names;
}
Now you can implement ITablefy in terms of any object.GetPropertyNames() using the extension method above.
There are a few questions that comes to my mind:
If It's so easy to do generically, why are you even using the interface?
Shouldn't you be checking properties for public accessors?
Shouldn't your interface be returning a more general type like IEnumerable<string> or ICollection<string>?
Wouldn't the interface be better designed to filter out property names that you don't want? That way you could assume all public properties are part of the set except those that aren't.
You make the interface be something like:
public interface IPropertyInfoFilterProvider {
public Func<PropertyInfo, bool> PropertyInfoSkipFilter { get; set; }
}
or
public interface IPropertyNameFilterProvider {
public Func<string, bool> PropertyNameSkipFilter { get; set; }
}
and then you can initialize the default to (prop) => false.
so now you can harvest the property names automagically and in one place and let implementations determine what gets taken and what doesn't and your harvesting code could use that filter in a linq where clause.
You could make it an extension method on ITablefy.
Or a static method on ITablefy

Ways around creating an instance of a generic interface in C#

I've coded myself into a corner and would like your help to dig me out again. In the right direction.
So, I've implemented a minor SQLite wrapper where I wanted the solution to be generic (don't we all). Afterwards, I now realize that the usage of these classes and interfaces aren't very intuitive nor generic.
Let's start at the bottom and work upwards. I've created a class called DataRow that works as a base class for my table rows. The class DataRow itself only has a property Id (since all rows need one). This results in the following definition: class DataRow { public int Id { get; set; } }
Using this DataRow class, is each table. And for database tables I've created one generic interface, and one generic base class. The definitions looks like this:
internal interface ITable<T>
where T : DataRow, new()
{
T Select(int id);
List<T> Select(List<int> ids);
int Insert(T t);
void Update(T t);
bool Delete(int id);
}
public class Table<T> : ITable<T>
where T : DataRow, new()
{
// Commented out to protect you from a minor case of serious brain damage.
}
This setup allows me to create concise definitions. In fact, they tend to be quite epic, really. Proud to say.
public class Car : DataRow
{
public decimal ParkingTicketDebt { get; set; }
public DateTime WhenWifeWillAllowReplacement { get; set; }
public bool CanTransformIntoSomethingAwesome { get; set; }
}
public class Cars : Table<Car> {
// Yep, that's all. You can go home now, folks. There's nothing here. Nothing at all. Especially not a great treasure of gold. Whops... I mean... there's really not. Not that I'm aware of, anyway... I mean, there could be. Not that I wouldn't say if I had any information on this great trasure of gold that might exist. But I know nothing of such an item. I really don't, so you can stop thinking about this great treasure of gold. Since I don't know anything about it, the chance that it even exist is extremely low. Miniscule. I mean, you would probably not find anything, not even if you digged for, like, a really long time. Seven years or something. Oookay. Slowly fading away...
}
As you may or may not have noticed, I'm using the class type name of Cars to determine the name of the table in the database. Likewise, I'm performing reflection on Car and use its public property names and types to get/set values in the database. And yes, I'm aware that I'm in the process of coding a stripped down version of Entity Framework. Which sounds both really stupid and quite time consuming.
Anyway, here is a usage example of the class Cars, which I must remind you that I'm proud of:
new Cars().Delete(3497); // Note that I have a great number of (expensive) cars.
Nice, eh? One slight problem. This means that I have to write strongly typed code, specific to the number of tables that exist in the database. And I don't like specific code. I like generic code.
You might start arguing here that I'm overdoing it. Then let me tell you this. You're damn right I'm overkilling! I'm intentionally flamethrowing the dead guy that was ran over by a tank. Seven times.
So I started experimenting a bit and came up with this delicate solution:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod(EnableSession = true)]
public int CreateEmptyRow(string tableName)
{
var tableType = Type.GetType(tableName);
if (tableType == null)
throw new TypeLoadException("Dumbass. That table doesn't exist");
var instance = Activator.CreateInstance(tableType) as ITable<dynamic>;
if (instance == null)
throw new TypeLoadException("Idiot. That type isn't a table");
return instance.Insert(new DataRow());
}
Note that I can really understand if you have no idea why someone would want to create an empty row.
So what's wrong with this? Well, it doesn't compile, for one. Here's the error: There is no implicit reference conversion from 'dynamic' to 'DataRow'. A search on Google gave few results.
The problem is obviously Activator.CreateInstance(tableType) as ITable<dynamic>. I've tried things like Activator.CreateInstance(tableType) as ITable<Table<DataRow>>, an attempt which gave me this error: The type 'DataRow' must be convertible to 'DataRow'.
So, as ive written in the comment, Im adding an extra non-generic interface:
interface ITable
{
DataRow Select(int id);
IEnumerable<DataRow> Select(List<int> ids);
int Insert(DataRow t);
void Update(DataRow t);
}
interface ITable<T> where T : DataRow, new()
{
T Select(int id);
List<T> Select(List<int> ids);
int Insert(T t);
void Update(T t);
bool Delete(int id);
}
class Table<T> : ITable<T>, ITable where T : DataRow, new()
{
public T Select(int id)
{
return new T();
}
public List<T> Select(List<int> ids)
{
return new List<T>();
}
public int Insert(T t)
{
return 1;
}
public void Update(T t)
{
}
public bool Delete(int id)
{
return true;
}
DataRow ITable.Select(int id)
{
return this.Select(id);
}
IEnumerable<DataRow> ITable.Select(List<int> ids)
{
return this.Select(ids);
}
public int Insert(DataRow t)
{
return this.Insert(t);
}
public void Update(DataRow t)
{
this.Update(t);
}
}
and this is how im implementing the CreateEmptyRow \ Select methods:
public static int CreateEmptyRow(string tableName)
{
var tableType = Type.GetType(tableName);
if (tableType == null)
throw new TypeLoadException("Dumbass. That table doesn't exist");
var instance = Activator.CreateInstance(tableType) as ITable;
if (instance == null)
throw new TypeLoadException("Idiot. That type isn't a table");
return instance.Insert(new DataRow());
}
public static List<DataRow> Select(List<int> ids, string tableName)
{
var tableType = Type.GetType(tableName);
if (tableType == null)
throw new TypeLoadException("Dumbass. That table doesn't exist");
var instance = Activator.CreateInstance(tableType) as ITable;
if (instance == null)
throw new TypeLoadException("Idiot. That type isn't a table");
return instance.Select(ids).ToList();
}
notice that if you want such a generic solution, the select method (for example) can only return an IEnumerable \ List of DataRow, which can be solved by using the provided Cast extension method:
var myList = Select(null, "Cars").Cast<Car>();
Note: as you probably know, to instantiate the Cars class by name, you also need to provide the namespace, which i skipped here, and probably the Table<T> class should be abstract as well.
One problem is you're trying to insert a DataRow into a table which takes some subclass of DataRow, so even if you could compile it, you would still get an exception at runtime.
You need to find the generic row type to insert and insert a new instance of that type:
object instance = Activator.CreateInstance(tableType);
var tableInterface = tableType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(ITable<>));
if(tableInterface == null) throw new ArgumentException("Type is not a table type");
var rowType = tableInterface.GetGenericArguments()[0];
var newRow = Activator.CreateInstance(rowType);
MethodInfo insertMethod = tableInterface.GetMethod("Insert");
return (int)insertMethod.Invoke(instance, new object[] { newRow });
However it seems you could make your CreateEmptyRow method generic in the table and row type and avoid reflection altogether:
public int CreateEmptyRow<TTable, TRow>()
where TRow : DataRow, new()
where TTable : ITable<TRow>, new()
{
var table = new TTable();
return table.Insert(new TRow());
}

LINQ Changes Not Recognized When Using Generics

I am currently trying to create a Class which employs generics in order to reduce the amount of work needed for future development. As I add Tables to the LINQ To SQL Designer, certain basic Methods are used in each one. Rather than reproduce them in every Partial Class associated with every new Table, I would like to employ a single generic Class. The issue is that any changes made to the Entities are not recognized and therefore not submitted.
Public Partial Class ABC
{
Public Static Bool Synchronize(string source, string destination)
{
try
{
DataContext destinationDB = DataConnection.Destination(destination);
Table<ABC> destinationABCs = destinationDB.ABCs;
DataContext sourceDB = DataConnection.Destination(source)
Table<ABC> sourceABCs = sourceDB.ABCs;
foreach (ABC ABCCode in sourceABCs)
{
ABC destABCCode = destinationABCs.SingleOrDefault(x => x.Id == ABCCode.Id);
bool same = EntityProcessing.AreIdentical(ABCCode, destABCCode);
if (same == false)
{
destABCCode = (ABC)EntityProcessing.Synchronize(ABCCode, destABCCode);
}
}
ChangeSet test = destinationDB.GetChangeSet(); // Test Line For Debugging
destinationDB.SubmitChanges();
}
return true;
}
}
The next Class is:
Public Static Class EntityProcessing
{
Public Static Bool AreIdentical(Object sourceEntity, Object destinationEntity)
{
if (sourceEntity.GetType() == destinationEntity.GetType())
{
Type t = sourceEntity.GetType();
FieldInfo[] tList = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (FieldInfo fi in tList)
{
if ((fi.GetValue(sourceEntity) != null ? fi.GetValue(sourceEntity).ToString()
: null) == (fi.GetValue(destinationEntity) != null ?
fi.GetValue(destinationEntity).ToString() : null))
{ continue; }
else
{ return false; }
}
return true;
}
else
{ return false; }
}
Public Static Object Synchronize(Object sourceEntity, Object destinationEntity)
{
if (sourceEntity.GetType() == destinationEntity.GetType())
{
Type t = sourceEntity.GetType();
FieldInfo[] tList = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (FieldInfo fi in tList)
{
fi.SetValue(destinationEntity, fi.GetValue(sourceEntity));
}
}
return destinationEntity;
}
}
I have tried modifying the EntityProcessing.Synchronize method into a Void method as well. Neither works. Both will return the correct Entity with the Fields set to the appropriate results. The issue lies in the fact that LINQ does not recognize the Entities as having changed.
If I add a temporary line of ChangeSet test = destinationDB.GetChangeSet();, the Updated count is zero. The loss appears to be in the conversion to Objects.
I have tried setting the Parameter Type to ABC on the EntityProcessing.Synchronize() method and modifying a Field, and the Updated count in test is correct. How do I resolve this?
How do I submit the updated entities to the database or rather, how do I get LINQ to recognize these entities are being changed and needing an update?
Do you mean: Public Static Bool Synchronize<ABC>(string source, string destination) with "ABC" as the generic type?
However, I don't think your .ABCs will work that simply. You may have to use reflection to get at the proeprty with that particular name. For example, first use reflection to get the name of the type parameter (ABC), and then use reflection to get the table field from the data source based on this type name.

How to Work Around Limitations in Generic Type Constraints in C#?

Okay I'm looking for some input, I'm pretty sure this is not currently supported in .NET 3.5 but here goes.
I want to require a generic type passed into my class to have a constructor like this:
new(IDictionary<string,object>)
so the class would look like this
public MyClass<T> where T : new(IDictionary<string,object>)
{
T CreateObject(IDictionary<string,object> values)
{
return new T(values);
}
}
But the compiler doesn't support this, it doesn't really know what I'm asking.
Some of you might ask, why do you want to do this? Well I'm working on a pet project of an ORM so I get values from the DB and then create the object and load the values.
I thought it would be cleaner to allow the object just create itself with the values I give it. As far as I can tell I have two options:
1) Use reflection(which I'm trying to avoid) to grab the PropertyInfo[] array and then use that to load the values.
2) require T to support an interface like so:
public interface ILoadValues
{
void LoadValues(IDictionary values);
}
and then do this
public MyClass<T> where T:new(),ILoadValues
{
T CreateObject(IDictionary<string,object> values)
{
T obj = new T();
obj.LoadValues(values);
return obj;
}
}
The problem I have with the interface I guess is philosophical, I don't really want to expose a public method for people to load the values. Using the constructor the idea was that if I had an object like this
namespace DataSource.Data
{
public class User
{
protected internal User(IDictionary<string,object> values)
{
//Initialize
}
}
}
As long as the MyClass<T> was in the same assembly the constructor would be available. I personally think that the Type constraint in my opinion should ask (Do I have access to this constructor? I do, great!)
Anyways any input is welcome.
As stakx has said, you can't do this with a generic constraint. A workaround I've used in the past is to have the generic class constructor take a factory method that it can use to construct the T:
public class MyClass<T>
{
public delegate T Factory(IDictionary<string, object> values);
private readonly Factory _factory;
public MyClass(Factory factory)
{
_factory = factory;
}
public T CreateObject(IDictionary<string, object> values)
{
return _factory(values);
}
}
Used as follows:
MyClass<Bob> instance = new MyClass<Bob>(dict => new Bob(dict));
Bob bob = instance.CreateObject(someDictionary);
This gives you compile time type safety, at the expense of a slightly more convoluted construction pattern, and the possibility that someone could pass you a delegate which doesn't actually create a new object (which may or may not be a major issue depending on how strict you want the semantics of CreateObject to be).
If you can create common base class for all of T ojects that you are going to pass to MyClass as type parameters than you can do following:
internal interface ILoadValues
{
void LoadValues<TKey, TValue>(IDictionary<TKey, TValue> values);
}
public class Base : ILoadValues
{
void ILoadValues.LoadValues<TKey, TValue>(IDictionary<TKey, TValue> values)
{
// Load values.
}
}
public class MyClass<T>
where T : Base, new()
{
public T CreateObject(IDictionary<string,object> values)
{
ILoadValues obj = new T();
obj.LoadValues(values);
return (T)obj;
}
}
If you cannot have common base class than I think you should go with solution proposed by itowlson.
I'm legitimately curious at how you would load the values of a class without using reflection unless you had methods hardcoded to accomplish it. I'm sure there's another answer, but I'm not too ashamed to say I do not have experience in it. As for something I wrote to auto-load data, I have two base data classes I work from: a single object and then a list. In the single object (BaseDataClass), I have this method.
public virtual void InitializeClass(DataRow dr)
{
Type type = this.GetType();
PropertyInfo[] propInfos = type.GetProperties();
for (int i = 0; i < dr.ItemArray.GetLength(0); i++)
{
if (dr[i].GetType() != typeof(DBNull))
{
string field = dr.Table.Columns[i].ColumnName;
foreach (PropertyInfo propInfo in propInfos)
{
if (field.ToLower() == propInfo.Name.ToLower())
{
// get data value, set property, break
object o = dr[i];
propInfo.SetValue(this, o, null);
break;
}
}
}
}
}
And then in the data list
public abstract class GenericDataList<T> : List<T> where T : BaseDataClass
{
protected void InitializeList(string sql)
{
DataHandler dh = new DataHandler(); // my general database class
DataTable dt = dh.RetrieveData(sql);
if (dt != null)
{
this.InitializeList(dt);
dt.Dispose();
}
dt = null;
dh = null;
}
protected void InitializeList(DataTable dt)
{
if (dt != null)
{
Type type = typeof(T);
MethodInfo methodInfo = type.GetMethod("InitializeClass");
foreach (DataRow dr in dt.Rows)
{
T t = Activator.CreateInstance<T>();
if (methodInfo != null)
{
object[] paramArray = new object[1];
paramArray[0] = dr;
methodInfo.Invoke(t, paramArray);
}
this.Add(t);
}
}
}
}
I'm open to criticism, because no one has ever reviewed this code before. I am the sole programmer where I work, so I do not have others to bounce ideas off of. Thankfully, now I've come across this website!
Edit: You know what? Looking at it now, I don't see why I shouldn't just rewrite that last method as
protected void InitializeList(DataTable dt)
{
if (dt != null)
{
Type type = typeof(T);
foreach (DataRow dr in dt.Rows)
{
T t = Activator.CreateInstance<T>();
(t as BaseDataClass).InitializeClass(dr);
this.Add(t);
}
}
}
I assume that works, although I haven't tested it. No need to use reflection on that part.

Categories

Resources