Sony Arouje Blog

a programmer's log

Posts Tagged ‘Generic repository

db4o A light weight Object Oriented Database

with 8 comments

Now a days I am spending my free time in rewriting one of my freeware for Flickr users called iTraveller. One of the design goals was the application should be scalable. My major concern was, I cannot freeze my db design before my first release because I am planning to have multiple version with added functionalities, so the db structure may change in the future. If I use conventional db’s then I have to write functionality to add or remove columns. iTraveller is not very data centric application, so managing db change scripts and writing a functionality to apply these changes when user upgrade to new version will be a big pain for me.

First I thought of using MongoDB, because I used Mongo db in a Silverlight application. But then finally dropped that idea. I want some thing more lighter than Mongo and NoSql sort of database. I evaluated several db’s but nothing suites what I am looking for. Finally my search ends at db4o. The usage of db4o is very straight forward and very easy to learn, db4o installer comes with a very good documentation to start off with the development.

db4o is a light weight Object Oriented database. Using db4o I can persist my entities very easily without any configuration. I can add or remove new properties to my entities without affecting the existing persisted entities.

I created a generic repository sort of class around db4o, the same way I did one for Entity Framework. This generic repository reduced lot of my work and act as the entry point to persist my entities to db4o data file. Below is the Generic repository I created for db4o.

using Db4objects.Db4o;
public class GenericRepository:IRepository,IDisposable
{
    private static IRepository _singleRepoInstance;
    public const string DBNAME = "Data.dat";

    public static IRepository GetRepositoryInstance()
    {
        if (_singleRepoInstance == null)
            _singleRepoInstance = new GenericRepository();

        return _singleRepoInstance;
    }

    IObjectContainer _context = null;
    private IObjectContainer Context
    {
        get 
        {
            if(_context==null)
                _context = Db4oFactory.OpenFile(DBNAME);
            
            return _context;
        }
    }

    private IList<TEntity> Query<TEntity>() where TEntity : class
    {
        return this.Context.Query<TEntity>();
    }

    public IList<TEntity> GetAll<TEntity>() where TEntity : class
    {
        return this.Query<TEntity>().ToList();
    }

    public IList<TEntity> GetAll<TEntity>(TEntity entity) where TEntity : class
    {
        return this.Context.QueryByExample(entity).Cast<TEntity>().ToList();
    }

    public IList<TEntity> Find<TEntity>(Func<TEntity, bool> criteria) where TEntity : class
    {
        return this.Query<TEntity>().Where<TEntity>(criteria).ToList<TEntity>();
    }

    public TEntity Single<TEntity>(Func<TEntity, bool> criteria) where TEntity : class
    {
        return this.Query<TEntity>().Single<TEntity>(criteria);
    }

    public TEntity First<TEntity>(Func<TEntity, bool> criteria) where TEntity : class
    {
        return this.Query<TEntity>().FirstOrDefault<TEntity>(criteria);
    }
    public TEntity First<TEntity>(TEntity criteria) where TEntity : class
    {
        return this.Context.QueryByExample(criteria).Cast<TEntity>().First<TEntity>();
    }
    public void Add<TEntity>(TEntity entity) where TEntity : class
    {
        this.Context.Store(entity);
    }

    public void Add<TEntity>(List<TEntity> entities) where TEntity : class
    {
        foreach (TEntity entity in entities)
        {
            this.Add<TEntity>(entity);
        }
    }

    public void Delete<TEntity>(TEntity entity) where TEntity : class
    {
        this.Context.Delete(entity);
    }

    public void Update<TEntity>(TEntity entity) where TEntity : class
    {
        this.Context.Store(entity);
    }

    public void Dispose()
    {
        this._context.Close();
        this._context.Dispose();
        this._context = null;
    }
}

The above repository is created in singletone mode. I did that way because I noticed that opening the database file is taking some delay and wanted to avoid that delay. So I wont close the data file until iTraveller get closed.

Let’s see how we can call the above repository to save an entity

GenericRepository repository = GenericRepository.GetRepositoryInstance();
repository.Add<Photo>(photos);

 

Below is the way we can retrieve some entities from the data file

