Атрибуты
2017 год
Применяем атрибуты или описатели зашиваем в код? Ниже приводятся два аналогичных по функциональности примера, но с учетом различного подхода.
Применение атрибутов:
[Business(0, "Business", typeof(Strings))] public class Business { public Business() { _id = new IdProperty(); } private IdProperty _id; [BusinessProperty("Id", typeof(Business), typeof(Strings))] public IdProperty Id { get { return _id; } } }
По аналогии с движком на С++ "зашиваем" обращение к ресурсам в код:
public struct PropertyInfo { public int Id { get; set; } public string NameId { get; set; } public string Description { get; set; } } public class BusinessManager { public BusinessManager() { _propertyInfo = new List<PropertyInfo>(); RegProperty(new PropertyInfo() { Id = 0, NameId = "Oid", Description = "Идентификатор" }); } private List<PropertyInfo> _propertyInfo; protected void RegProperty(PropertyInfo pi) { _propertyInfo.Add(pi); } public int Find(string nameId) { foreach (var pi in _propertyInfo) { if (pi.NameId == nameId) return pi.Id; } throw new Exception(); } } public class Business { public Business() { _id = new OidProperty(); _manager = new BusinessManager(); _properties = new Dictionary<int, IBusinessProperty>(); RegProperty(0, Id); } protected OidProperty _id; public OidProperty Id { get { return _id; } set { _id = value; } } protected BusinessManager _manager; private Dictionary<int, IBusinessProperty> _properties; protected void RegProperty(int id, IBusinessProperty property) { _properties.Add(id, property); } }
Вывод: применяем атрибуты