LinqRepository examples - Aghyad-Khlefawi/Coddee GitHub Wiki

C# Example:

//Code generated by the .dbml file
[Database(Name = "ExampleDB")]
public partial class ExampleDataContext : System.Data.Linq.DataContext
{

    private static MappingSource mappingSource = new AttributeMappingSource();

    #region Extensibility Method Definitions
    partial void OnCreated();
    #endregion

    public ExampleDataContext() :
        base(Properties.Settings.Default.ExampleDBConnectionString, mappingSource)
    {
        OnCreated();
    }

    public ExampleDataContext(string connection) :
        base(connection, mappingSource)
    {
        OnCreated();
    }

    public ExampleDataContext(System.Data.IDbConnection connection) :
        base(connection, mappingSource)
    {
        OnCreated();
    }

    public ExampleDataContext(string connection, MappingSource mappingSource) :
        base(connection, mappingSource)
    {
        OnCreated();
    }

    public ExampleDataContext(System.Data.IDbConnection connection, MappingSource mappingSource) :
        base(connection, mappingSource)
    {
        OnCreated();
    }

    public System.Data.Linq.Table<Person> Persons
    {
        get
        {
            return GetTable<Person>();
        }
    }
}

[Table(Name = "dbo.Persons")]
public class Person
{

    private int _ID;

    private string _FirstName;

    public Person()
    {
    }

    [Column(Storage = "_ID", DbType = "Int",IsPrimaryKey = true)]
    public int ID
    {
        get
        {
            return _ID;
        }
        set
        {
            if ((_ID != value))
            {
                _ID = value;
            }
        }
    }

    [Column(Storage = "_FirstName", DbType = "NVarChar(200)")]
    public string FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            if ((_FirstName != value))
            {
                _FirstName = value;
            }
        }
    }
}

//The business Model 
public class PersonModel : IUniqueObject<int>
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public int GetKey
    {
        get { return ID; }
    }
}
//The DBManager definition 
public class ExampleLineDBManager : LinqDBManager<ExampleDataContext>
{
    public override ExampleDataContext CreateContext()
    {
        return new ExampleDataContext(Connection);
    }
}
// The repository manager definition
public class ExampleRepositoryManager : LinqRepositoryManager<ExampleDataContext> { }

// The repository interface definition
public interface IPersonRepository : ICRUDRepository<PersonModel, int>
{
    Task<PersonModel> GetPersonByName(string name);
}

//The repository LinqToSQL implementation
[Repository(typeof(IPersonRepository))]
public class PersonRepository : CRUDLinqRepositoryBase<ExampleDataContext, Person, PersonModel, int>, IPersonRepository
{
    public Task<PersonModel> GetPersonByName(string name)
    {
        return Execute((db,table) =>
        {
            return _mapper.Map<PersonModel>(table.First(e => e.FirstName == name));
        });
    }
}

//The program
class Program
{
    static void Main(string[] args)
    {
        StartAsync();
        Console.ReadKey();
    }

    public static async void StartAsync()
    {
        //Create a new DBmanager
        var dbManager = new ExampleLineDBManager();
        dbManager.Initialize("data source=.;initial catalog=ExampleDB;integrated security=true;");
        //Create a new RepositoryManager
        var repoManager = new ExampleRepositoryManager();
        repoManager.Initialize(dbManager, new ILObjectsMapper());
        //Register the repositories from the same assembly
        repoManager.RegisterRepositories(Assembly.GetEntryAssembly().GetName().Name);
        //Get the repository manager
        var personRepository = repoManager.GetRepository<IPersonRepository>();

        var person = new PersonModel
        {
            ID = 1,
            FirstName = "Aghyad"
        };

        await personRepository.InsertItem(person);

        var res = await personRepository.GetItems();
        Console.WriteLine(res.Count()); //Outputs: 1

        var byID = await personRepository[1];
        Console.WriteLine(byID.FirstName);//Outputs: Aghyad

        person.FirstName = "Khlefawi";
        await personRepository.UpdateItem(person);

        var byName = await personRepository.GetPersonByName("Khlefawi");
        Console.WriteLine(byName.FirstName);//Outputs: Khlefawi

        await personRepository.DeleteItem(person);

        res = await personRepository.GetItems();
        Console.WriteLine(res==null); //Outputs: True
    }
}
⚠️ **GitHub.com Fallback** ⚠️