IRepository repository = GenericRepository.GetRepositoryInstance();
List<LocalCategory> categories = repository.GetAll<LocalCategory>().ToList<LocalCategory>();
 

 

You can also retrieve data based on any condition as well. The below code will return a list of Photo object whose categoryId is 10.

IRepository repository = GenericRepository.GetRepositoryInstance();
return repository.Find<Photo>(p => p.Category.ID == 10).ToList<Photo>();

 

As you can see the db4o is very easy to use light weight database without the hurdles of mapping to tables and all sort of stuffs.

Written by Sony Arouje

January 5, 2011 at 4:26 pm

Posted in .NET

Tagged with , , ,

Photo Album using Silverlight, Caliburn Micro and Mongo DB

with 14 comments

In this post I am going to explain a small Photo Album I created using Silverlight. With this app I also joined the band wagon of NoSQL movement. I used MongoDB as my backend for this application. This application is still in the development mode. I will add more functionality to this application like Public comments, admin module, etc. Currently this app shows the functionality of admin module, like upload photo, delete photo, etc. The application uses MVVM pattern and as usual I used Caliburn Micro here. I used several framework in the app as shown below

  • Silverlight
  • Caliburn Micro
  • Reactive Extension
  • MongoDB
  • WCF

In this application I used Reactive Extension (Rx) to perform Async service calls. I felt the Rx is a very powerful tool to perform Async calls. The one good advantage of Rx is you can place Async calls in Thread Pool and push it to dispatcher once the call completed. All these can be done in one or two lines of code. This way the UI will always responsive.

This is my first experiment with Mongo db, I can say it’s very easy to use and fast. There are several Mongo drivers for .NET available in the market and is free. I used the one from here. The advantage of Mongodb is, I don’t want to write code to map my POCO to tables. You can directly persist your entity to Mongodb. You can find a lot of article about Mongo db in internet

I don’t think I need to explain Caliburn Micro in detail here. I wrote a post that gives a brief introduction to Caliburn Micro.

Running MongoDB

Below are the steps to run MongoDB

  1. Download the latest version of Mongo db from Mongodb.org
  2. Extract the zip file to C:\Mongodb
  3. Create a Folder called data in C drive
  4. Create a subfolder called db inside data
  5. Goto command prompt and change directory to C:\Mongodb
  6. Type Mongod in the command prompt and hit enter

Now the Mongodb will listen to the default port and our app can make request. There are different ways of hosting Mongodb, you can get more details from Mongodb site.

The App

Below is the screen shot of my application.

image

This page will display the thumbnails of uploaded images. We can see the bigger image by clicking on the thumbnail as show below.

image 

The preview will display in a Windowless popup. We can delete the photo by clicking the Trash icon bottom right side of the image. We can upload images by clicking on the upload icon at the top right of the window. The upload page will get displayed as shown below.

image

When the user upload the image, the application will create a thumbnail version of the image and store along with the original image in the server. This helps to transfer and load the thumbnail page.

