/xamarin-forms-clean

Sample Xamarin.Forms application using Clean Architecture

Primary LanguageC#Apache License 2.0Apache-2.0

xamarin-forms-clean

Sample Xamarin.Forms application using Clean Architecture. Inspired in the done work by Fernando Cejas

Used NuGet Packages

sqlite-net-pcl: Nice ORM to store data with SQLite

Xam.Plugin.Connectivity: Plugin to detect network state

System.Reactive.Linq: Rx.NET plugin

Clean Architecture

You can review the concepts of Clean Architecture in this site.

Principles

1. Independent of Frameworks. The architecture does not depend on the existence of some library of feature laden software. This allows you to use such frameworks as tools, rather than having to cram your system into their limited constraints.

2. Testable. The business rules can be tested without the UI, Database, Web Server, or any other external element.

3. Independent of UI. The UI can change easily, without changing the rest of the system. A Web UI could be replaced with a console UI, for example, without changing the business rules.

4. Independent of Database. You can swap out Oracle or SQL Server, for Mongo, BigTable, CouchDB, or something else. Your business rules are not bound to the database.

5. Independent of any external agency. In fact your business rules simply don’t know anything at all about the outside world.

Alt text

Code sample

Observable class creation from API call.

return Observable.Create<List<CityEntity>>((emitter) =>
  {
    var json = GetAllCitiesFromApi();
    if (json != null)
    {
      emitter.OnNext(CityEntitySerializer.FromJson(json));
      emitter.OnCompleted();
    }
    else
    {
      emitter.OnError(new Exception("Cities were not found"));
    }
  });

Transformation from entity to a model class using a mapper

Mapper

	public class CityMapper : BaseMapper<City, CityEntity>
	{
		public override City Transform(CityEntity entity)
		{
			var city = new City
			{
				Image = entity.ImageLink,
				Name = entity.Name
			};
			return city;
		}
	}

Transform a list of entities into a list of model class

dataSource.Cities().Select(x => Mapper.TransformList(x));

Usecase execution

		public void Execute(IObserver<T> observer, P param)
		{
			if (observer != null)
			{
				Task.Run(() => 
				{
					IObservable<T> observable = BuildUseCaseObservable(param);
					AddDisposable(observable.SubscribeSafe(observer));
				});
			}
		}

Collaborate

You can collaborate with this project. Check the guidelines to contribute.

License

Copyright 2018 Gonzalo Martin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.