Saturday, September 5, 2015

Hello again after a bit of delay.

Let's look at DataLibrary, the one containing the Entity Data Model, data methods and custom data types.

Below is the image of a custom data type to be used when querying for items in store:

It is a public class in it's own namespace (DataLibrary.Types.Store) containing properties that will hold data from Item entity representing store.Item data table. Interesting part is Details and Comments. I could've made those as two new classes and use them here. I decided to use built in types as they were enough to represent my data: list of strings for Details and dictionary of strings for Comments - key for title, value for comment.

Important note about creating custom data type classes: if you need a complex type for displaying data on a page from various joined data tables and no any extra data needed (data not binded to any table column for example), save time by NOT creating a type manually and then having to write complex linq query to fetch it from database. Use what is already available: SQL View. You can import views from database into entity framework, query them with linq to add some conditions or ordering and have data pronto.


Now let's look at the more interesting stuff - data methods. Below is the image of the public static Security class containing methods for operating on Security data located in security schema tables that holds data regarding users:

All methods are declared static so they can be used without instantiating the class. Static classes do not have a constructor but we could have a setter for eShopEntities instance to utilize Dependency Injection, a lot of buzz is about it these days. I don't do that and will rather instantiate eShopEntities object generated by the Entity Framework inside using statements inside our methods to work with database when needed. We are disposing of eShopEntities instance as soon as we get data from database, don't want it hanging around (it implements IDisposable interface so using statement will tell garbage collector to dispose of it as soon as we are done with it). These methods, I call them data methods as they work with data and they are located in DataLibrary, effectively make Stored Procedures in database obsolete. You do not need to have Linq inline with you business logic if you don't want to, and you don't want to except if you just need it in that one place, you can create methods that are reusable and respect DRY principle (don't repeat yourself). Feel free to download source code of the eShop website application and look around DataLibrary yourself.

Next is the WebLibrary, the code that runs it all, business logic layer.

In WebLibrary I will show you how to design and implement custom Forms Authentication with your own data stored in forms ticket (basically an encrypted cookie), connect that to Google Sign In so you can utilize Claims based authentication and how to implement Moneris Api. This will be divided over couple of posts.

As always, thank you for reading.

Wednesday, August 26, 2015

Dear readers, sorry for the delay. I had business I had to attend to and zero time left for other activities. I expect this to change in a couple of days. I didn't forget, and I still do care about this blog.

Thank you.

Tuesday, August 11, 2015

If you read my previous post, I was talking about DataLibrary class library. This library contains Entity Data Model which works with eShop database. In fact there are two databases used by the solution:
  1. eShop database which contains all the tables we are using for the custom functionality (users, shopping carts, payments, lookups) and this database was designed from ground up for eShop solution. I will be writing more below about this one.
  2. eShopContent database is database Composite C1 is using for placing its data. Tables were automatically generated by Composite C1. Interesting thing about Composite C1 is that data is not placed in SQL database right away after installation but you need to download SqlServerDataProvider package from Composite C1 store (you do this from Composite C1 console). I will not go into depths about this database as you can find more documentation on it online and it is easy to figure out where data is placed. Anyway, it is not important at all, except if you are merging different versions of the solution, then it can quickly become a mess if you are not careful!
Here is the ER diagram of the eShop database:
You can see that tables are placed in schemas - lookup, security, user, store and finance. There is only one table in lookup schema as the website is not a production one and there are not many features done on it except for the security and payment integration. In a production system there are usually multiple lookup tables, sometimes there are several lookup schemas, following other schema naming. For instance we can have user schema and lookupUser schema to contain the lookup tables that will be related to the tables in user schema.

Security schema contains tables that hold data for the security model used. Talking about security, I used Forms authentication with custom forms ticket data (forms ticket is an encrypted cookie saved on the client's browser). For more info about it, read the next posts where I will explain Google Sign In integration with Forms authentication model. You can notice that table security.User doesn't contain a password field as we do not have a password for the user - user is logged in trough Google Sign In and we are provided with a claim - in this case a token in a form of unique characters (a string) that we tie to the user data. Next time user logs on the website using Google Sign In we will check if we have the token value in the security.User table, if we do, we log the user in using Forms authentication. This is called Claim based authentication as we are having an Identity provider (IdP) we trust, in this case the Google, to provide is with claims. Claim can be anything, an email, first name, last name, unique id of some sort. I also use email and first and last name claims to populate data on the enrollment screen after first login. Enrollment is like registration but unlike registration we are not creating user identity, we are just enrolling this identity to our security model where it makes sense for us.

User schema is not that interesting, just storing some additional user information like user contact and address details.

Store schema is actually a main schema as it contains tables that hold items data. All items displayed in the eShop store are contained in groups (store.Group table) and items themselves are in store.Item table. ItemDetail and ItemComment tables contain additional information and comments for the particular item. I must say this is a very primitive store system as it has at least three drawbacks: first is that we do not keep the amount of items at hand anywhere. User can order an item but we don't track if we have it anymore. Second drawback is that we have only one level of grouping. This can be easily resolved by adding another column to store.Group table that would contain parent group id value. If value is null, that group is the parent group. If value is not null, that group is a nested group. That way we can create a tree structure with unlimited nesting capabilities. Third drawback is that there is no discount lookup table and discount percentage is entered directly in the store.Item table and thus making job of updating discounts a bit difficult.

Finance schema contains the shopping cart which is connecting user with items they want to buy (items added to cart) and logs the approved or declined transaction response data we receive from Moneris Api (finance.ShoppingCartPayment table). Moneris Api is a dll that I added to the bin folder of we website, created methods around it to give me ability to pay directly from shopping cart by calling one line of code and passing it credit card information. Moneris is a company based in Canada and they have pretty decent payment solutions and good documentation! I can't stress enough how documentation is important when creating an API that will be used by 3rd party developers and Moneris gives you everything in one PDF file, just read it and code away. There are no stored procedures nor functions in database as I am using Linq to query data and Entity Framework for all CRUD operations. Entity data model contains all these tables and follows schemas by having a separate diagram for every schema containing only tables from that schema.

Tomorrow, I will go deeper into code with some screenshots and explanations. Mainly how I query data with Linq and how data methods look like. I want to do this asap and get database and data layer out of the way before we can go into security and other business layer features.

There is not much to say about data layer integration with Composite C1, integration starts at the business logic layer. Feel free to download zip archive containing database backup files of both eShop and eShopContent databases and restore them to your test sql server to take a closer look.

Thank you.

Monday, August 10, 2015

Without further ado, this is how the eShop solution structure looks like when viewed in Visual Studio Solution Explorer:


You may notice we have 3 projects in a solution:

  1. Website: this is the website project, downloaded and extracted from Composite C1 zip file, installed (first time you run the website it will start Composite C1 installer - maybe better name is configurator, not installer?) and modified afterwards. Lets talk about Composite C1 a bit. It is a classical website, coded in C#. It has a routing system preset so you get nice URLs, no more convoluted .aspx links. Also, if you choose minimal razor template, it already has references to jQuery library, bootstrap, razor etc. That stuff is preset for you so you don't have to worry about it and waste time. Coolest thing about using bootstrap is that it just works, on all devices, all resolutions, you don't have to write a single line of CSS if you don't want to as everything is already there. There are also bootstrap interface builders available online for free where you can layout your page in minutes, copy the generated html and it will work on the site just like that. That is because generated html will have correct style classes selected for every html element and those style classes are defined by bootstrap. Another thing about Composite C1 is that all custom functionality that should be added on pages is contained in functions. Functions are like code units, containing the code, layout or both. For those coming from classical ASP.NET development world, user controls is something you do, day by day. You can create your function as user control, login to Composite C1 console and create a new page, layout it in Composite, add your function (user control) to this page and you are done. Recently, I moved away from the user controls as I stopped (well, almost) using code behind in my functions. I use Razor functions now as it makes writing html a breeze if you want to insert server tags, especially loops. No more percent signs for me, thank you (like <%= value from code behind %>). Loops are used when you want to display data from database in a repeater fashion. I also started heavily using Ajax calls, so my data is posted to service and response is sent back trough a service. A bit of JavaScript and Web Api controllers do the magic just fine. I will show more of this in my future posts, step by step. To take a look at what bootstrap has to offer take a peek here.
  2. DataLibrary is a class library containing the Entity Data Model, custom types (entities that are not in the edm nor database, custom types are basically classes with properties that represent custom views to aggregate data from multiple entities into one entity or abstract complex entity), methods that retrieve the data (static methods that query data using linq) and enumerations that represent lookup values. Data methods are placed here as they are reusable code, it makes maintenance much easier than to search your main project for linq queries. Enumerations makes code look nicer and make more sense (return clients where client type = individual makes more sense than return clients where client type = 1).
  3. WebLibrary class library contains custom security objects, helper classes, base classes and in this case, as this is an eShop solution, finance classes that abstract shopping cart and payment functionality. WebLibrary should also contain data caching classes, audit classes, resource objects etc. but these are not present as I mentioned in posts before, this is not production ready solution.
I will go into specific classes and explain more in future posts. For tomorrow, I will show database ER diagram and explain why we have 2 databases for the solution.

You can download full source code of the eShop website if you want to take a closer look yourself.

Thank you.

Sunday, August 9, 2015

This is the second post, one day after the first, as promised.

Today is a Sunday, a lazy day, so today I will try to explain more of the origins of the eShop website and tomorrow I will upload source code and database backups and start explaining those part by part.

Few years back, at work, I was wondering which way to go. Clients requested more dynamical website, they needed an easy way how to change content without contacting developers. Mind you, these are enterprise applications, mainly for the government, these website are not going to be updated daily by the human operator, they will pull data from database and accept user input trough forms of some sort. This functionality is custom and coded by a developer, but what about the plain content like FAQ, contact info, news updates, new regulations and announcements that should be displayed to general public? Only solution for this "static" content was to use some kind of CMS solution to allow client to create and edit pages. Sure, there are many solutions out there. Company I work in is a Microsoft partner so all solutions that are not using Microsoft stack are out of the window. After this condition there are not many CMS solutions left, and those come with their own framework, a steep learning curve and a convoluted way of customization (not just design stuff, I mean real core customization like implementing Claim based authentication security model with multiple Identity Providers using Shibboleth and Forms Authentication). One of those CMS solutions I browsed trough was Composite C1. I fell in love with it right away as it wasn't anything else but a website project I can open in Visual Studio and start coding, just the way I am used to and just the way I like it (uh huh uh huh :D). It's a good starter CMS and I say starter because you can't get site done in one day (my friend is using WordPress and makes websites daily) mainly because there are still not many plugins for it. That is a bad thing and a good thing as CMS doesn't come installed with garbage right away, you can even choose if you want bare bones installation or some kind of simple template. Best part is that you get a website with source code exposed that you open up in Visual Studio, compile it, do whatever you like, compile it again and publish it (copy-paste operation) on an IIS server and it just works. After doing several projects in Composite C1 for the company I decided to make a website on my free time at home as my graduation project (I must shamefully admit I was postponing that for several years due to other obligations at work and at home). So that is how eShop website was born and raised. I learned few things along the way while using Composite C1 over the years and I will try to explain more tomorrow when you can take a first look at the project itself.

Thank you.

Saturday, August 8, 2015

Hello World!

As a software developer I must say: starting with Hello World was the only logical option.

Hello my name is Ivan and this is my first blog. I made this blog to show you how you can modify and mold Composite C1 (free CMS written for .NET framework) into anything you need (I made eShop website) using Visual Studio to change guts and rearrange bare bones of the solution. I am not just talking about coding custom functions (plugins), but about changing and tweaking the solution itself by adding custom security model with Google Sign In, adding Web API for providing Json data to your Ajax calls, custom credit card payment solution by utilizing Moneris API.

Just a note: solution is not production ready as it wasn't meant for production, it was my playground. Some core features are missing like data caching, auditing and resources for full multilingual support. Website is currently hosted on WinHost and should be working without issues. I don't have SSL on it but for production environment it is a must as we are handling sensitive credit card data for payments!

I will present full source code of the solution and database for download, live preview of the website, and disclose several tips and tricks.

EShop website is a multi project solution. First project in a solution is a website, downloaded from here. That is the Composite C1 part. Other two are library projects, one for data and other for helper methods, security, shopping cart etc., all the custom stuff.

Language used is C# with technologies like Entity Framework, Linq and Web Api for backend and HTML5 with Razor, jQuery and Bootstrap for frontend.

I know this doesn't make much sense right now, it will make more so when I start showing code. It will be my obligation from now on to have at least one useful post a day. Scratched this one out as it turns out it's mission impossible for me to have time each day for (useful) posting.

Thank you.