Let’s see the entity used in this application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace PicturePortfolio.Domain
{
    [DataContract]
    public class Photo
    {
        private const int THUMBWIDTH = 100;
        private const int THUMBHEIGHT = 100;
        public Photo()
        {
 
        }
        public Photo(PhotoPersistanceObject persistanceObject)
        {
            this.Name = persistanceObject.Name;
            this.Description = persistanceObject.Description;
            this.ID = persistanceObject.ID;
            this.PublicComments = persistanceObject.PublicComments;
            this.GUID = persistanceObject.GUID;
            
            this.LoadThumbNail();
        }

        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string GUID { get; set; }

        [DataMember]
        public byte[] ImageFile { get; set; }

        [DataMember]
        public byte[] ImageThumbNail { get; set; }

        [DataMember]
        public string Description { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public List<PublicComment> PublicComments { get; set; }

        public PhotoPersistanceObject GetDataForPersistance()
        {
            PhotoPersistanceObject photoPersistance = new PhotoPersistanceObject();
            photoPersistance.ID = this.ID;
            photoPersistance.Name = this.Name;
            photoPersistance.ImagePath = "/test/" + this.Name;
            photoPersistance.Description = this.Description;
            photoPersistance.PublicComments = this.PublicComments;
            if (string.IsNullOrEmpty(this.GUID) == true)
                photoPersistance.GUID = Guid.NewGuid().ToString();
            else
                photoPersistance.GUID = this.GUID;
            return photoPersistance;
        }


        public void SaveImage()
        {
            if (this.ImageFile == null)
                throw new ArgumentNullException();
            try
            {
                byte[] buffer = this.ImageFile.ToArray();
                MemoryStream memStream = new MemoryStream();
                memStream.Write(buffer, 0, buffer.Length);

                this.SaveOrginalImage(memStream);
                this.SaveImageThumbNail(memStream);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public void LoadImage()
        {
            string fullFileName = this.GetFileFullPath();
            this.ImageFile = this.GetImageAsByteArray(fullFileName);
        }

        public void LoadThumbNail()
        {
            string fullFileName = this.GetFilePath() + this.ThumbNailImageName;
            this.ImageThumbNail = this.GetImageAsByteArray(fullFileName);
        }

        #region Private Methods
        private void SaveOrginalImage(MemoryStream memStream)
        {
            System.Drawing.Image imgToSave = System.Drawing.Image.FromStream(memStream);
            imgToSave.Save(this.GetFileFullPath(), ImageFormat.Jpeg);
        }
        private void SaveImageThumbNail(MemoryStream memStream)
        {
            Image img = Image.FromStream(memStream);
            Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            Image imageToSave = img.GetThumbnailImage (THUMBWIDTH, THUMBHEIGHT, myCallback, IntPtr.Zero);
            imageToSave.Save(this.GetFilePath() + ThumbNailImageName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        private static bool ThumbnailCallback() { return false; }

        private byte[] GetImageAsByteArray(string fullFileName)
        {
            FileInfo fil = new FileInfo(fullFileName);
            if (fil.Exists == true)
            {
                FileStream fileStream = new FileStream(fullFileName, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[fileStream.Length];
                fileStream.Read(buffer, 0, (int)fileStream.Length);
                fileStream.Close();
                return buffer;
            }
            else
                return null;
        }

        private string GetFilePath()
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "Images\\";
            return filePath;
        }
        private string GetFileFullPath()
        {
            string fullPath = this.GetFilePath() + this.Name;
            return fullPath;
        }
        private string ThumbNailImageName
        {
            get
            {
                string fileName = this.Name.Substring(0, Name.LastIndexOf("."));
                return fileName + "_thumb.jpg";
            }
        }
        #endregion
    }
}

 

The entity class is very simple. I may need to give a brief explanation about PhotoPersistanceObject used in the entity class. The PhotoPersistanceObject is used for persisting meta data of the image like Name, description etc. You might think that why cant we save Photo object directly, yes I also thought the same way but their is some issue with Mongodb driver I used here. It’s not supporting byte array neither binary, It allows to store binary in Mongodb but throws error while retrieving. So I used a trimmed down version of Photo entity to persist the data.

Async Calls using Thread Pool

As I told before one of the cool feature of Reactive Extension is it’s advantage of making async calls in thread pool. Even if we place async calls in separate thread in Silverlight still it will block the UI to load if we do it in page load. Because the our thread will work in UI. We can achieve the placing calls in thread pool using Rx. See the below code.

private void GetAllThumbnails()
{
    var func=Observable.FromEvent<GetAllPhotosCompletedEventArgs>(_service,"GetAllPhotosCompleted")
        .ObserveOn(Scheduler.ThreadPool);
    _service.GetAllPhotosAsync();
    func.ObserveOn(Scheduler.Dispatcher)
        .Select(result => result.EventArgs.Result)
        .Subscribe(s => this.ParseThumbnails(s.ToList()));
}

As you can see the service calls are doing in ThreadPool. Once the call is done we will change the ObserveOn to Dispatcher, other wise cross thread expection will throw. I call the GetAllThumbnails in the constructor of a ViewModel. As we are doing the call in Threadpool my UI will loaded without any delay, once the call is completed the thumbnails will start display in the listbox.

Mongodb Repository

I wrote a Generic repository similar to the one I wrote for EF. You can find the generic repository for EF in one of my post. Below is the Generic repository class for Mongodb.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB;
using MongoDB.Linq;
using MongoDB.Connections;
namespace PicturePortfolio.Persistance
{
    public class GenericRepository:IRepository
    {
        public GenericRepository()
        {
 
        }

        Mongo _mongoDb=null;
        public IMongoCollection<TEntity> GetQuery<TEntity>() where TEntity : class
        {
            if (_mongoDb == null)
            {
                _mongoDb = new Mongo();
                _mongoDb.Connect();
            }
            return DataBase.GetCollection<TEntity>(typeof(TEntity).Name + "s");
        }

        IMongoDatabase db = null;
        private IMongoDatabase DataBase
        {
            get
            {
                if (db == null)
                {
                    db = _mongoDb.GetDatabase("Protfolio");
                }
                return db;
            }
        }

        public IList<TEntity> GetAll<TEntity>() where TEntity : class
        {
            try
            {
                return GetQuery<TEntity>().Linq().ToList<TEntity>();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public IList<TEntity> Find<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>> criteria) where TEntity : class
        {
            return this.GetQuery<TEntity>().Linq<TEntity>().Where(criteria).ToList<TEntity>();
        }

        public TEntity Single<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>> criteria) where TEntity : class
        {
            return this.GetQuery<TEntity>().Linq<TEntity>().Single(criteria);
        }

        public TEntity First<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>> criteria) where TEntity : class
        {
            return this.GetQuery<TEntity>().Linq<TEntity>().First(criteria);
        }

        public void Add<TEntity>(TEntity entity) where TEntity : class
        {
            this.GetQuery<TEntity>().Save(entity);
        }

        public void Delete<TEntity>(TEntity entity) where TEntity : class
        {
            this.GetQuery<TEntity>().Remove(entity);
            
        }

        public void Delete<TEntity>(IEnumerable<TEntity> entites) where TEntity : class
        {
            throw new NotImplementedException();
        }

        public void Update<TEntity>(TEntity entity, TEntity old) where TEntity : class
        {
            this.GetQuery<TEntity>().Update(entity, old, UpdateFlags.None, true);
        }
    }
}

 

This app is in very primitive stage. you may need to do lot of refactoring to make use in real scenario. I will work on this app and upload it to codeplex some times later.

Download the current source here.

Generic Entity Framework Repository with Eager Loading

with 7 comments

In this post am going to explain how we can write Generic repository for Entity framework. The implementation is based on the Repository pattern. Inspired by one of the blog by hibernatingrhinos 

Below code explains the Interface I used for creating the repository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace Generic.Repository.Repository
{
    public interface IRepository
    {
        IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class;
        IList<TEntity> GetAll<TEntity>() where TEntity : class;
        IList<TEntity> Find<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class;
        void Add<TEntity>(TEntity entity) where TEntity : class;
        void Attach<TEntity>(TEntity entity) where TEntity : class;
        void SaveChanges();
        bool EnableEagerLoading { get; set; }
    }
}

I only included only limited functionality to this Interface. You can extend the interface to add more functionality. Let’s see the implementation

using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Reflection;
namespace Generic.Repository.Repository
{
    public class GenericRepository:IRepository
    {
        string _connectionString = string.Empty;
        public GenericRepository(string connection)
        {
            _connectionString = connection;
        }
        #region Private methods
        private GenericContext _context = null;
        private GenericContext Context
        {
            get
            {
                if (_context == null)
                {
                    _context = GenericContext.GetContext(_connectionString);
                    _context.ContextOptions.LazyLoadingEnabled = false;
                }
                return _context;
            }
        }
        private IList<TEntity> LoadNavigationFields<TEntity>(IList<TEntity> entities) where TEntity : class
        {
            foreach (TEntity entity in entities)
            {
                PerformEagerLoading<TEntity>(entity, this.Context);
            }
            return entities;
        }
        private TEntity LoadNavigationFields<TEntity>(TEntity entity) where TEntity : class
        {
            PerformEagerLoading<TEntity>(entity, this.Context);
            return entity;
        }
        private void PerformEagerLoading<TEntity>(TEntity entity, ObjectContext context) where TEntity : class
        {
            PropertyInfo[] properties = typeof(TEntity).GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object[] keys = property.GetCustomAttributes(typeof(NavigationFieldAttribute), true);
                if (keys.Length > 0)
                {
                    context.LoadProperty(entity, property.Name);
                }
            }
        }
        #endregion

        #region Generic Repository methods
        public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
        {
            return this.Context.CreateGenericObjectSet<TEntity>();
        }

        public IList<TEntity> GetAll<TEntity>() where TEntity : class
        {
            IList<TEntity> entities = this.GetQuery<TEntity>().AsEnumerable<TEntity>().ToList();
            if (this._enableEagerLoading == false)
            {
                return entities;
            }
            else
            {
                return this.LoadNavigationFields<TEntity>(entities);
            }
        }

        public IList<TEntity> Find<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>> criteria) where TEntity : class
        {
            IList<TEntity> entities = this.GetQuery<TEntity>().Where(criteria).ToList();
            if (this._enableEagerLoading == false)
            {
                return entities;
            }
            else
            {
                return this.LoadNavigationFields<TEntity>(entities);
            }
        }

        public void Add<TEntity>(TEntity entity) where TEntity : class
        {
            this.Context.CreateGenericObjectSet<TEntity>().AddObject(entity);
        }
        public void Attach<TEntity>(TEntity entity) where TEntity : class
        {
            this.Context.CreateGenericObjectSet<TEntity>().Attach(entity);
        }
        public void SaveChanges()
        {
            this.Context.SaveChanges();
        }

        bool _enableEagerLoading = false;
        public bool EnableEagerLoading
        {
            get { return _enableEagerLoading; }
            set { _enableEagerLoading = value; }
        }
        #endregion
    }
}

The above implementation can be used in most of the CRUD operations. If you have any specific logic for your repository then you can extend it by implementing the IRepository interface. Most of the functionality doesn’t required much explanation except PerformEagerLoading().

Normally we load related entities using LoadProperty in the context or use Include in the Linq query. Both approach wont work here in generic approach as we do not know the properties to do this. I searched a lot to achieve eager loading in a generic mode. But nothing worked then thought of implementing one myself. I haven’t verified the performance of this approach but worked well for my scenario.

The approach I come with is, decorate the related entities with an attribute and use the reflection and iterate through the entity and load it using LoadProperty of context object. Below code will explain my approach.

private void PerformEagerLoading<TEntity>(TEntity entity, ObjectContext context) where TEntity : class
{
    PropertyInfo[] properties = typeof(TEntity).GetProperties();
    foreach (PropertyInfo property in properties)
    {
        object[] keys = property.GetCustomAttributes(typeof(NavigationFieldAttribute), true);
        if (keys.Length > 0)
        {
            context.LoadProperty(entity, property.Name);
        }
    }
}

 

NavigationFieldAttribute is a new attribute inherited from System.Attribute. You can see the implementation in my source code. For the implementation I used the same db model explained in my previous post.

Let’s write a test to verify our GenericRepository

string connectionString = "Data Source=PROLAPE00700\\SQLserver;Initial Catalog=ImagePublisher;User ID=sony;PWD=sony;MultipleActiveResultSets=True;";

[TestMethod()]
public void GetAllTest()
{
    IRepository genericRepository = new GenericRepository(connectionString);
    genericRepository.EnableEagerLoading = true;
    IList<User> users = genericRepository.GetAll<User>();
    Assert.AreNotEqual(0, users.Count);
}

[TestMethod]
public void FindTest()
{
    IRepository genericRepository = new GenericRepository(connectionString);
    genericRepository.EnableEagerLoading = true;
    IList<User> users = genericRepository.Find<User>(u => u.UserID == 1);
    User user = users[0];
    Assert.AreNotEqual(0, user.UserRoles.Count);
}

 

You can understand the EagerLoading functionality by setting false to EnableEagerLoading  in the FindTest above. If we do so the test will fail as it wont load User.UserRoles.

The advantage of this generic approach is we don’t want to create Repository object for each entity if you want to fetch a different entity in the same function. For e.g

IList<User> users=genericRepository.GetAll<Users>();

IList<Role> roles=genericRepository.GetAll<Role>();

Download the source code

Written by Sony Arouje

October 15, 2010 at 5:20 pm

Follow

Get every new post delivered to your Inbox.

Join 166 other followers

%d bloggers like this: