Skip to content

0020: There is nothing but the problems


Hello all,

Another not so short period without writing here. Not a surprise, huh? 🙂 Instead I have done many other things, like investigation on architecture, code reorganization (another one), fixing code bugs (so I hah had to check the code too) and many other things. I hope that I will describe it today (I’m not so sure, because I’m very tired and I would love to get some rest ASAP). So, get to it.

First of all I have changed code structure (again). I’ve got rid of interfaces (IRepository, IBookRepository, etc.). So BookRepository class had to be removed too. Instead of this I have done one generic class,

Repository where T : Entity

Entity is a base class that any class representing database entity will inherit from. It implements INotifyPropertyChanged interface and contains one field that represents IdNo column. As you can imagine, this column will contain unique number that will identify any entity.

So, Repository contains for CRUD operations that have structure similar to this one:

virtual public void Add(List<T> items)
 using (LiteDataContext context = new LiteDataContext(new SQLiteConnection(this.connectionString)))

These methods are:

virtual public void Add(List<T> items)
virtual public void Delete(List<T> items)
virtual public List<T> GetAll()
virtual public List<T> GetByPredicate(Predicate<T> match)
virtual public void Update(List<T> items)

As you can see, methods are marked as virtual. It means that if I would like to create repository of very non-standard entities I would inherit from Repository and override desired methods. I think that this possibility will never occur, but it’s a nice feature that could help just in case.

There is one interesting method – GetByPredicate. Thanks to it I could remove any entity-specific method that was used to list entities (eg. GetByAuthor). Now I can just pass custom predicate and desired collection will be returned. I think that this increases level of code genericness.

That’s all about new class Repository. But after removing interfaces I had to remove partial classes DB.Books and DB.TableData. Instead of them I implemented new methods in DB class:

public void Add<T>(List<T> items, string tableName) where T : Entity
public void Delete<T>(List<T> items, string tableName) where T : Entity
public List<T> GetAll<T>(string tableName) where T : Entity
public List<T> GetByPredicate<T>(Predicate<T> match, string tableName) where T : Entity
public void Update<T>(List<T> items, string tableName) where T : Entity
public EntityCollection<T> GetEntityCollection<T>(string repoName) where T : Entity

Methods above are generic ones. Last one returns new generic class – EntityCollection that represents whole table. It means that it contains list of entities and few information about the table (creationDate and tableName). Andrzej suggested it and I think that it’s nice feature that would be helpful in the future.
Generally speaking this DB class is a part of the Bridge design pattern (not Facade that I stated previously).
Notice that I remove problem with calling generic interfaces that I described month ago, but I remove it temporarily – it will show up level above, when I implement next layer (maybe user interface, maybe controller or something that I don’t know yet), but it’s nothing that troubles me right now. Besides Andrzej told me that he will discuss OLGAtherer architecture with me this Friday. We think that it may be smart to reconsider all considerations 🙂 and plan whole architecture from the scratches. I believe that there won’t be many changes, because what’s right now seems to be quite reasonable. But we will see.

OK, what’s next. Aaa, I remember. I tried to check the code yesterday. I had a problem with ResourceManager – I used wrong data to create it and this piece of code thrown an error. Nothing that I couldn’t override. What was much more strange, that this code doesn’t work for me:

public partial class LiteDataContext : DataContext
  public LiteDataContext(IDbConnection connection)
           : base(connection)

This thrown exception: Unable to load the `DbLinq.SqlServer’ DbLinq vendor within assembly ‘SqlServer.dll’. Parameter name: connectionString. Strange, isn’t it? It took me quite a long time to figure out that I have to invoke another base constructor :

public LiteDataContext(IDbConnection connection)
            : base(connection, new DbLinq.Sqlite.SqliteVendor())

Strange. But it’s getting stranger when I’m recalling that I had this problem before but I forgot it. Silly me.

And last thing – I figured out that my concept of database structure was wrong. I assumed that any user would have got its own database and in this database there would be number of tables. For example, if user would like to create two repositories of books, because one physical repository is placed in the room, and second one is placed in the bedroom, there would be two Books table created with different names. This table structure would be reflected by Book class in OLGAtherer. THIS IS WRONG. There will be just one table of Books that will contain books from these two repositories, but there will be another table that will contain information of two separated physical repositories (libraries), and books entities from Book table will be linked with this table contains. I can’t describe it better now because we will discuss it with Andrzej this Friday (hopefully). I figured out that current architecture is wrong when I learned about TableAttribute. MSDN states that name of the table to be reflected must be known before code will be compiled. I planned to do otherwise. Of course I could “hack” it with reflection, or something else, but this would be stupid. There is something like “good practice” that I would like to learn about, and if I would do “hacks” like this program would work, but this isn’t my point – my program have to work well 🙂 As you can see I’m not a good “architect”, but I try 🙂 With help from good people I will overcome all the problems and learn a lot 🙂

Best regards

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: