How would I use this Factory class and whats the point? - c#

This is my first ever post to SO and am very new to C# with most of my IT experience in databases. I am starting to look at some of our code and would like to understand how I would use this class and it methods for resusability purposes.
public class FileCreator
{
public string Territory { get; set; }
public string CV { get; set; }
public string AdDate { get; set; }
public string Category { get; set; }
public string Advertiser { get; set; }
public string Brand { get; set; }
public decimal SumOfSpend { get; set; }
public decimal SumOfVolume { get; set; }
public string Spots { get; set; }
public string PageNumber { get; set; }
internal static List<FileCreator> Create(DataSet data)
{
var result = new List<FileCreator>();
if (data.Tables.Count > 0)
{
result = Create(data.Tables[0]);
}
return result;
}
public static List<FileCreator> Create(DataTable dataTable)
{
var result = new List<FileCreator>();
foreach (DataRow row in dataTable.Rows)
{
result.Add(Create(row));
}
return result;
}
private static FileCreator Create(DataRow row)
{
var fileCreator = new FileCreator();
fileCreator.Territory = (row["Territory"].ToString());
fileCreator.CV = row["CV"].ToString();
fileCreator.AdDate = row["Ad_date"].ToString();
fileCreator.Category = row["Category"].ToString();
fileCreator.Advertiser = row["Advertiser"].ToString();
fileCreator.Brand = row["Brand"].ToString();
fileCreator.SumOfSpend = Convert.ToDecimal(row["SumOfSpend"].ToString());
fileCreator.SumOfVolume = Convert.ToDecimal(row["SumOfVolume"].ToString());
fileCreator.Spots = row["Spots"].ToString();
fileCreator.PageNumber = row["Page Number"].ToString();
return fileCreator;
}
}
Why not just create a new instance of a datatable i.e. var dt = new Datatable()?
I must be missing the point of this approach. How will I benefit from this approach when I normally just create multiple datatables?
Go easy. It's my first post 🙂
Thank you

The point of this is get a strongly typed class instead of relying on hard-coded strings to get the data out and then casting everything to the type you are expecting. Instead, its taking data from the Datatable and transforming it to a "Dto" (Data Transfer Object) where you know all the properties (columns) that exist and their types because they are explicitly declared. There is no guessing! :)
While the given approach here works, I hope it is more for legacy sake. It is more efficient to simply create this Dto class directly instead of creating a Datatable and then mapping it.

Related

Add class properties from the List and put them into the string together with a comma as a delimiter

Although the thing I want to do seems be really trivial I can not find a way to achieve what I want. I know there exist multiple questions how to put class properties into the list together and separate it by a comma like that on SO, but none of them seemed to be relevant to my case.
I have a class Form defined as follows:
public class Form
{
public string CustomerName { get; set; }
public string CustomerAdress { get; set; }
public string CustomerNumber { get; set; }
public string OfficeAdress { get; set; }
public string Date { get; set; }
public Boolean FunctionalTest { get; set; }
public string Signature { get; set; }
public Form()
{
}
}
In the MainPage.xaml.cs, I create a List<Form> with the Form class properties and subsequently I would like to create a string with all of those class properties separated by a comma. For that case I use basic Join method with Select which converts any kinds of objects to string.
I do that by createCSV method inside MainPage.xaml.cs :
void createCSV()
{
var records = new List<Form>
{
new Form {CustomerName = customerName.Text,
CustomerAdress = customerAdress.Text,
CustomerNumber = customerNumber.Text,
OfficeAdress = officeAdress.Text,
Date = date.Date.ToString("MM/dd/yyyy"),
FunctionalTest = testPicker.ToString()=="YES" ? true : false,
Signature = signature.Text
}
};
string results = String.Join(",", (object)records.Select(o => o.ToString()));
}
The problem is instead of the desirable outcome which is:"Mark Brown,123 High Level Street,01578454521,43 Falmouth Road,12/15/2020,false,Brown"
I am getting: "System.Linq.Enumerable+SelectListIterator'2[MyApp.Form,System.String]"
PS. As you have noticed I am newbie in C#. Instead of non constructive criticism of the code, please for a valuable reply which would help me to understand what am I doing wrong.
Thanks in advance
In the Form class, You can override the ToString() method and use System.Reflection to get your comma string.
Form.cs
public class Form
{
public string CustomerName { get; set; }
public string CustomerAdress { get; set; }
public string CustomerNumber { get; set; }
public string OfficeAdress { get; set; }
public string Date { get; set; }
public bool FunctionalTest { get; set; }
public string Signature { get; set; }
public override string ToString()
{
string modelString = string.Empty;
PropertyInfo[] properties = typeof(Form).GetProperties();
foreach (PropertyInfo property in properties)
{
var value = property.GetValue(this); // you should add a null check here before doing value.ToString as it will break on null
modelString += value.ToString() + ",";
}
return modelString;
}
}
Code
List<string> CSVDataList = new List<string>();
List<Form> FormList = new List<Form>();
...
foreach (var data in FormList)
{
CSVDataList.Add(data.ToString());
}
Now you have a list of string CSVDataList with each Form object's data in comma style
P.S.
for DateTime
var value = property.GetValue(this);
if(value is DateTime date)
{
modelString += date.ToString("dd.MM.yyyy") + ",";
}

Parse XML with Linq with multiple child elements

This is my first question on SO, please let me know if I am doing anything wrong!
I am trying to parse an XML similar to this:
<LiveUpdate>
<CityID>F0A21EA2</CityID>
<CityName>CityTown</CityName>
<UserName>john</UserName>
<ApplicationDetails>
<ApplicationDetail
Application="AC"
Licensed="true"
Version="2015.2"
Patch="0001"
/>
<ApplicationDetail
Application="AP"
Licensed="true"
Version="2015.2"
Patch="0002"
/>
</ApplicationDetails>
</LiveUpdate>
I have classes that look like this:
public class Client
{
public string cityID { get; set; }
public string cityName { get; set; }
public string userName { get; set; }
public List<Apps> appList { get; set; }
}
public class Apps
{
public string app { get; set; }
public string licensed { get; set; }
public string version { get; set; }
public string patch { get; set; }
}
I need to be able to have a client class with a list of all the application details to be iterated over.
So far the best I've come up with is:
XDocument xml = XDocument.Load(#"C:\blah\Desktop\1.xml");
var liveUpdate = xml.Root;
var clients = (from e in liveUpdate.Elements()
select new Client()
{
cityID = e.Element("CityID").Value,
cityName = e.Element("CityName").Value,
userName = e.Element("UserName").Value,
appList = e.Elements("ApplicationDetails")
.Select(a => new Apps()
{
app = a.Element("Application").Value,
licensed = a.Element("Licensed").Value,
version = a.Element("Version").Value,
patch = a.Element("Patch").Value
}).ToList()
});
However, I'm currently running into an error that says Object reference not set to an instance of an object.
I've seen some similar examples on here, but not that deal with data before the multiple children.
I'm fairly new to XML and Linq so any help here would be greatly appreciated!
Your XML only contains one LiveUpdate tag, so rather than iterating over all of the elements inside of it, you just want to look at the Root element.
In ApplicationDetails, Application, Licensed and such are attributes, not elements. Use .Attribute() to access them.
ApplicationDetails is a single tag, and inside it you have ApplicationDetail tags.
There is no DateTime element in your LiveUpdate tag.
This works:
var liveUpdate = xml.Root;
var e = liveUpdate;
var clients = new Client()
{
cityID = e.Element("CityID").Value,
cityName = e.Element("CityName").Value,
userName = e.Element("UserName").Value,
//dateTime = e.Element("DateTime").Value,
appList = e.Element("ApplicationDetails").Elements("ApplicationDetail")
.Select(a => new Apps()
{
app = a.Attribute("Application").Value,
licensed = a.Attribute("Licensed").Value,
version = a.Attribute("Version").Value,
patch = a.Attribute("Patch").Value
}).ToList()
};
Since you have already defined a class into which you wish to deserialize, you can use XmlSerializer to deserialize it for you.
First, let's rename some of your property names to more closely match the XML and c# naming conventions:
[XmlRoot("LiveUpdate")]
public class Client
{
public string CityID { get; set; }
public string CityName { get; set; }
public string UserName { get; set; }
[XmlArray("ApplicationDetails")]
[XmlArrayItem("ApplicationDetail")]
public List<Apps> AppList { get; set; }
}
public class Apps
{
[XmlAttribute]
public string Application { get; set; }
[XmlAttribute]
public bool Licensed { get; set; }
[XmlAttribute]
public string Version { get; set; }
[XmlAttribute]
public string Patch { get; set; }
}
Then add the following extension methods:
public static class XmlSerializationHelper
{
public static T LoadFromXML<T>(this string xmlString)
{
using (StringReader reader = new StringReader(xmlString))
{
object result = new XmlSerializer(typeof(T)).Deserialize(reader);
if (result is T)
{
return (T)result;
}
}
return default(T);
}
public static T LoadFromFile<T>(string filename)
{
using (var fs = new FileStream(filename, FileMode.Open))
{
object result = new XmlSerializer(typeof(T)).Deserialize(fs);
if (result is T)
{
return (T)result;
}
}
return default(T);
}
}
Now you can deserialize from your XML file as follows:
string fileName = #"C:\blah\Desktop\1.xml";
var client = XmlSerializationHelper.LoadFromFile<Client>(fileName);
I manually updated your Client class to map correctly to the provided XML, but if you wanted to do it automatically, see here: Generate C# class from XML.

Values returned from class revert to null

I create an new Contractor object "gc" that calls a method GetContractor() to return all the properties. The results it is returning is correct, however the "gc" object shows all "NULL". I assume I doing something incorrectly in my aspx.cs page?
aspx.cs
protected void fvWasteCollected_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert")){
ValidationSummaryWasteDetail.ValidationGroup = "WasteReceivedDetail";
if (IsValid) {
odsMRWWasteCollectedDetail.InsertParameters["WasteTypeId"].DefaultValue = ddlWasteCollectedType.SelectedValue;
odsMRWWasteCollectedDetail.InsertParameters["DisposalMethodId"].DefaultValue = ddl_disposalMethod.SelectedValue;
Contractor gc = new Contractor();
gc.GetContractor(2);
var contractorName = gc.MRWContractorName;
}
}
}
.cs
public class Contractor
{
public Contractor GetContractor(int MRWContractorId)
{
using (DataAccessLINQDataContext db = new DataAccessLINQDataContext())
{
var result = db.MRWContractors.Where(c => c.MRWContractorId == MRWContractorId).Select(c => new Contractor
{
MRWContractorId = c.MRWContractorId,
MRWContractorName = c.MRWContractorName,
MRWContractorAddress = c.MRWContractorAddress,
MRWContractorCity = c.MRWContractorCity,
MRWContractorStateCode = c.MRWContractorStateCode,
MRWContractorZipCode = c.MRWContractorZipCode,
MRWContractorPhone = c.MRWContractorPhone,
MRWContractorFax = c.MRWContractorFax,
MRWContractorEmail = c.MRWContractorEmail
}).SingleOrDefault();
return result;
}
}
public int MRWContractorId { get; set; }
public string MRWContractorName { get; set; }
public string MRWContractorAddress { get; set; }
public string MRWContractorCity { get; set; }
public string MRWContractorStateCode { get; set; }
public int? MRWContractorZipCode { get; set; }
public string MRWContractorPhone { get; set; }
public string MRWContractorFax { get; set; }
public string MRWContractorEmail { get; set; }
}
You are loosing the value of gc when you dont assign it to something.
Try this instead:
var contractor = gc.GetContractor(2);
var contractorName = contractor.MRWContractorName;
You are creating one empty instance of the object that is only used to call the GetContractor method. The GetContractor method creates another instance that contains data, which is returned, but you just throw that instance away and expect the data to be available in the first instance that never got populated.
Make the GetContractor method static so that you don't need an instance to call it:
public static Contractor GetContractor(int MRWContractorId)
Now you can call the method to get that instance that contains the data, without first creating an empty instance:
Contractor gc = Contractor.GetContractor(2);
string contractorName = gc.MRWContractorName;

