Created
February 2, 2016 00:11
-
-
Save jmreynolds/b9be80285d390d7a7764 to your computer and use it in GitHub Desktop.
C# 6 Features: Immutable Auto-Properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void RunService() | |
{ | |
var personService = new PersonService(new SqlHelperFactory()); | |
var person = new Person | |
{ | |
FirstName = "Joe", | |
LastName = "Reynolds" | |
}; | |
var newId = personService.WritePerson(person); | |
Console.WriteLine(newId); | |
ReadKey(); | |
personService.CleanUp(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Person GetPerson(Guid personId) | |
{ | |
string sSQL = "SELECT FirstName, LastName " + | |
"FROM Person " + | |
"WHERE PersonId = @personId"; | |
Person result; | |
using (var sqlHelper = _sqlHelperFactory.GetSqlHelper()) | |
{ | |
sqlHelper.AddParam("personId", personId); | |
var table = sqlHelper.GetTable(sSQL); | |
var firstName = table.Rows[0][0].ToString(); | |
var lastName = table.Rows[0][1].ToString(); | |
result = new Person | |
{ | |
PersonId = personId, | |
FirstName = firstName, | |
LastName = lastName | |
}; | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Person GetPerson(Guid personId) | |
{ | |
string sSQL = "SELECT FirstName, LastName " + | |
"FROM Person " + | |
"WHERE PersonId = @personId"; | |
Person result; | |
using (var sqlHelper = _sqlHelperFactory.GetSqlHelper()) | |
{ | |
sqlHelper.AddParam("personId", personId); | |
var table = sqlHelper.GetTable(sSQL); | |
var firstName = table.Rows[0][0].ToString(); | |
var lastName = table.Rows[0][1].ToString(); | |
result = new Person(personId, firstName, lastName); | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person | |
{ | |
public Person() | |
{ | |
} | |
public Person(Guid personId, string firstName, string lastName) | |
{ | |
PersonId = personId; | |
FirstName = firstName; | |
LastName = lastName; | |
} | |
public Guid PersonId { get; } = Guid.Empty; | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person | |
{ | |
public Guid PersonId { get; } = Guid.Empty; | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment