Are you ready for HTML5

With HTML5 becoming more of the norm, well if it's not it is about to be, and IE6 not supporting it (hang on IE9 does not currently support it either!), so it will just have to be dumped, at last  :-)  As us developers will need to get up to speed on HTML5 there is a new resource available from Google, HTML5Rocks, lots of great resource and information available on this site

LINQ to SQL using SoC

I have had a few people saying that LINQ to SQL is hard to use for Separation of Concerns, so I've created a small project that show how to do it.  I have used the MEF as my SoC framework.

Project is using .Net 3.5

SoCLINQ2SQL.zip (905.61 kb)

To enhance this it is worth look at Mike Hadlow post of Repository.GetById using LINQ Expression Syntax

MEF not supporting open generic types

If you are trying to use MEF with open generic types like:

[Export(typeof(IRepository<>))]
public class itial; padding: 0px; margin: 0px; border: 0px initial initial;">Repository<T> : IRepository<al; background-color: transparent; color: black; background-position: initial initial; background-repeat: initial initial; padding: 0px; margin: 0px; border: 0px initial initial;">T>
   
where T : class
{

With an import of

[Import(typeof(IRepository<>))]
private IRepository<Contact> repository;

You'll come stuck, as the current implementation MEF does not support Open Generic Types.

For more information take a look at Glenn Block article Why doesn’t MEF support open-generics for exports? Because MEF is not type based

Also there is Open-generic support in the MEF Contrib, which can be found on codeplex.

The reasoning is this, MEF parts relate on contracts which are strings, not types. To illustrate, see the code below.

namespace Orders {

  public interface IOrderRepository {}

  [Export(typeof(IOrderRespository))]

  public class OrderRepository : IOrderRepository {

  }

}

Although I have passed typeof(IOrderRepsitory) to the export, that is just a convenience that gets converted to a contract name of "Orders.IOrderRepository". The same applies to the importer...

[Export]

public class OrderService(

  [Import]

  public IOrderRepository Repository {gets;set;} 

)

The import on IOrderRepository also gets converted to a contract name of "Orders.IOrderRepository". This way the container is able to satisfy the import as the 2 contracts match. In the same way we support closed generics, so....

public interface IRepository<T> {}

namespace Orders {

  [Export(typeof(IRepository<Order>))]

  public class OrderRepository : IRepository<Order> {

  }

}

[Export]

public class OrderService(

  [Import]

  public IRepository<Order> Repository {gets;set;} 

)

Will work because the OrderRepository is exporting the contractname "Orders.IRepository<Order>" and the OrderService is importing the same contract.

However, this is what it looks like if we try the same with open generics.

public interface IRepository<T> {}

namespace Utils {

  [Export(typeof(IRepository<>))]

  public class Repository : IRepository<T> {

  }

}

[Export]

public class OrderService(

  [Import]

  public IRepository<Order> Repository {gets;set;} 

Now the contract names will be different. The exporter will have a contract of  "Utils.IRepository<>" and the importer will have a contract of "Utils.IRepository<Order>".

It is a simple match up, that breaks down in the open-generics case. This is because fundamentally, MEF is not matching on type.

Original article form codeplex

How to generate LINQ to SQL Mapping

You may or may not have come across the ability within LINQ to SQL to have mapping to and from your own objects.

Microsoft has a mapping builder called sqlmetal, which is a code generation tool to generate the mapping automatically.

Here is a sample command line to generate the mapping file.

[code language=c#]

sqlmetal /server:. /database:mydb /map:mydbMappings.xml /code:code.dbml

[/code]

Running Gallio tests within Visual Studio

While using Gallio for performing Unit Tests within Visual Studio, I have noticed that some times you can't run Unit Tests, or it says that no tests available, with thanks to Lee, he pointed me to a simple fix

Visual Studio can be used to run your tests in process, with the debugger. In order for a project to be “testable”, you'll need to make sure it has the following in the csproj:

[code language=xml]

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

[/code]

 

PRETEC i-DISK - how to rescue

I recently purchased a Pertec i-Disk Bullet 32Gb, and within a hour I had broken it, it had become write protected, not sure why.  After an e-mail to Pertec support they sent me a solution to solve the problem, here is what they said and how to get your i-Disk working again

Please use USB flash drive recovery tool as attach file. And repair unworkable i-Disk Bulletproof as following procedures;

If this utility still can’t help you to repair your failure Bulletproof USB, we suggest you can send back the USB to original purchased store/vendor,

They will provide you RMA service for further check and repair process.

URescue utility repairing procedures:

1.           Backup your personal data stored in i-Disk Bulletproof if available.

2.           Plug unworkable i-Disk Bulletproof into a safety virus-free desktop or notebook computer.

3.           Execute “URescue.exe”, click “Update” button to repair i-Disk Bulletproof.

4.           Please be remind, all data stored in i-Disk Bulletproof will be deleted. Click “OK” to continue.

 

5.           Repair successfully, click “OK” to exit URescue utility.

URescue.zip (5.31 mb)

AutoMapper

I have just given an hour presentation on AutoMapper, and great Object to Object mapper.

Here is the source file with the samples

AutoMapper.zip (855.99 kb)

Convert a List in to a Collection

If you have an object that contains a List or IList, and you need to convert it in to a Collection, here is how you can do it in a single line.

[code language=c#]
var foo = new Collection<FooList>(fooList);
[/code]

 

Dependencies

How to serialize/deserialize a nullable date

The time will come when you are serializing and deserializing object to and from XML that you will come across nullable dates, or more correctly an attribute XML date that is nullable.  If you are like me and using the System.Xml.Serialization namespace, which make the complete process of serializing and deserializing so much easier.

Here is the issue, the code below is a nullable datetime, which is fine in .NET

[code language=c#]

 

        private DateTime? dob;
        [XmlAttribute]
        public DateTime? DOB
        {
            get
            {
                return dob;
            }
            set
            {
                dob = value;
            }
        }
[/code]

When you come the serialize/deserialize you find you get an error of:

System.InvalidOperationException: Cannot serialize member 'DOB' of type System.Nullable`1[System.DateTime]. XmlAttribute/XmlText cannot be used to encode complex types.

So to over come this you have a number of options, the one I have come up with is to pull the date in using a string and then pre-process the date and then store it in to the nullable date field, like this:

[code language=c#]

 

        private DateTime? dob;
        [XmlIgnore]
        public DateTime? DOB
        {
            get
            {
                return dob;
            }
            set
            {
                dob = value;
            }
        }

        [XmlAttribute("DOB")]
        public string DoBstring
        {
            get { 
                return DOB.HasValue
                ? XmlConvert.ToString(DOB.Value, XmlDateTimeSerializationMode.Unspecified)
                : string.Empty;
            }
            set { 
                DOB = 
                !string.IsNullOrEmpty(value)
                ? XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Unspecified)
                : (DateTime?) null;
            }
        }
[/code]

You notice that the nullable Date field is not decorated with an XmlIgnore attribute, and the string is picking up the DOB attribute tag

 

 

About the author

You have probably figured out by now that my name is Bryan Avery (if not, please refer to your browser's address field).  Technology is more than a career to me - it is both a hobby and a passion.  I'm an ASP.NET/C# Developer at heart...

Month List