For some situation, I need to save data or configuration as a default to the DLL and EXE files. First I make XML file in Visual Studio and fill the contents. After that set XML file’s Build Action to Embedded Resource. After compiling and produce DLL or EXE you can access the resources using the methods below.
/// <summary>
/// Get XML from Embedded Resources
/// </summary>
/// <returns></returns>
public XmlReader GetXmlConfig()
{
XmlReader xmlReader = null;
Assembly asm = Assembly.LoadFrom(“myLibrary.dll”);
if (asm != null)
{
string[] strArr = asm.GetManifestResourceNames();
if (strArr != null && strArr.Length > 0)
{
foreach (string str in strArr)
{
if (str.EndsWith(“MyXML.xml”))
{
Stream stream = asm.GetManifestResourceStream(str);
xmlReader = XmlReader.Create(stream);
}
}
}
} return xmlReader;
}
Saturday I went to I met Salamun, Cipto and Eriawan there. It was nice since we talked freely without any interruption and problem.
We talk everything on programming approach, how to handle the data, how to write the code etc. We have some conclusion on how bad and good programming.
- Always thinks keep codes long enough in one methods if only called once.
For example if have methods RunForDataX that run sql statement to get data and manipulate it.
public void RunForDataX()
{
// — 200 lines –
}
It is hard to maintain the long codes so I like to split up into methods with shorter codes.
- Loading unnecessary data and code to memory and manipulating on the fly. It is expensive for desktop and web application even you deploy on server farm.
- Bad class or library design. Some classes have no design at all or not use defacto pattern/guide. Some has implement it wrong. It will increase maintenance cost.
- Don’t know when it will freeze. Any new improvement and feature added but don’t know when it has to be freeze and deliver the products.
- Build in the wrong way, wrong library and wrong tools.
- Misuse and overuse the code or even the technology.
- Make unnecessary object dependency and sometimes calling some methods in the sequentially. Most of the codes is too tight coupling.
In the System.Drawing, Image can only get from the file and don’t have methods to download the images from the internet. The code below is getting from the url. First define the url. and download the data using stream.
private Image LoadFromURL(string url)
{
Image image = null;
MemoryStream ms = null;
byte[] data = null;
Uri uri = new Uri(url,UriKind.RelativeOrAbsolute);
WebClient webClient = new WebClient();
data = webClient.DownloadData(uri);
ms = new MemoryStream(data);
if (ms != null)
{
image = Image.FromStream(ms);
}
return image;
}
Some forums said that I should use 3rd party controls to accelerate development process because of the reusable. It can be problems since I don’t know the controls works. I just know the features of the controls and using it. Some of them don’t have clear documentation and manuals.
Most of them claims the product best and complaint with some technology or other vendor controls. In fact they cannot claim 100% works. For examples the controls has feature of persistence and ajax / microsoft ajax technology. When they assemble into the HTML pages, There are many errors because of the javascript or callback.
To decrease the problem, use 3rd party controls wise.
Some controls is very expensive to buy although they have “sounds good” license like developer license, seat license. Some controls will open their source, but who has a time to analyze the source and add functionality their ? Wait for major or minor release ? Forget it. I have the job to finish quickly and deliver it to customer.
Using from the framework or base control is nice idea. We can do what we want like encapsulate or extending the functionality. It will come the price for time to develop the control to provide the features I want.
Using open source or free 3rd control is not bad. Since sometimes it is not really free. Some features is not complete and maybe bugs. But we still can extending the functionality or repair bugs step by step.
I need to know what kind of control that are available on my form when I design complex control. First make the property and give attribute TypeConverter with type of ConverterID and IDReferenceProperty to WebControl
For example:
[TypeConverter(typeof(ControlIDConverter)), IDReferenceProperty(typeof(WebControl))]
public string TargetControlID
{
get { return this.targetControlID; }
set { this.targetControlID= value; }
}
private string targetControlID;
After build, drag the control from the toolbox to web form and the property will show up with the list of control ID on the form.