Wednesday, January 2, 2008

Cancer Sympathyquotes

Several tables to an entity map

are usually stored normalized data in a database. In the application you want but often work with the non-normalized data. Is the ADO.NET Entity Framework is the solution?

Yes and no is the answer. And to put it forward to take away, a mapping is possible from multiple tables to one entity, but only if they have a 1:1 relationship. That is, in most cases you will be better off if created in the database an appropriate view, this is mapped to the entity and the Update, Insert and Delete stored procedures to be solved.

But the solution. First, a model is created with the wizard, which contains all the tables.

The adjustments to the Conceptual Model (CSDL) are simple. On the desired attributes of the entity from the other tables are added. For example, here the entity Cars to the attribute value has been enhanced.

\u0026lt;xml: namespace prefix = edmx /> \u0026lt;edmx:conceptualmodels>
\u0026lt;schema xmlns = "http://schemas.microsoft.com/ado/2006/04/edm" alias = " Self "namespace =" Model ">
\u0026lt;entitycontainer name="Entities">
\u0026lt;entityset name="Cars" entitytype="Model2.Cars">
\u0026lt;/ entitycontainer>
\u0026lt;EntityType name="Cars"&#062
&#060key&#062
&#060propertyref name="ID_Cars"&#062
&#060/key&#062
&#060property name="ID_Cars" nullable="false" type="Guid"&#062&#060/property&#062
&#060property name="Brand" type="String" maxlength="50"&#062&#060/property&#062
&#060property name="Model" type="String" maxlength="50"&#062&#060/property&#062
&#060property name="Value" type="String" maxlength="50"&#062&#060/property&#062
\u0026lt;/ EntityType>
\u0026lt;/ schema>
\u0026lt;/ edmx: conceptualmodels>

adjustments to the storage model (SSDL) are not necessary because of the database, which is represented by this model will not change.

by adjusting the mapping of the newly defined attributes on the entity now in the existing storage model mapped. This is achieved by adding an additional fragment mapping.

\u0026lt;edmx:mappings>
\u0026lt;Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" space="CS">
&#060entitycontainermapping cdmentitycontainer="Entities" storageentitycontainer="dbo"&#062
&#060entitysetmapping name="Cars"&#062
&#060entitytypemapping typename="IsTypeOf(Model.Cars)"&#062
&#060mappingfragment storeentityset="Cars"&#062
&#060scalarproperty name="ID_Cars" columnname="ID_Cars"&#062
&#060scalarproperty name="Brand" columnname="Brand"&#062
&#060scalarproperty name="Model" columnname="Model"&#062
&#060/mappingfragment&#062
&#060mappingfragment storeentityset="Extensions"&#062
\u0026lt;scalarproperty name="ID_Cars" columnname="ID_Extensions">
\u0026lt;scalarproperty name="value" columnname="Value">
\u0026lt;/ MappingFragment>

\u0026lt;/ EntityTypeMapping & # 062
\u0026lt;/ EntitySetMapping>
\u0026lt;/ EntityContainerMapping>
\u0026lt;/ mapping>
\u0026lt;/ edmx: mappings> Do not

code as needed and EntitySets AssociationSet are CSDL and the To delete mapping.

Wednesday, December 19, 2007

St John's Wort Help With Confidence

From the name to the instance with DynamicMethod

A customer places during logging LogMessages different (different Classes) in a database. It shall state the type of message stored (LogMessage class name), the message itself as well as other fields in the table.
should now read the entries and the original objects are restored.

For this solution the obvious way with Reflection was chosen:

string s = "Namespace.Name.FromDB";
Assembly.GetAssembly Assembly a = (typeof (this class));
a.GetType Type t = (s );
base type b = (base type) Activator.CreateInstance (t);

Since this is not very performant, the code has improved as follows:

string s = "Namespace.Name.FromDB";
Assembly Assembly.GetAssembly a = (typeof (this class));
Type t = a.GetType (s);
ConstructorInfo i = t.GetConstructor (Type.EmptyTypes);
base type b = (base type) i.Invoke (null);

Where the class name and the associated ConstructorInfo in a static Dictionaire be stored so they do not have to be re-evaluated on every call. Thus we have from the second to nth call the desired performance gain. The code looks like this.

ConstructorInfo c = null;
if (_cinfo.TryGetValue (s, out c)) {

c = t.GetConstructor (Type.EmptyTypes);
lock (_cinfo)

{if (_cinfo.ContainsKey (s) )
{
_cinfo.Add (s, c);}

}}


yet elegant and much better performance you go with Dynamic Methods. The value returned by the dynamic method delegate is as previously passed the ConstructorInfo in a Dictionaire and for subsequent calls reused:

public delegate BaseType CtorDelegate ();

DynamicMethod dm = new DynamicMethod
(MyCtor ", typeof (base type), Type.EmptyTypes, typeof (base type) modules).
ILGenerator Ilgen dm.GetILGenerator = ();
ilgen.Emit (OpCodes.Newobj, t.GetConstructor (Type.EmptyTypes)); ilgen.Emit
(OpCodes.Ret) ;
CtorDelegate d = (CtorDelegate) dm.CreateDelegate (typeof (CtorDelegate));

The call is then just as short (d previously read from the Dictionnaire) and incredibly fast:

message = t (t) d ();

Now we just need everything to be beautifully packaged in a factory class and finished . More on this subject including performance measurements can be found as always with Google ;-)

Monday, November 5, 2007

Babies, Butterflies, Quotes

language features C # 3.0 and how to change the world

The. NET Framework 3.5 are again several features to the language C # 3.0 added to ease the developer's life much (unless you are on the maintenance page ).
Automatic Properties, offering Extension Methods, Lambda Expressions, Anonymous Types, etc., the developer of new ways, which can lead to very stylish code. With excessive Apply only the features or the elegance for which can lead to difficult and complex code visible presence and voice excesses.

Nevertheless, very nice is this (even if not very useful ;-):
7.TimesPrint ("my text");

elegant as essential: int
for (i = 0; < 7; i++)

{Console.WriteLine ( "my text");
}

But to make it work's needs another extension method:
public static void Times print (this int no, string s int)
{
for (i = 0; < no; i++)
{
Console.WriteLine ( s);}

}

Somewhat more generally could the call look like this and so does really make sense (the extension method must of course be adjusted accordingly):
7.Times (i => Console.WriteLine ("my text"));


Whether the maintainability of the major visible functionality is complex or the elegant syntax makes it more likely will show the latest practice.

Either way, the new features a lot of fun!

PS: A good overview of the new features here's .