Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ORMS: Lightspeed example
public class DbNamer : INamingStrategy
{
public string GetColumnName(string defaultName, string fieldName)
{
return DefaultName(defaultName);
}
public string GetCreatedOnColumnName(string defaultTableName, string className, string defaultName)
{
return DefaultName(defaultName);
}
public string GetIdColumnName(string defaultTableName, string className, string defaultName)
{
return DefaultName(defaultName);
}
public string GetLockVersionColumnName(string defaultTableName, string className, string defaultName)
{
return DefaultName(defaultName);
}
public string GetSchema(string defaultName, string objectName)
{
return DefaultName(defaultName);
}
public string GetSequenceName(string defaultName)
{
return DefaultName(defaultName);
}
public string GetSoftDeleteColumnName(string defaultTableName, string className, string defaultName)
{
return DefaultName(defaultName);
}
public string GetTableName(string defaultName, string className)
{
return "example_" + DefaultName(defaultName) + "s";
}
public string GetUpdatedOnColumnName(string defaultTableName, string className, string defaultName)
{
return DefaultName(defaultName);
}
// Turns a name such as Category into categories
private string DefaultName(string defaultName)
{
if (string.IsNullOrEmpty(defaultName))
return defaultName;
else
{
if (defaultName.EndsWith("y"))
defaultName = defaultName.Remove(defaultName.Length - 1) + "ie";
return defaultName.ToLower();
}
}
}
public class Person : Entity<Guid>
{
private string _name;
private string _email;
/// <summary>
/// Gets/sets the Name.
/// </summary>
public string Name
{
get { return _name; }
set { Set(ref _name, value); }
}
/// <summary>
/// Gets/sets the Email.
/// </summary>
public string Email
{
get { return _email; }
set { Set(ref _email, value); }
}
}
public class PersonManager
{
public static void Example()
{
// Requires app.config/web.config settings:
// <lightSpeedContexts default="SqlServer">
// <add name="SqlServer" connectionStringName="local" dataProvider="SqlServer2005"/>
// </lightSpeedContexts>
// Save example
LightSpeedContext context = new LightSpeedContext("SqlServer");
context.NamingStrategy = new DbNamer();
// 'var' is used in the examples
using (IUnitOfWork work = context.CreateUnitOfWork())
{
Person person = Person.New();
person.Name = "John";
person.Email = "john@aol.com";
work.Add(person);
// Other querying is available with a Query object
IList<Feed> list = work.Find<Person>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.