The Sharp Factory App generates an entire framework based on repositories in order to give the programmer the ability to interact with one or more databases.
To select entities from the data store you will need to call the one of the methods available on the entity's repository.
The Sharp Factory App generates one Repository per entity.
You will need an instance of the RepositoryContainer class to access your repositories.
The naming convention is RepositoryContainer.{Model Name}.{Sql Schema Name}.{Entity Name}.{Name Of The Method}
The methods available for a particular entity's repository can vary from one entity to another.
For example if the entity has a Primary Key a method called ByPK will be created for that entity.
The following is a list of all possible data retrieval methods that could potentially be present in an entity's repository:
-
ByPK the parameters depend on the column or columns that comprise the Primary Key.
Returns a single entity.
Will only be present in the repository of those entities which have a primary key.
One parameter will be created for each column in the primary key. They will be named the same as the columns and have the correct C# types.
-
ByUK the parameters depend on the column or columns that comprise the Unique Key.
Returns a single entity.
Will only be present in the repository of those entities which have one or more unique keys.
One method will be created for each unique key present on the particular entity.
So if the entity has multiple unique keys one method will be generated for each one.
Each method would be named ByPK{number} like ByUK1, ByUK2 etc...
Intellisense will let you know the name of the Unique Key(in SQL Server) to which the method is mapped to.
One parameter will be created for each column in the unique key. They will be named the same as the columns and have the correct C# types.
-
ToList takes a single parameter of type Query.
Returns a list of entities.
Will be present in all repositories.
-
Top takes two parameters:
Returns a list of entities.
Will be present in all repositories.
-
First takes a single parameter of type Query.
Returns a single entity.
Will be present in all repositories.
Throws an exception if no entities are found that match the query.
-
FirstOrDefault takes a single parameter of type Query.
Returns a single entity.
Will be present in all repositories.
If no entities are found for the query will return null.
-
Single takes a single parameter of type Query.
Returns a single entity.
Will be present in all repositories.
Throws an exception if no entities are found that match the query.
Throws an exception if more than one entity is found that matches the query.
-
SingleOrDefault takes a single parameter of type Query.
Returns a single entity.
Will be present in all repositories.
If no entities are found for the query it will return null.
Throws an exception if more than one entity is found that matches the query.
The following example shows how to perform the following pseudo-code query.
Select the "Customer where PrimaryKey equals 123":
var repo = new RepositoryContainer();
var customer = _repo.AppDb.Sales.Customer.ByPK(123);
if (customer != null)
{
}
In this example assume the Customer table has a Unique Key with two columns: First Name and Last Name
The following example shows how to perform the following pseudo-code query.
Select the "Customer where FirstName(UK Field) equals "John" and LastName(UK Field) equals "Doe"":
var repo = new RepositoryContainer();
var customer = _repo.AppDb.Sales.Customer.ByUK("John", Doe);
if (customer != null)
{
}
The following example shows how to perform the following pseudo-code query.
Select all "Customers where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.BeginPredicate()
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
})
.EndPredicate();
var result = repo.AppDb.Sales.Customer.ToList(query);
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.Builder((builder) =>
{
builder
.Predicate((predicate) =>
{
predicate
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
});
});
});
var result = repo.AppDb.Sales.Customer.ToList(query);
The following example shows how to perform the following pseudo-code query.
Select the top 3 "Customers where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.BeginPredicate()
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
})
.EndPredicate();
var result = _repo.AppDb.Sales.Customer.Top(3, query);
foreach (var customer in result)
{
}
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.Builder((builder) =>
{
builder
.Predicate((predicate) =>
{
predicate
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
});
});
});
var result = _repo.AppDb.Sales.Customer.Top(3, query);
foreach (var customer in result)
{
}
Throws an exception if none are found.
The following example shows how to perform the following pseudo-code query.
Select the first "Customer where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.BeginPredicate()
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
})
.EndPredicate();
try
{
var customer = _repo.AppDb.Sales.Customer.First(query);
}
catch(InvalidOperationException ex)
{
}
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.Builder((builder) =>
{
builder
.Predicate((predicate) =>
{
predicate
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
});
});
});
try
{
var customer = _repo.AppDb.Sales.Customer.First(query);
}
catch(InvalidOperationException ex)
{
}
Returns null if none are found.
The following example shows how to perform the following pseudo-code query.
Select the first "Customer where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.BeginPredicate()
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
})
.EndPredicate();
var customer = _repo.AppDb.Sales.Customer.FirstOrDefault(query);
if (customer != null)
{
}
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.Builder((builder) =>
{
builder
.Predicate((predicate) =>
{
predicate
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
});
});
});
var customer = _repo.AppDb.Sales.Customer.FirstOrDefault(query);
if (customer != null)
{
}
Throws an exception if none are found.
Throws an exception if more than one is found.
The following example shows how to perform the following pseudo-code query.
Select a single "Customer where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.BeginPredicate()
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
})
.EndPredicate();
try
{
var customer = _repo.AppDb.Sales.Customer.Single(query);
}
catch(InvalidOperationException ex)
{
}
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.Builder((builder) =>
{
builder
.Predicate((predicate) =>
{
predicate
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
});
});
});
try
{
var customer = _repo.AppDb.Sales.Customer.Single(query);
}
catch(InvalidOperationException ex)
{
}
Return null if none are found.
Throws an exception if more than one is found.
The following example shows how to perform the following pseudo-code query.
Select a single "Customer where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.BeginPredicate()
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
})
.EndPredicate();
try
{
var customer = _repo.AppDb.Sales.Customer.SingleOrDefault(query);
if (customer != null)
{
}
}
catch(InvalidOperationException ex)
{
}
var repo = new RepositoryContainer();
var query = new Query<CustomerProperty>()
.Builder((builder) =>
{
builder
.Predicate((predicate) =>
{
predicate
.Where(CustomerProperty.Balance).GreaterThan(0)
.Or((exp)=>
{
exp
.Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
.Where((exp2) =>
{
exp2
.Where(CustomerProperty.Status).Equals("new")
.Or(CustomerProperty.Status).Equals("inactive");
});
});
});
});
try
{
var customer = _repo.AppDb.Sales.Customer.SingleOrDefault(query);
if (customer != null)
{
}
}
catch(InvalidOperationException ex)
{
}