Recently I’m doing some study on some WPF code, I fall in an interesting example from Microsoft called Outlook HOL WPF, which basically shows how to do an Outlook UI WPF replica, I found many posts about that but each with the same problem, where is the source code?? The missing..

So, after some Googlin’ I found it!! Here I whish to share with you the Outlook HOL WPF link I hope someone will find interesting as I do!

I found starting from an  MSDN Forum post, as the post describes, the last in the thead, you can download a zip file WPF.zip and insied you will the things: Code, Presentations and more!

Enjoy, Ciao!

Tags:

Hi,

recently I was doing some code about enum parsing, I think  an operation everyone’s involved in one time. After some Googlin’ I found some well or not well done examples. Doing a generic parse method is too simple as you can see on Windows.NET Blog post but you don’t have no type check code or constraints, or something about a default check value, so I done my implementation, which assumes that the target enum type has some well defined constraints like: inheriting from the struct base type,  having an Undefined or a None element.. if not the first enum member is assumed as the default one. Here is my little code

/// <summary> /// Translates from a string name enum member /// to the given gene ric enumeration Type /// </summary> /// <param name="name">the name string to translate</param> /// <returns></returns> private T TranslateFrom<T>(string name) where T : struct { const string DEFAULT = "Undefined"; const string NONE = "None"; Type enumType = typeof(T); T result; if (Enum.IsDefined(enumType, command)) { result = (T)Enum.Parse(enumType, command, true); } else { string defaultName = null; if (Enum.IsDefined(enumType, DEFAULT)) defaultName = DEFAULT; else if (Enum.IsDefined(enumType, NONE)) defaultName = NONE; if (!string.IsNullOrEmpty(defaultName)) { result = (T)Enum.Parse(enumType, command, true); } else { string[] values = Enum.GetNames(enumType); result = (T)Enum.Parse(enumType, values[0], true); } } return result; }

This is just a little helper method I wish to share, enjoy!

Ciao

Tags:

Hi, as I said in my previous post, I’m working on a kind of soa-like system, in the system we are exchanging some envelopes messages, something like this:

1 <Envelope> 2 <AuthData name="" pwd="" /> 3 <ContextData name="" /> 4 <Input> 5 <FirstNode /> 6 <FirstNode /> 7 <SecondNode /> 8 </Input> 9 <Output> 10 <ThirdNode /> 11 <FourthNode /> 12 <FourthNode /> 13 </Output> 14 </Envelope>

In my previous post Xml Default serialization I was talking about some serialization problems I found, especialy with serializing collections. I have to say that the Service, the remote service is an old-style ASMX Web Service, wich accept a string parameter and returns a string representing the output message. Well, while I was coding up a client to that service, I tried, first, to evolve the Client using a WCF style proxy, we know it’s truly possible to call an ASMX service from with a WCF client, but talking about the client proxy I made is out of the scope of this post.

Well, I made a Proxy library with a Proxy client and some Objects, well..Contracts, describing the solution and then using a DataContractSerializer in conjunction with an XmlSerializer to serialize-deserialize messages regarding the simple-and-raw message string representation which the Service expect..

So, the First problem was, How to create a DataContract object with a Input and Output DataMembers properties which could accept any kind of object? This because, clearly, I can send in the envelope any operation-request and expect the related operation-response..but how can I put them in to the evelope-DataContract-Object?? after some search I realized that the better thing was to accept in the Input and in the Output DataMember properties an xml String, so the things are:

  1. create the Message object
  2. create the Operation object
  3. serialize with an XmlSerializer the operation itself and put the result in the Input part of the Message previously created

But, when I tried to do as above, I fall into a DataContractSerializer behavior wich will "encode" the xml string, so the string instead of been passed for example as "<xml></xml>" is passed as "&lt;xml&gt;&lt;/xml&gt;", it’s clear that the encode xml string will not be accepted as a valid xml, so what could I try more?

