Click here to Skip to main content
12,406,986 members (64,263 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Mobile Android
hello every body
i want to know how can i do :
decoupling user interface from back end design patterns

ie. you have mobile application to detect certain type of devices (tv or air condition ) which have different options (tv -> on, off ,up and down sound ... ,Air condition -> set coal degree ,on ,off ,timer...) ,where the mobile application load dynamic interface which produce the form page containing options according to detected device functionality

thanks 4 all
Posted 28-Jan-13 4:12am
Updated 28-Jan-13 20:44pm
v3
Comments
Ibrahim Uylas 28-Jan-13 10:26am
   
What is your purpose? Please give more detail
JemiM 28-Jan-13 10:30am
   
Please improve your question.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

That isn't really a question we can answer in a little tiny text box like this - it's a big question, with a lot of possible answers.
The simplest is probably to suggest that you look at various design patterns: Wiki: Three Tier[^] would probably be a good start.
  Permalink  
Comments
Sergey Alexandrovich Kryukov 28-Jan-13 11:29am
   
Right, a 5. A also suggested to see architectural material — main well-known patterns — please see.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I guess you want to implement a datamodel that is able to interact with the UI components, without knowing anything about them.

.Net provides a solid foundation for developing this kind of data objects.

I usually call this kind of objects for entities, and the first thing you need to look at is the INotifyPropertyChanged[^] interface.

public class EntityBase : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  
  protected void OnChanged(string propertyName)
  {
    if(PropertyChanged != null)
    {
      var eventArgs = new PropertyChangedEventArgs(propertyName) 
      PropertyChanged(this, eventArgs);
    }
  }
}

The next interface you need to be familiar with is the IEditableObject[^] which allows your object to back out of an editing operation.

public class EntityBaseData
{
  public virtual EntityBaseData Clone()
  {
    return new EntityBaseData();  
  }
}
 
public class EntityBase : INotifyPropertyChanged, IEditableObject
{
  EntityBaseData originalData;
  EntityBaseData data;
 
  public event PropertyChangedEventHandler PropertyChanged;
  
  protected void OnChanged(string propertyName)
  {
    if(PropertyChanged != null)
    {
      var eventArgs = new PropertyChangedEventArgs(propertyName) 
      PropertyChanged(this, eventArgs);
    }
  }
 
 
  // override this method in a derived class to create
  // an object that contains the data for that entity
  protected virtual EntityBaseData CreateData()
  {
    EntityBaseData result = new EntityBaseData();
    return result;
  }
 
  // override this method in a derived class to save changes
  // to some storage - such as a database
  protected virtual void SaveData()
  {
  }  
 
  protected EntityBaseData GetData()
  {
    if(data == null)   
    {
      data = CreateData();
    }
    return data;
  } 
 
  void IEditableObject.BeginEdit()
  {
    if(originalData == null)
    {
      originalData = GetData();
      data = originalData.Clone();
    }
  }
  
  void IEditableObject.CancelEdit()
  {
    if(originalData != null)
    {
      data = originalData;
      originalData = null;
      OnChanged("");
    }
  }
 
  void IEditableObject.EndEdit() 
  {
    SaveData();
    originalData = null;
  }
}

You would use the above classes in this manner:
public class PersonData : EntityBaseData
{
 int id;
 string name;
 
 public PersonData()
 { } 
  
 public override EntityBaseData Clone() 
 {
   PersonData result = new PersonData(); 
   result.Id = Id;
   result.Name = Name;
   return result;
 }
 
  public int Id
  {
    get
    {
      return id;
    }
    set
    {
      id = value;
    }
  }
  public string Name
  {
    get
    {
      return name;
    }
    set
    {
      name = value;
    }
  } 
}
 
public class PersonEnity : EntityBase
{
  private PersonData Data
  {
    get
    {
      return (PersonData)GetData();
    }
  }
 
  protected override EntityBaseData CreateData()
  {
    return new PersonData();
  }
 
  public int Id
  {
    get
    {
      return Data.Id;
    }
    set
    {
      if (Data.Id == value)
      {
        return;
      }
      Data.Id = value;
      OnChanged("Id");
    }
  }
  public string Name
  {
    get
    {
      return Data.Name;
    }
    set
    {
      if (Data.Name == value)
      {
        return;
      }
      Data.Name = value;
      OnChanged("Name");
    }
  } 
}

This works well for a single object, but it is quite common to work with lists of objects, which brings us to the generic BindingList[^] class:

public class BindingListEx<T> : BindingList<T>, ITypedList
{
  [NonSerialized()]
  private PropertyDescriptorCollection propertyDescriptorCollection;
  public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
  {
    if (listAccessors != null && listAccessors.Length > 0)
    {
      PropertyDescriptorCollection result = ListBindingHelper.GetListItemProperties(listAccessors[0].PropertyType);
      return result;
    }
    else
    {
      if (propertyDescriptorCollection == null)
      {
        propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T), new Attribute[] { new BrowsableAttribute(true) });
      }
      return propertyDescriptorCollection;
    }
  }
 
  public string GetListName(PropertyDescriptor[] listAccessors)
  {
    string result = typeof(T).Name;
    return result;
  }
}
Note that I'm also implementing the ITypedList[^] interface, which provides type information used by Visual Studio during designtime (Windows Forms)

The next thing you need is a Component[^]
public class BindingComponent<T> : Component,IListSource
{
  BindingListEx<T> items;
  public BindingComponent()
  {
    items = new BindingListEx<T>();
  }
 
  public BindingListEx<T> Items
  {
    get
    {
      return items;
    }
  }
 

  public bool ContainsListCollection
  {
    get { return false; }
  }
 
  public System.Collections.IList GetList()
  {
    return Items;    
  }
}

The above can now be used to create a bindable data source for PersonEntity
public class PersonsComponent : BindingComponent<PersonEnity>
{
 public PersonsComponent()
 { 
 }
}
The above can be used with Visual Studio where PersonsComponent should show up in the toolbox.

Well, this is the basics for creating entity classes in .Net.

Best regards
Espen Harlinn
  Permalink  
Comments
Sergey Alexandrovich Kryukov 28-Jan-13 15:58pm
   
Good ideas, effort, a 5.
—SA
Espen Harlinn 28-Jan-13 15:59pm
   
Thank you, Sergey
jibesh 28-Jan-13 19:59pm
   
my 5 Espen. you almost make OPs life easier ;)
Espen Harlinn 29-Jan-13 3:25am
   
Thank you, jibesh :-D
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):


MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].


But look at the pattens to learn the ideas, not to collect some cookbook recipes. You need to do architectural work starting from the goals of your application, taking into account its specific requirements.

—SA
  Permalink  
Comments
Espen Harlinn 28-Jan-13 14:36pm
   
Good links :-D
Sergey Alexandrovich Kryukov 28-Jan-13 15:57pm
   
Thank you, Espen.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160730.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100