Mercurial > pub > bltoolkit
comparison HowTo/Data/DbManagerDemo.cs @ 0:f990fcb411a9
Копия текущей версии из github
| author | cin |
|---|---|
| date | Thu, 27 Mar 2014 21:46:09 +0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f990fcb411a9 |
|---|---|
| 1 using System; | |
| 2 using System.Collections.Generic; | |
| 3 | |
| 4 using NUnit.Framework; | |
| 5 | |
| 6 using BLToolkit.Data; | |
| 7 using BLToolkit.Mapping; | |
| 8 | |
| 9 namespace HowTo.Data | |
| 10 { | |
| 11 [TestFixture] | |
| 12 public class DbManagerDemo | |
| 13 { | |
| 14 // The MapValue attribute is used by BLToolkit. | |
| 15 // | |
| 16 public enum Gender | |
| 17 { | |
| 18 [MapValue("F")] Female, | |
| 19 [MapValue("M")] Male, | |
| 20 [MapValue("U")] Unknown, | |
| 21 [MapValue("O")] Other | |
| 22 } | |
| 23 | |
| 24 // Business object. Here we use C# 3.0 automatic properties, | |
| 25 // however it can be public fields, regular or abstract properties. | |
| 26 // The MapField attribute is used by BLToolkit to associate a database field | |
| 27 // with a business object property if they have different names. | |
| 28 // | |
| 29 public class Person | |
| 30 { | |
| 31 [MapField("PersonID")] | |
| 32 public int ID { get; set; } | |
| 33 public string FirstName { get; set; } | |
| 34 public string MiddleName { get; set; } | |
| 35 public string LastName { get; set; } | |
| 36 public /*[a]*/Gender/*[/a]*/ Gender { get; set; } | |
| 37 } | |
| 38 | |
| 39 // BLToolkit data access method. | |
| 40 // | |
| 41 public List<Person> /*[a]*/GetList/*[/a]*/(Gender gender) | |
| 42 { | |
| 43 /*[a]*/using/*[/a]*/ (/*[a]*/DbManager/*[/a]*/ db = new DbManager(/*[a]*/"DemoConnection"/*[/a]*/)) | |
| 44 { | |
| 45 return db | |
| 46 ./*[a]*/SetCommand/*[/a]*/( | |
| 47 "SELECT * FROM Person WHERE Gender = @gender", | |
| 48 db./*[a]*/Parameter/*[/a]*/("@gender", /*[a]*/Map.EnumToValue/*[/a]*/(gender))) | |
| 49 ./*[a]*/ExecuteList/*[/a]*/<Person>(); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 [Test] | |
| 54 public void Test() | |
| 55 { | |
| 56 List<Person> list = GetList(Gender.Male); | |
| 57 Assert.Greater(list.Count, 0); | |
| 58 } | |
| 59 } | |
| 60 } |