A developer team I was working with, take the solution using an XmlDocument and let it be serialized-deserialized by the serializer, but I don’t really love this solution nor the XmlDocument itself for many reasons, then I took a solution from a forum-port I found on the net regarding a problem as mine, how to serialize a raw-xml string without involve in encoding-issue which is not an Issue but in my case it was..

so here is the solution, the brilliant one (I cannot find where I put the original forum-post-idea link), the solution is mixing the IXmlSerializable and the DataContractSerializer serialization. The DataContractSerializer we know will serialize DataContracts, DataMembers and more, even types marked as inheriting from IXmlSerializable, so using an object of that type as a DataMember of my Message-envelope will produce a well defined, not encoded xml string. The thing is made using implicit operators which will do the magic of translating an xml string to and from this kind of object.

I named the object XmlRawData because it represent a special place-holder object to represent an xml string inside a DataMember representation, but here is the code:

1 using System.Xml; 2 using System.Xml.Schema; 3 using System.Xml.Serialization; 4 5 namespace XGuy.Common.Serialization 6 { 7 /// <summary> 8 /// Rpresent a wrapper-placeholder for raw xml string 9 /// representation where is not possible to use direct 10 /// <b>string</b> data type 11 /// </summary> 12 public class XmlRawData : IXmlSerializable 13 { 14 /// <summary> 15 /// implicit operator from <b>XmlRawData</b> to <b>string</b> 16 /// </summary> 17 /// <param name="item"> 18 /// the XmlRawData instance to convert 19 /// </param> 20 /// <returns>the content string</returns> 21 public static implicit operator string(XmlRawData item) 22 { 23 return item == null ? null : item.Content; 24 } 25 26 /// <summary> 27 /// implicit operator from <b>string</b> to <b>XmlRawData</b> 28 /// </summary> 29 /// <param name="content">the xml raw string to convert 30 /// and wrap with an XmlRawData object</param> 31 /// <returns>the XmlRawData object</returns> 32 public static implicit operator XmlRawData(string content) 33 { 34 return content == null ? null : new XmlRawData(content); 35 } 36 37 /// <summary> 38 /// Gets or sets the string content of the object 39 /// </summary> 40 public string Content 41 { 42 get { return _content; } 43 set{ _content = value;} 44 } 45 46 /// <summary>internal content string placeholder</summary> 47 private string _content; 48 49 /// <summary> 50 /// Creates a new <b>XmlRawData</b> instance 51 /// </summary> 52 public XmlRawData() 53 {} 54 55 /// <summary> 56 /// Creates a new <b>XmlRawData</b> instance 57 /// </summary> 58 /// <param name="content">the raw xml string to wrap</param> 59 public XmlRawData(string content) 60 { 61 _content = content; 62 } 63 64 /// <summary> 65 /// Get the schema instance of the schema 66 /// related to the content 67 /// string 68 /// </summary> 69 /// <returns>a null reference</returns> 70 public XmlSchema GetSchema() 71 { 72 return null; 73 } 74 75 /// <summary> 76 /// Read from an xml reader the raw xml string content 77 /// previous serialized 78 /// </summary> 79 /// <param name="reader"></param> 80 /// <remarks> 81 /// Here is the first part of the magic of the object which 82 /// will take the inner xml of the containing xml and 83 /// reads it 84 /// into the inner content placeholder preventing the 85 /// encoding-decoding steps 86 /// </remarks> 87 public void ReadXml(XmlReader reader) 88 { 89 if (reader.IsEmptyElement) 90 { _content = string.Empty; } 91 else 92 { 93 switch (reader.NodeType) 94 { 95 case XmlNodeType.EndElement: 96 _content = string.Empty; break; 97 case XmlNodeType.Element: 98 _content = reader.ReadInnerXml(); 99 break; 100 } 101 } 102 } 103 104 /// <summary> 105 /// Writes the raw xml string into an <see cref="XmlWriter"/> 106 /// </summary> 107 /// <param name="writer"></param> 108 /// <remarks> 109 /// Here is the second part of the magic of the object which 110 /// will take the raw xml string and writes out with the 111 /// WriteRaw method this preventing the 112 /// encoding-decoding steps so the xml result clear 113 /// </remarks> 114 public void WriteXml(XmlWriter writer) 115 { 116 if(!string.IsNullOrEmpty(_content)) 117 writer.WriteRaw(_content); 118 } 119 120 /// <summary> 121 /// Returns the inner raw xml string 122 /// </summary> 123 /// <returns></returns> 124 public override string ToString() 125 { 126 return Content; 127 } 128 } 129 }

The above code shows how we can avoid the raw xml encoding step during the serialization, but let explaing and showing it with an usage example:

1 using System.ServiceModel; 2 using System.Runtime.Serialization; 3 4 namespace XGuy.Common.Serialization.Test 5 { 6 //first of all define a DataContract which will 7 //contains the xml raw DataMember property 8 9 [DataContract(Namespace = "http://xguy.serialization/2008/11")] 10 public class SimpleEnvelope 11 { 12 ///<summary> 13 /// Contains the envelope operation name 14 ///</summary> 15 [DataMember()] 16 public string Name { get; set; } 17 18 ///<summary> 19 /// Contains the envelope input data xml 20 ///</summary> 21 [DataMember()] 22 public XmlRawData Input { get; set; } 23 24 ///<summary> 25 /// Contains the envelope output data xml 26 ///</summary> 27 [DataMember(EmitDefaultValue = false)] 28 public XmlRawData Output { get; set; } 29 } 30 31 ///<summary> 32 /// Simple test class 33 ///</summary> 34 public class XmlRawDataTest() 35 { 36 public void Run() 37 { 38 //now I can do something like this 39 SimpleEnvelope env = new SimpleEvelope() 40 { 41 Name = "TestOperation", 42 Input = @"<Operation><Data>Test data 43 content</Data></Operation>" 44 }; 45 46 StringBuilder buffer 47 = new StringBuilder(); 48 49 XmlWriterSettings settings = new XmlWriterSettings(); 50 settings.OmitXmlDeclaration = true; 51 52 using (StringWriter writer = new StringWriter(buffer)) 53 using (XmlWriter xwriter = 54 XmlDictionaryWriter.Create(writer, settings)) 55 { 56 57 DataContractSerializer ser = 58 new DataContractSerializer(typeof(SimpleEnvelope)); 59 ser.WriteObject(xwriter, env); 60 xwriter.Flush(); 61 } 62 63 Console.WriteLine(buffer.ToString()); 64 } 65 } 66 }

So the result written out in the console will be:

1 <SimpleEnvelope 2 xmlns="http://xguy.serialization/2008/11"> 3 <Name>TestOperation</Name> 4 <Input> 5 <!-- this is the raw xml added part --> 6 <Operation><Data>Test data content</Data></Operation> 7 </Input> 8 <SimpleEnvelope/>

instead of the following without the XmlRawData object:

<SimpleEnvelope xmlns="http://xguy.serialization/2008/11"> <Name></Name> <Input> <!-- this is the encoded raw xml added part--> &lt;Operation&gt;&lt;Data&gt;Test data content&lt;/Data&gt;&lt;/Operation&gt; </Input> </SimpleEnvelope>

the difference it’s clear and no needs of more explain! Hope this Helps!

Ciao!

 

I’m working on a “SOA-Like” system, which lives and exchange data in the xml-envelope form which many of us knows as

   1: <Envelope>
   2:     <AuthData name="" pwd="" />
   3:     <ContextData name="" />
   4:     <Input>
   5:         <FirstNode />
   6:         <FirstNode />
   7:         <SecondNode />
   8:     </Input>
   9:     <Output>
  10:         <ThirdNode />
  11:         <FourthNode />
  12:         <FourthNode />
  13:     </Output>
  14: </Envelope>

I’m starting from an xsd schema which describes the envelope and each “Message” which will be enclosed in the “Input” part, then the operation-result message will be returned as a child of “Output” node. Until now everything could be fine, a standard Envelope, ESB-Message, or what else you want to name it, but a standard situation. But here my problems begins.

First of all, I cannot use a DataContract-fashion-class and the DataContractSerializer, because for the DataContract context it’s impossible to render an xml as above where child nodes are “variables” in the sense of I have a container, in my case the “Input” Object, which could have childs, maybe represented by a form of xmltype-anonymoustype array of objects, every time I can write a collection, an array in the form of:

   1: <Input>
   2:     <FirstNodeCollection>
   3:         <FirstNode />
   4:         <FirstNode />
   5:     </FirstNodeCollection>
   6: </Input>

in this way my objects hierarchy must be InputObject -> FirstNodeCollection -> FirtsNodeObject. I hate this way of representing the objects, but DataContract-fashion-classes and DataContractSerializer, could not produce a different “rendering”

Second, If I want to use the XmlSerializer instead of the DataContractSerializer, first I have to fill my classes of a lot of attributes, and even here, I can have some troubles, even if the only object which natively support the above representation is just the XmlSerializer.

Well, the XmlSerializer could help me, but the first thing which makes me hate it, is the fact that for each Type which it’ll represent, the XmlSerializer will generate a Temp assembly containing the ad-hoc serializer for that Type, and this is the thing which makes me really angry and hungry..of what?? of a normal, well driven, serialization API!

Personally I don’t know which makes Microsoft to choiche this kind of implementation about the DataContractSerializer, but I cannot believe there’s not an easy, more customizable way of doing it. Yes, someone could say me : “Hey, look at the IXmlSerializable interface” but this is a RAW implementation, this makes me think an Italian way to say, (..orrible translation..) “doing everything or doing nothing”, I try to explain it.. saying this I’m saying that Microsoft could makes a Monster, complicated over-rich API, or a simplest, it doesn’t know a “middle way” of doing things.

So, here I’m starting my provocation: why we cannot implement a simple new serialization API? maybe we could make in the beginning simple, and then let it grow and become more complicated! Maybe we could learn something from some JAVA implementation… I hope I can find anyone which wants to talk about, and maybe, start to think to implement something!

Ciao

Technorati Tag: ,

 

Recently, no sorry, it’s too much time, I saw online some discussions about using and the “best-practice-usage” of NHibernate in conjuction with WCF, and I saw so many thing about it I think someone has to say smething serious and real about this thread!

I Think I’m not one voice which can say something about it as some people in the net could do, I know many names of peoples who can be a relevant voice better than me, (I’m really new in posting to blog!) But I have some experience in this argument and so I wish to share my experience made in the past 3 months.

Now I’m working on a project built as a 3-tier app, the core technologies are surely WCF and NHibernate. I’m proud of using WCF as I see and as I heard from many people, no much people has used WCF yet (for example in my city – Rome – ), and more, WCF in conjuction with NHibernate.

I have some real experience in WCF I have made 4 or 5 big projects using that, and I have some minor experience in NHibernate, but the past 3 month gave to me a “little strong” experience on that  ” WCF + NHibernate “.

I”m working on an “intranet system” made as follow:

  • about 100 entities
  • about 50 of them are connected between them
  • about 10 services exchanging them
  • strong and large usage of ICriteria API

These are just some of the things that made up my solution, but are the more relevant I think to talk about the question:

  • My Objects are just POCOs, decorated with DataContract and used by some services.
  • My Objects are mapped as the NHibernate Best Practice would, with HBM files directly embedded in the containing assembly.
  • Mappings and pocos are physically in 2 different assemblies
  • The user Interface layer makes use of the POCOs directly by referencing the PoOCOs assembly (this is irrilevant as the nature of WCF that is Contrat Firts. Personally I hate generated entities..)

Here that’s the first thing note: everithing works correctly!

I heard and read somewhere something as “using WCF and NHibernate is impossible” or “using them togheter could made explode WCF”. Well, I think is important to understand and note what we think and wht we expect about when we say EVERITHING WORKS CORRECTLY.

Beginning with WCF, I have to remember that , it’s true WCF is born to glue some core technologies (Web Services, Remoting, …) as a “foundation” but, spending no many words, it’s made to work with “Entities” and, Entities, in the WCF world are just POCOs!

Continuing with NHibernate, it’s clear that some CORE functionalities are the MOST important things offered by the framework, I mean eg. lazy loading, collections, and so on.. These functinoalities, togheter with a fat-list of features, makes the framework on of the most important and more productive now.

Now with the advent of MS Entity Framework, the problem could be the same. I saw some Open Source projects that has been built for WCF, to give features like the “connected-local” way does to the “disconnected-distribuited” way could expect. These features makes the game as with DataSets, with Original, Modified, New and Deleted values. But came a question in mind to me, this however to work fine, surely has to “respect” some limitations? so we could take all these limitations (including maybe loosing the standards reducing the system to work just .net to .net), all the features, all the things that NH has more than Entity Framework and vice versa, and think, “if this is the situation I can use NH with WCF too” making use of some rules and some best-practice, trying to use all of the features I can without fall into a technology BREAK!

I have to say this could be a dissertation which could continue maybe for ever… I think should be clear that “the situation” drive into the solution, not all the projects are the same… So in the next post I will talk about the rules I adopted, maybe many people could find them antiquate, boring and non-elegant, but for me the most important thing is: Using NHibernate with WCF is possible!

Ciao

p.s. Sorry for my poor english…

Technorati Tag: ,

Recently I was in fight with an ORACLE “issue” I found in my day-by-day coding activity. I was lucky to discover this “issue”, after 2 hours spent in debugging, rewriting mappings, and some bad words I said to my computer, I found a solution and a post which was helpful for me.

The situation: I was mapping a simple, usual, set of Entity Properties to a related set of ORACLE NUMBER columns with no precision info. These columns were updated by a Package Procedure. So, assuming Column1 and Column2 were the comlumns I was trying to map, I mapped them in a standard way as:

   1: <!-- ... -->
   2: <property name="PropertyOne"
   3:           type="System.Decimal">
   4:   <column name="COLUMN1"/>
   5: </property>
   6: <property name="PropertyTwo"
   7:           type="System.Decimal">
   8:   <column name="COLUMN2"/>
   9: </property>
  10: <!-- ... -->

I though this could be a very easy and standard mapping, so I didnt take  so much care about, because this was a usual mapping, but I was wrong (maybe I wasn’t but Oracle were)

The problem: After I’ve done with the mapping above, I experimented a strange kind of Exception, this Exception was so strange I fall in a desperate search of all possible causes, but with no asnwers. The Exception was “Arithmetic operation resulted in an overflow“.

Now, no spending more words about my search for a cause, This situation makes me crazy, what could be the problem I asked to me? Then, as a last try, I’ve done the same operation I was doing with NHibernate in the old ADO.NET – DataSet way, and here the surprise: I FOUND THE SAME ERROR!! How was it possible, a column of NUMBER, with no precision (this assume the biggest precision supported, I though) fall in an Exception during reading from?

So after some search in the Google universe, I found a possible solution, and when I read about, it makes me too much crazy than before. I found a post in the Oracle Forum which describe a similar situation with the same exception, the answer says something like:

..We found that this is caused by a return value that has more places to the right of the decimal point than .NET can handle…Be careful of functions that return calculated values. They somethimes try to return a larger number than even ODP.NET can handle.

The solution: After days, I found this a ridiculous thing, why this could happen? Trying to better explain what above sayd, the problem was with CALCULATED VALUES maybe some times a calculated decimal can result in a decimal point number too bigger than a standard .NET decimal point number, so, it’s clear that I felt in this kind of exception, and NHibernate was innocent, because the underlying ODP.NET was the problem. I don’t want to spend much words, (I wrote more than I’ll wish), following the suggestion, this kind of bug could be solved in no many ways:

  1. change the mapping to use with a VIEW with truncated values, but I don’t like to intriduce more objects than it’s needed
  2. change the mapping to truncate values, but this fall into a “broken mapping”, a mapping which target to a single Database implementation, in my case Oracle
  3. change the procedure to outputs a well delimited  NUMBER value, but this wasn’t possible because this part of the application wasn’t written and controlled by me

So I decided for second way, in this way, just change the mapping as I will show, my code and my mapping go ahead without exceptions and any kind of problem, here is the modified mapping:

   1: <!-- ... -->
   2: <property name="PropertyOne"
   3:           type="Decimal"
   4:           formula="TRUNC(COLUMN1, 6)" />
   5: <property name="PropertyTwo"
   6:           type="Decimal"
   7:           formula="TRUNC(COLUMN2, 6)" />
   8: <!-- ... -->

In this way the resulting query result with the TRUNC function which applied as above truncate the decimal places as a desired, fixed number.

I hope this helps and could prevent anyone to a “crazy-noon” as does! ah… here is the Oracle Forum Post which gracefully helps me Arithmetic opreation…POST

Sorry for my poor and bad English, I hope I will learn well..Ciao!

Technorati Tag: ,,

Hi, recently I read many requests about how to map an Oracle RAW() Type. About 2 months ago, I was one of the those who was asking for a solution, I really cannot believe how could be possible there is not a solution, so searching in the Net I found 3 kind of solution, then, I decided what was the better for me and so I’ve implemented in my project, let’s begin.

First of all, in general a RAW column data type describe a raw binary data, generally describing a Guid. Even generally, when using a RAW data type mean, as .NET world, using a GUID, so our need is to use a RAW as GUID.

Normally there are 2 modes of reading a RAW data type, in the ADO.NET way, from a command:

  • if not is specified the RAW column returns as a Byte[] array
  • if specified, it’s possible to use during reading the RAWTOHEX(columnName) that converts from a raw byte array to a binary string, and vice versa during writing the HEXTORAW(columnName) that converts back from a binary string to a raw byte array.

but what if we want to map that column data type in a HBM mapping? Well, there are 3 ways (usually I use the last one):

  • Creating a custom NHibernate Dialect inheriting from the OracleDialect and map the Guid data type so the convertion is made while reading clearly by the engine
  • Specify a “formula” surrounding the column name and decorate the column name with the RAWTOHEX function, bu the problem came up when we want to write and we should use the HEXTORAW function
  • Creating a custom IUserType that converts to and from the RAW type.

As I said before I prefer the last one, so here is my solution: (here i will discuss the code in pieces, then in the end of the post is available the code to download)

Firts of all, we start creating a class named (in my case) RawType:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Data;
   4: using System.Text;
   5: using NHibernate;
   6: using NHibernate.SqlTypes;
   7: using NHibernate.UserTypes;
   8: 
   9: namespace MySolution.Data.Types
  10: {
  11:     public class RawType : IUserType
  12:     {

The class must inherits from IUserType, this interface instructs NHibernate we want map a specifed database column name with a custom type. Next, to correctly implement the type we need to specify the NHibernate SqlType represented by our custom type, I used the DbType.Binary in a static SqlType array variable as:

   1: private static SqlType[] types =
   2:     new SqlType[] { new SqlType(DbType.Binary) };

Next the core part of the implementation are the methods NullSafeGet and NullSafeSet that are used to handle the “raw ado read value” and translate it depending if we are reading or writing. In order, the implementation handles this situation as follow:

Reading – the value is converted from the raw binary data to System.String, this is done using the System.Guid structure that permit to create a Guid from binary array assuming a valid 32 byte array, the the ToString method with the “N” format creates a guid strings without wpecial characters

Writing - the value is converted from System.String to a raw binary representation, this is yet done using the System.Guid structure ToByteArray method

   1: public object NullSafeGet(IDataReader rs, string[] names, object owner)
   2: {
   3:     string result = null;
   4:     byte[] buffer = (byte[])NHibernateUtil.Binary.NullSafeGet(rs, names[0]);
   5:     if (null != buffer)
   6:     {
   7:         result = new Guid(buffer).ToString("N");
   8:         Array.Clear(buffer, 0, buffer.Length);
   9:     }
  10:     return result;
  11: }
  12: 
  13: public void NullSafeSet(IDbCommand cmd, object value, int index)
  14: {
  15:     if (null != value)
  16:     {
  17:         byte[] buffer = new Guid(((string)value)).ToByteArray();
  18:         NHibernateUtil.Binary.NullSafeSet(cmd, buffer, index);
  19:         Array.Clear(buffer, 0, buffer.Length);
  20:     }
  21: }

The rest part of the class is a normal implementation of a NHibernate custom type.

Now the interesting part is the hbm mapping file. We can use our type in any property even as id, declaring the generator as “assigned” because we can’t use any algorithm to assign the id value.

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
   3:                 assembly="MySolution"
   4:                 namespace="MySolution.Common.Entities">
   5:     <class name="Class1"
   6:             table="TABLE1">
   7:         <id name="Id"
   8:             type="MySolution.Data.Types.RawType, MySolution.Data">
   9:             <column name="ID" />
  10:             <generator class="assigned" />
  11:         </id>
  12:     <!-- ... -->
  13:     </class>
  14: </hibernate-mapping>

The last thing, I implemented an helper static method which generates a new “guid” from scratch.

   1: public static string GenerateNewId()
   2: {
   3:     Guid nextVal = Guid.NewGuid();
   4:     return nextVal.ToString("N");
   5: }

I hope this helps, I spend much time in finding a solution to my problem, because in the net there’s no so much on the argument, so I had to “create” a solution for my project. The next post would be another kind of serializing/deserializing binary raw to and from an Oracle Database taking care when the RAW column is not sure that contains a “well formed” binary data to build a System.Guid.

Here is the complete RawType code:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Data;
   4: using System.Text;
   5: using NHibernate;
   6: using NHibernate.SqlTypes;
   7: using NHibernate.UserTypes;
   8: 
   9: namespace MySolution.Data.Types
  10: {
  11:     public class RawType : IUserType
  12:     {
  13:         public static string GenerateNewId()
  14:         {
  15:             Guid nextVal = Guid.NewGuid();
  16:             return nextVal.ToString("N");
  17:         }
  18:         private static SqlType[] types = new SqlType[]
  19:             { new SqlType(DbType.Binary) };
  20:
  21:         public object Assemble(object cached, object owner)
  22:         {
  23:             return DeepCopy(cached);
  24:         }
  25:
  26:         public new bool Equals(object x, object y)
  27:         {
  28:             return (x == null ? false : x.Equals(y));
  29:         }
  30: 
  31:         public object DeepCopy(object value)
  32:         {
  33:             return value;
  34:         }
  35: 
  36:         public object Disassemble(object value)
  37:         {
  38:             return DeepCopy(value);
  39:         }
  40: 
  41:         public int GetHashCode(object x)
  42:         {
  43:             return x.GetHashCode();
  44:         }
  45: 
  46:         public bool IsMutable
  47:         {
  48:             get { return true; }
  49:         }
  50: 
  51:         public object NullSafeGet(IDataReader rs, string[] names, object owner)
  52:         {
  53:             string result = null;
  54:             byte[] buffer = (byte[])NHibernateUtil.Binary.NullSafeGet(rs, names[0]);
  55:             if (null != buffer)
  56:             {
  57:                 result = new Guid(buffer).ToString("N");
  58:                 Array.Clear(buffer, 0, buffer.Length);
  59:             }
  60:             return result;
  61:         }
  62: 
  63:         public void NullSafeSet(IDbCommand cmd, object value, int index)
  64:         {
  65:             if (null != value)
  66:             {
  67:                 byte[] buffer = new Guid(((string)value)).ToByteArray();
  68:                 NHibernateUtil.Binary.NullSafeSet(cmd, buffer, index);
  69:                 Array.Clear(buffer, 0, buffer.Length);
  70:             }
  71:         }
  72: 
  73:         public object Replace(object original, object target, object owner)
  74:         {
  75:             return original;
  76:         }
  77: 
  78:         public Type ReturnedType
  79:         {
  80:             get { return typeof(string); }
  81:         }
  82: 
  83:         public SqlType[] SqlTypes
  84:         {
  85:             get { return types; }
  86:         }
  87:     }
  88: }

Ciao!

Technorati: ,

Hi, I want, with this post, start writing about NHibernate and the related world of development. I recently started developing and using NHibernate in some projects, and I found the first set of great functionalities offered by the framework, I said the first set because I know that we know just a little subset of all the functionalities of the framework, specially all of us coming from the .NET area of development.

I’ve got the opportunity to use the framework in a project recently, I also used before but with a small set of features, and I’ve done some usage of the tool against the Oracle 10g Database, with ome restrictions, but we, the team, found some interesting patterns and practice to use with.

I’ll like to share with all of you about our experience, because we came up from the community, and we want to give tro the community our contribution, so I decided to begin to write in my blog about NHibernate, our experience, usage stories and code to share. I know Ayende owns the better blog about NHibernate, but I wish to contribute.

So, I know and I believe that NHibernate it’s still one of the better frameworks, I wish to came up with some initial posts about:

  1. Oracle RAW (GUID) type handler (implemented with a custom NH type)
  2. NHibernate criteria helper objects, which helps in the day by day ICriteria queries

So in the next days I’ll begin to write about. Ciao!

Technorati Tag:
Hi, I wish to do some post about some helpful, exciting and great tools I knew, discovered from the net, and from my job experience. I’m a supporter in sharing knowledge, so I wish to share with you what I found. I’ve learned so much from the net, and from other shared with me..!
Today I want to start speaking about some well known technologies as: Javascript and AJAX (again! I wish I’m going to start speaking about much more!!).
 
Some time ago I knew from a friend of mine a library about javascript called Mootools. I was so exited when I found it and when I tried what could be done with it, that I started to play with, and including in my projects. Why I was so exited?! Just two reasons: first, it’s so spectacular what it’s possible to do with, second, years ago I started to think javascript in that manner, but there anyone could believe javascript so.
 
Following my “discovery”, I started to looking for related technologies, and the things came up were so exciting, I found many other libraries doing the same (in some manner) but each one with some added value. Recently I found one of the best: Ext Library.
This library is more than a simple Javascript library, is a set of controls, codes, utilities, in other words, a World!
For now I will not say more about, I wish let you discover some features, next I will post about Ext.
 
With this fabulous library I found some other exciting things, a development environment, one of the best IDE Javascript, PHP, Rudy, and more. The most exciting feature is the Javascript Debugging one. Have to take a look! This is the Aptana IDE 1.0. Aptana is a free and commercial IDE, in the site you will find videos, examples a much much more! take a look!
 
Next post I will do, I wish to start directly with links and possible examples, with few words from “me”!
 
Recap:
image
Mootools Javascript Library:
 
image
ExtJS Javascript Library
 
image
Aptana IDE 1.0 (The Web 2.0 IDE)
 
Technorati Tags: , ,

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Yesterday night I was looking for some news from an open source project, accidentally I found 2 interesting tools, one from Microsoft and one from Businessware Architects.

The one from Microsoft is the XSDObjectGen, a tool for gerating code, pure classes, from xsd Schemas. It’s free to download at: http://www.microsoft.com/downloads/details.aspx?familyid=89e6b1e5-f66c-4a4d-933b-46222bb01eb0&displaylang=en

The one from Businessware Architects is the CodeXS, a code generation tool who does the same as XSDObjectGen and XSD tools from Microsoft. The Only difference is that CodeXS is open source and more, efficient, modular… It’s frre to download (registration required) at: http://www.bware.biz/default.htm?http://www.bware.biz/DotNet/Development/CodeXS/Article/Article_web.htm. There’s also an interesting article related to the tool which take a tour over functionality and more.

So I wish to take a try to evaluate the potential from that tool. Maybe one could say I found nothing new to the folks, but before yesterday I never saw that tools, so I want to examinee how can I use them in my projects.

Ciao!

Technorati tags: ,

Next Page »