/// <summary> /// Configure the ActiveRecord for Accounts /// NOTES: - All service objects can be constructed with a repository, validator, settings object. /// - All service objects can be constructed with a fake repository ( in memory ) for testing. /// /// POSSIBLE CONFIGURATIONS: /// 1. Singleton account service as is. /// 2. Singleton account service and enable repository connection configuration. /// 3. Factory method for creating account service and enable repository connection configuration. /// 4. Factory methods for creating service, repository, validator and enabling repository connection configuration. /// </summary> /// <param name="context"></param> public override void Init(object context) { string config = (context == null) ? "factory_service" : (string)context; string columnsToIndex = "Id,UserName,UserNameLowered,Email,EmailLowered,Password"; IEntityRepository <Account> repository = new EntityRepositoryInMemory <Account>(columnsToIndex); switch (config) { case "singleton_service": Accounts.Init(new AccountService(repository, new AccountValidator(), new AccountSettings())); break; case "singleton_service_db": Accounts.Init(new AccountService(repository, new AccountValidator(), new AccountSettings()), true); break; case "factory_service": Accounts.Init(() => new AccountService(repository, new AccountValidator(), new AccountSettings()), true); break; case "factory_all": Accounts.Init(() => new AccountService(), () => new EntityRepositoryInMemory <Account>(columnsToIndex), () => new AccountValidator(), true); break; default: // "factory_service" Accounts.Init(() => new AccountService(repository, new AccountValidator(), new AccountSettings()), true); break; } }
/// <summary> /// Run the application. /// </summary> public override BoolMessageItem Execute() { // Fake repository for examples/unit testing. // Use this since database is not ready. EntityRepositoryInMemory <Person> repo = new EntityRepositoryInMemory <Person>("Id,Name,Ssn"); // 1. Initialize the configuration for active record. // See Example_ActiveRecord for more information. Persons.Init(() => new PersonService(repo, new PersonValidator(), new PersonSettings()), false); // 2. Create new Person Person p = Persons.New(); p.Name = "kishore"; p.Ssn = 123456789; // 3. Save. BoolResult <Person> result = Persons.Save(p); Console.WriteLine(string.Format("Saved : {0} - {1}", result.Success, result.Message)); // 4. Retrieve by id. Person p2 = Persons.Get(p.Id).Item; Console.WriteLine("Name : {0} - Ssn {1} - Desc {2}", p2.Name, p2.Ssn, p2.PersonDescription); return(BoolMessageItem.True); }