Hi, all! Almost a week have passed, after I have posted my new flexible provider article. Maybe, nobody commented it, because of article compicity... So, today I will try to show you, how to use AcroDb.
The model
Usually, when you start to design your future application, you need to describe 3 levels of MVC, and first of it goes the model layer. For the simple projects, the model layer consists of simple tables/entities. Let's say we have to build a model for only one entity type, to store cars.
Car entity description
What fields should this entity have? Let them be:
ID
- the unique Guid of car record (primary key).Name
- car name string (if we need to specify the limit of this field, let it be maximum 80 chars).YearOfAssembly
- the year, when this car starts to drive (integer value).Description
- the text with notes about the car (infinite string).
Car entity prototype in AcroDb
Now let me describe, how this entity will look like in code using AcroDb
library.
[AcroDbEntity] | |
public interface ICar | |
{ | |
Guid ID { get; set; } | |
[AcroColumnStringLength(80)] | |
string Name { get; set; } | |
int YearOfAssembly { get; set; } | |
[AcroColumnStringLong] | |
string Description { get; set; } | |
} |
And if you have setup AcroDataContext
class settings correctly as it was described in first post, after application start you will have a Car
table in your database/no-relational system. Or if you will change properties of this interface, table schema will be updated, thanks to SubSonic
.
So, AcroColumnStringLength
attribute is used to specify the maximum length of the column in db. The AcroColumnStringLong
attribute marks te column as infinite (MAX) size. For all column attributes, please, read here.
Working with entity
Now I will show the parts of code, how to work with our entity.
Creating and saving an object
using (var manager = AcroDataContext.Go) | |
{ | |
//Object creation and filling | |
var car = manager.Provide<ICar>().Create(); | |
car.Name = "Mitsubisi Lancer"; | |
car.YearOfAssembly = 2009; | |
car.Description = "Mitsubisi Lancer, a bit new, but red one"; | |
//Saving an entity | |
manager.Provide<ICar>().Save(car); | |
//Don't forget to submit changes | |
manager.SubmitChanges(); | |
} |
Querying a db
Here is a sample of Linq query for multiply objects:
using (var manager = AcroDataContext.Go) | |
{ | |
//We will find all cars assembled in 2009 and will show it to console | |
var collection = manager.Provide<ICar>() | |
.Query.Where(c => c.YearOfAssembly == 2009); | |
foreach (var car in collection) | |
Console.WriteLine(car.Name); | |
} |
Almost the same example for single object and count function:
using (var manager = AcroDataContext.Go) | |
{ | |
//We will find one car assembled in 2009 and which is first in our db | |
var car = manager.Provide<ICar>() | |
.Query.Where(c => c.YearOfAssembly == 2009).First(); | |
Console.WriteLine(car.Name); | |
} |
As you can see, here you can use usual Linq methods applying to the Query
property, as Query
property is IQueryabe
type.
Updating and deleting an entity
using (var manager = AcroDataContext.Go) | |
{ | |
//Object search and changing | |
var car = manager.Provide<ICar>() | |
.Query.Where(c => c.YearOfAssembly == 2009).First(); | |
car.Name = "Mitsubisi Lancer (Red)"; | |
manager.Provide<ICar>().Save(car); | |
manager.SubmitChanges(); | |
//Deleting an entity | |
manager.Provide<ICar>().Delete(car); | |
//Don't forget to submit changes even after delete | |
manager.SubmitChanges(); | |
} |
The end for today
As you can see, I have tried to save the simplicity in working with objects as in Linq2Sql
, but have added a multi-context support for it, so you are not limited only for MS SQL...
Also, you don't need to work with db directly. All tables/objects will be created for you automatically.
If you have any comments/proposals please, write them in comment form. Next time I plan to describe, how to use simple business logic layer with AcroDb
and relations between entities.