I wanted to share this tip because I feel some Visual Studio features look like magic. In this tip, you'll see how to convert a C# class into a DataSource.
DataSources are objects and objects are classes. Transforming a class into a DataSource is great because it allows you to use LINQ to enumerate and read the data. This is also useful when you create a report or need to have access to individual records in DataRow formats. So, take a class with the data definition and with custom properties and add a key to the class to make it appear as a DataSource.
Let's start by creating a class. By assigning some attributes to a class, you're telling the compiler to make it a data object. In the following code, we add a DataObjectAttribute to a partial class and also make it serializable.
- using System.ComponentModel;
-
- [System.Serializable]
- [DataObjectAttribute]
- public partial class ReportProcessosEAndamentos
- {
- public int ID { get; set; }
- public string Name { get; set; }
- pubblic string MyData => MyCustomDataTransforms(); // You can process fields from the table or project
- // ANY OTHERS PROPERTIES
- // You can Add readonly property using private:
- public int MyReadOnlyProperty { get; private set; }
-
- }
Now, we need to add a property to the function that will load the DataSource and retrieve a data list.
[DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
- [DataObjectMethodAttribute(DataObjectMethodType.Select, true)] public static IEnumerable<BIExportData> GetList()
- => FillData(ConnectionString);
Here is a full sample from my real app www.Advocati.NET.
- using System;
- using System.Text;
- using System.Data;
- using System.Xml.Serialization;
- using System.Collections.Generic;
- using MenphisSI.DB;
- using System.Data.SqlClient;
- using System.ComponentModel;
-
- namespace MenphisSI.GerAdv
- {
-
-
-
- [System.Serializable]
- [DataObjectAttribute]
-
- public partial class DBAcao : VAuditor, ICadastros
- {
-
- private string m_FDescricao;
-
- [XmlAttribute]
- public int ID {get;set;}
- [XmlAttribute]
- public string FDescricao
- {
- get => m_FDescricao ?? string.Empty;
- set => m_FDescricao = value;
- }
-
- public DBAcao() { }
-
-
-
-
-
-
-
-
-
-
- [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
- public static IEnumerable<DBAcao> Listar(string cWhere, string cOrder, string cCnn, int nTop = 0)
- {
- var cSql = new StringBuilder(TSql.Select);
- if (nTop > 0) cSql.Append($" TOP {nTop} ");
- cSql.Append(DBAcao.CamposSqlX);
- cSql.Append(TSql.From);
- cSql.Append($"[dbo].[{DBAcao.PTabelaNome}] ");
- if (cWhere.NotIsEmpty())
- {
- if (cWhere.NãoContemUpper(TSql.Where)) cSql.Append(TSql.Where);
- cSql.Append(cWhere);
- }
- if (cOrder.NotIsEmpty())
- {
- if (cOrder.NãoContemUpper(TSql.OrderBy))
- cSql.Append($"{TSql.OrderBy} {cOrder}");
- else
- cSql.Append(cOrder);
- }
- else if (DBAcao.CampoNome.NotIsEmpty())
- {
- cSql.Append($"{TSql.OrderBy}{DBAcao.CampoNome}");
- }
- DataTable ds;
- try
- {
- using (var oCnn = ConfiguracoesDBT.GetConnection(cCnn))
- {
- if (oCnn is null) yield break;
- ds = ConfiguracoesDBT.GetDataTable(cSql.ToString(), oCnn);
- }
- }
- catch { yield break; }
- if (ds.Rows.Count <= 0) yield break;
- for (var nt = 0; nt < ds.Rows.Count; nt++)
- yield return new DBAcao
- {
- ID = Convert.ToInt32(ds.Rows[nt][DBAcaoDicInfo.CampoCodigo]),
- FDescricao = ds.Rows[nt][DBAcaoDicInfo.Descricao].ToString(),
- };
- }
- }
Note that "yield return" is a powerful command that will return the object directly to the IEnumerable list instead of crerating a list and return it in the end of the function/method.
Now, let's take a look at the sample using Telerik as Object DataSource and how to use it.
Open the DataSource.
Name it, and choose your load method, mark to get only.
To use the class, you need to populate with data.
How to use your "new" data component source is up to you.
Good luck!