Â
Importing relies on 2 distinct (and entirely extensible) mechanisms:
Note: Most âimportableâ classes implement both IImportSource as well as IImportDestination, as such, can be used as a source of import rows and a destination to import into
DoddleImport ships with some basic Import Sources, but just like all other Doddle Projects, you can easily create your own by implementing a simple interface.
// Basic import from one type of in-memory collection to another
ImportableDictionary source = new ImportableDictionary();
source.Fields.Add("ProductID", typeof(int));
source.Fields.Add("ProductName", typeof(string));
IDictionary row1 = source.AddRow();
row1["ProductID"] = 1;
row1["ProductName"] = "My Product";
List products = new List<Product>();
IImportDestination destination = new ImportableCollection<Product>(products);
Importer importer = new Importer();
importer.Import(source, destination);
// More realistic example, importing an Excel Spreadsheet into a SharePoint list
Stream uploadedFile = fileUpload.PostedFile.InputStream;
Spreadsheet spreadsheet = new Spreadsheet(uploadedFile);
SPList myList = Web.Lists["My List"];
IImportDestination destination = new ImportableSPList(myList);
Importer importer = new Importer();
importer.Import(spreadsheet, destination);
DoddleImport will provide automatic validation of your Import Source against the specified destination. This ensures that the entire import contents will be successfully imported as well as provide helpful error messages to your user so they can easily correct errors.
The validation mechanism works by automatically evaluating rules against each row being imported. This is handled through the IValidationRule interface.
If you want to change some of th default behavior of DoddleImport then using your application configuration file is the best place to start.
To use configuration, be sure to register the <section> node between <configSections> in your app.Config or web.Config
From there, you can add or remove validation rules that will be automatically applied to every import. You are also able to customize the default validation messages. Below is a quick sample of some of the changes that can be made.
<configuration>
<configSections>
<section name="doddleImport"
type="Doddle.Import.Configuration.ImportSection, Doddle.Import, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6f5f0fd458d019c9" />
</configSections>
<doddleImport>
<validation>
<rules>
<remove name="MissingHeadersRule" />
<add name="MyCustomRule" type="MyName.Importing.CustomValidationRule, MyName.Importing" />
</rules>
<messages>
<remove rule="RequiredFieldsRule" />
<add rule="RequiredFieldsRule" message="Unable to locate field '{0}'"/>
</messages>
</validation>
</doddleImport>
</configuration>
Thatâs all for now, check back for updates on this and some other projects.
Leave a Comment