Konfiguracja encji w osobnej klasie | Ilaro.Admin
W Ilaro.Admin możemy konfigurować encje na dwa sposoby. Poprzez dodawanie atrybutów do klasy i zmiennych, albo poprzez fluent interfejs.
Atrybuty nie zawsze możemy dodać, lub też po prostu nie chcemy ich dodawać.
Natomiast fluent konfiguracja tak by wszystkie formularze wyglądały i działały tak jak chcemy trochę zajmuje, co zaśmieca Global.asax.cs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
Entity<Customer>.Register() .Table("Customers") .Display("Klient", "Klienci") .Id(x => x.CustomerID) .DisplayProperties( x => x.ContactTitle, x => x.ContactName, x => x.City, x => x.Country) .SearchProperties(x => x.ContactName, x => x.City) .DisplayFormat("{ContactName} - {CompanyName}") .PropertiesGroup("Base", x => x.CompanyName) .PropertiesGroup( "Contact", true, x => x.ContactName, x => x.ContactTitle, x => x.Phone, x => x.Fax) .PropertiesGroup( "Location", x => x.Address, x => x.City, x => x.Region, x => x.PostalCode, x => x.Country) .Property(x => x.CompanyName, x => { x.Column("ComapnyName"); x.Display("Firma", "Nazwa firmy klienta"); x.Template( display: Templates.Display.Html, editor: Templates.Editor.Html); x.Searchable(); x.Visible(); }); |
Oczywiście możemy utworzyć sobie klasę, w której umieścimy konfiguracje wszystkich encji i wywołamy ją w Global.asax.cs, co w sumie nie jest złym pomysłem.
Natomiast by to ułatwić przygotowałem generyczną klasę
EntityConfigurator .
Korzystanie z niej wygląda w ten sposób:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public class CustomerConfigurator : EntityConfiguration<Customer> { public CustomerConfigurator() { Table("Customers"); Display("Klient", "Klienci"); Id(x => x.CustomerID); DisplayProperties( x => x.ContactTitle, x => x.ContactName, x => x.City, x => x.Country); SearchProperties(x => x.ContactName, x => x.City); DisplayFormat("{ContactName} - {CompanyName}"); PropertiesGroup("Base", x => x.CompanyName); PropertiesGroup( "Contact", true, x => x.ContactName, x => x.ContactTitle, x => x.Phone, x => x.Fax); PropertiesGroup( "Location", x => x.Address, x => x.City, x => x.Region, x => x.PostalCode, x => x.Country); Property(x => x.CompanyName, x => { x.Column("ComapnyName"); x.Display("Firma", "Nazwa firmy klienta"); x.Template( display: Templates.Display.Html, editor: Templates.Editor.Html); x.Searchable(); x.Visible(); }); } } |
W mojej skromnej opinii wygląda to znacznie czytelniej niż łańcuch metod.
Konfiguracja poprzez EntityConfiguration korzysta z dokładnie tego samego API co pierwotna fluent konfiguracja.
Gdy już utworzymy takie klasy dla wszystkich encji musimy jeszcze je zarejestrować. By to zrobić w Global.asax.cs dodajemy:
1 |
Admin.AssemblyCustomizers(typeof(Customer).Assembly).Register(); |
Rejestracja konfiguratorów korzysta z tego samego API co masowa rejestracja encji.
Implementując to rozwiązanie udało mi się ujednolicić API wszystkich metod konfiguracji, które wcześniej było niespójne i niekonsekwentne.
Dodaj komentarz