Add temporary property to list of objects

I have 3 models
public class Payroll
{
public int ID { get; set; }
public DateTime Date { get; set; }
public int PayCategoryID { get; set; }
public virtual PayCategory PayCategory { get; set; }
}
this one:
public class PayCategory
{
public int ID { get; set; }
public string PayScenario { get; set; }
public int PayGroupID { get; set; }
public virtual PayGroup PayGroup { get; set; }
}
and this one:
public class PayGroup
{
public int ID { get; set; }
public string Label { get; set; }
public string Description { get; set; }
public string EntryType { get; set; }
}
If I create a List of Payroll I will only get a list of Payroll objects with 4 properties each.
I want a list of , where each Payroll object will have the fields
Date, PayScenario, Label, Description, and EntryType. I know these can be easily obtained by
Payroll.PayCategory.PayScenario
Payroll.PayCategory.PayGroup.Label
etc.
But I am exporting these to an excel document using this generic method:
public static void Export(List<T> data, string name, Controller controller)
{
XLWorkbook workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add(name);
worksheet.Cell(1, 1).InsertTable(data);
controller.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheet.sheet";
controller.Response.AddHeader("content-disposition", String.Format(#"attachment;filename={0}.xlsx", name.Replace(" ", "_")));
using (MemoryStream memoryStream = new MemoryStream())
{
workbook.SaveAs(memoryStream);
memoryStream.WriteTo(controller.Response.OutputStream);
memoryStream.Close();
}
controller.Response.End();
}
I call from controller like this:
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
And the results of course give me a simple table with only the value Date in it, (plus some other trash columns like System [...] PayCategory)
Ultimately, I want the properties of these 3 models to be merged (excluding ID's) to give me a single list that I can pass on to the ExportData method. As in, is there a way to add a column to a list of objects? Something similar to
for(int i = 1; i<payrolls.Count; i++)
payrolls[i].Add.(payrolls[i].PayCategory.PayScenario);
which of course, doesn't work.
Please don't ask why I don't simply have one model instead of these 3. It's not an option unless I want to hard code values.
Thanks
If you can import Linq, you can do something like:
var payrolls = from aPayroll in (List<Payroll>)Session["payrolls"]
select new {
Date = aPayroll.Date,
PayScenario = aPayroll.PayCategory.PayScenario,
Label = aPayroll.PayGroup.Label,
Description = aPayroll.PayGroup.Description,
EntryType = aPayroll.PayGroup.EntryType
};
That would create an anonymous type with the five properties you want.
Assuming you're using ClosedXML, you should also be able to create a DataTable and send it directly to a worksheet.
What you are looking for is an "Anonymous Object".
Example usage:
var anonymousObject = new
{
ID = payrollObj.Id,
// other payroll properties
PayScenario = payrollObj.PayCategory.PayScenario
// other extended properties
};
Here's an article about them
http://www.c-sharpcorner.com/UploadFile/ff2f08/anonymous-types-in-C-Sharp/
The suggestion here is to create a new anonymous object within your loop and pass that onto your excel function instead of the original payrollObject.

Get Facebook Wall Facebook C# SDK -> Nested JSON

I am trying to get facebook wall but I am having issues with JSON Nested structure.
following is my code so far
var wall = (JsonObject)fb.Get("me/feed");
List<FBWall> myWall = new List<FBWall>();
foreach (var obj in (JsonArray)wall["data"])
{
FBWall myWall = new Wall();
myWall.Id = (string)(((JsonObject)obj)["id"]);
}
actual string in wall is coming something like this
{"data":[{"id":"576376893_10150590188751894","from":{"name":"Afnan Bashir","id":"576376893"},"story":"\"how are you man??? \" on Xain Ul-Abiddin's timeline."........ and so on
now I have got data but when i do (as it should come like this because main file contains other json arrays)
(((JsonObject)obj)["from"]) I get {"name":"Afnan Bashir","id":"576376893"}
now how would you write in foreach to get everything without going to another foreach. I was thinking for some way may be with Linq
Try this
public class Post
{
public string ID { get; set; }
public BaseUser From { get; set; }
public string Text { get; set; }
public ItemType Type { get; set; }
public string Picture { set; get; }
public string Link { set; get; }
public string CreatedTime { get; set; }
public string UpdatedTime { get; set; }
}
public class BaseUser
{
public string ID { get; set; }
public string Name { get; set; }
}
public enum ItemType
{
Story,
Message
}
try
{
List<Post> posts = new List<Post>();
dynamic p = (IDictionary<string, object>)fb.Get(userID + "/feed");
foreach (dynamic item in p.data)
{
try
{
Dictionary<string, object>.KeyCollection keys = item.Keys;
Post post = new Post();
if (keys.Contains<string>("story"))
{
post.Type = ItemType.Story;
post.Text = item.story;
post.Link = item.link;
post.Picture = item.picture;
}
else if (keys.Contains<string>("message"))
{
post.Type = ItemType.Message;
post.Text = item.message;
//post.Link = user.Link;
//post.Picture = item.picture;
}
keys = null;
post.ID = item.id;
post.UpdatedTime = item.updated_time;
post.CreatedTime = item.created_time;
BaseUser baseUser = new BaseUser();
dynamic from = item.from;
baseUser.ID = from.id;
baseUser.Name = from.name;
post.From = baseUser;
posts.Add(post);
}
catch (Exception)
{}
}
}
catch (Exception)
{}
It's always hard to cast an object like FbWall to the returned results of the Graph API. As much as I like type safety and being able to see compile-time errors, it was too much of a pain in the butt to convert the dynamic data over to a type.
So I've become adjusted to using dynamic (if you're using 4.0).
If you're not using 4.0, then you can continue to use wall["data][0]["from"][0]["name"] to get at values of the object returned.
I really do love living around the warmth and snuggliness of type safety, and I kicked and screamed about having to live down this unknown and uncomfortable road. It will be all right, just "bite the bullet" and "get 'er done".

Categories

Resources