Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest RESTier and OData libraries #223

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public IQueryable<Airport> Airports
/// <returns>
/// <see cref="Person">
/// </returns>
[Operation(EntitySet = "People")]
[UnboundOperation(EntitySet = "People")]
public Person GetPersonWithMostFriends()
{
Person result = null;
Expand Down Expand Up @@ -158,7 +158,7 @@ public Person GetPersonWithMostFriends()
/// <returns>
/// <see cref="Airport">
/// </returns>
[Operation(EntitySet = "Airports")]
[UnboundOperation(EntitySet = "Airports")]
public Airport GetNearestAirport(double lat, double lon)
{
var startPoint = GeographyPoint.Create(lat, lon);
Expand All @@ -178,7 +178,7 @@ public Airport GetNearestAirport(double lat, double lon)
return nearestAirport;
}

[Operation(IsBound = true)]
[BoundOperation]
public Airline GetFavoriteAirline(Person person)
{
var countDict = new Dictionary<string, int>();
Expand Down Expand Up @@ -216,7 +216,7 @@ public Airline GetFavoriteAirline(Person person)
/// <summary>
/// Bound Function, get the trips of one friend with userName
/// </summary>
[Operation(IsBound = true)]
[BoundOperation]
public ICollection<Trip> GetFriendsTrips(Person person, string userName)
{
var friends = person.Friends.Where(p => p.UserName.Equals(userName)).ToArray();
Expand All @@ -231,7 +231,7 @@ public ICollection<Trip> GetFriendsTrips(Person person, string userName)
}
}

[Operation(IsBound = true)]
[BoundOperation]
public ICollection<Person> GetInvolvedPeople(Trip trip)
{
var shareID = trip.ShareId;
Expand All @@ -258,7 +258,7 @@ public ICollection<Person> GetInvolvedPeople(Trip trip)
/// <summary>
/// Unbound action, reset datasource.
/// </summary>
[Operation(OperationType = OperationType.Action)]
[UnboundOperation(OperationType = OperationType.Action)]
public void ResetDataSource()
{
DataStoreManager.ResetDataStoreInstance(Key);
Expand All @@ -270,7 +270,7 @@ public void ResetDataSource()
/// <param name="person">The person to be updated.</param>
/// <param name="lastName">The value of last name to be updated.</param>
/// <returns>True if update successfully.</returns>
[Operation(IsBound = true, OperationType = OperationType.Action)]
[BoundOperation(OperationType = OperationType.Action)]
public bool UpdateLastName(Person person, string lastName)
{
if (person != null)
Expand All @@ -284,7 +284,7 @@ public bool UpdateLastName(Person person, string lastName)
}
}

[Operation(IsBound = true, OperationType = OperationType.Action)]
[BoundOperation(OperationType = OperationType.Action)]
public void ShareTrip(Person personInstance, string userName, int tripId)
{
if (personInstance == null)
Expand Down Expand Up @@ -343,15 +343,15 @@ private static double CalculateDistance(GeographyPoint p1, GeographyPoint p2)

internal class ModelBuilder : IModelBuilder
{
public Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken cancellationToken)
public IEdmModel GetModel(ModelContext context)
{
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.Namespace = "Trippin";
modelBuilder.EntitySet<Person>("People");
modelBuilder.EntitySet<Airline>("Airlines");
modelBuilder.EntitySet<Airport>("Airports");
modelBuilder.Singleton<Person>("Me");
return Task.FromResult(modelBuilder.GetEdmModel());
return modelBuilder.GetEdmModel();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,48 @@ public static void Register(HttpConfiguration config)
config.MessageHandlers.Add(new ETagMessageHandler());
config.SetUrlKeyDelimiter(ODataUrlKeyDelimiter.Slash);
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
config.Routes.MapHttpRoute("Options", "{*OPTIONS}", new { controller = "CORS", action = "Options" }, new { Options = new myHttpRouteConstraint()});
config.UseRestier<TrippinApi>((services) =>
config.Routes.MapHttpRoute("Options", "{*OPTIONS}", new { controller = "CORS", action = "Options" }, new { Options = new myHttpRouteConstraint() });
config.Filter().Expand().Select().OrderBy().MaxTop(100).Count().SetTimeZoneInfo(TimeZoneInfo.Utc);
config.UseRestier((builder) =>
{
Func<IServiceProvider, IDataStoreManager<string, TripPinDataSource>> defaultDataStoreManager =
sp => new DefaultDataStoreManager<string, TripPinDataSource>()
builder.AddRestierApi<TrippinApi>(services =>
{
MaxDataStoreInstanceCapacity = 1000,
MaxDataStoreInstanceLifeTime = new TimeSpan(0, 30, 0)
};
Func<IServiceProvider, IDataStoreManager<string, TripPinDataSource>> defaultDataStoreManager =
sp => new DefaultDataStoreManager<string, TripPinDataSource>()
{
MaxDataStoreInstanceCapacity = 1000,
MaxDataStoreInstanceLifeTime = new TimeSpan(0, 30, 0)
};

Func<IServiceProvider, ODataValidationSettings> validationSettingFactory = sp => new ODataValidationSettings
{
MaxAnyAllExpressionDepth = 4,
MaxExpansionDepth = 4
};
Func<IServiceProvider, ODataValidationSettings> validationSettingFactory = sp => new ODataValidationSettings
{
MaxAnyAllExpressionDepth = 4,
MaxExpansionDepth = 4
};

services.AddSingleton<ODataValidationSettings>(validationSettingFactory);
services.AddChainedService<IModelBuilder>((sp, next) => new TrippinApi.ModelBuilder());
services.AddChainedService<IChangeSetInitializer>((sp, next) => new ChangeSetInitializer<TripPinDataSource>());
services.AddChainedService<ISubmitExecutor>((sp, next) => new SubmitExecutor());
services.AddSingleton(defaultDataStoreManager);
services.AddSingleton<ODataValidationSettings>(validationSettingFactory);
services.AddChainedService<IModelBuilder>((sp, next) => new TrippinApi.ModelBuilder());
services.AddChainedService<IChangeSetInitializer>((sp, next) => new ChangeSetInitializer<TripPinDataSource>());
services.AddChainedService<ISubmitExecutor>((sp, next) => new SubmitExecutor());
services.AddSingleton(defaultDataStoreManager);

// Add custom TrippinBatchHandler
ODataBatchHandler trippinBatchHandler = new TrippinBatchHandler(GlobalConfiguration.DefaultServer);
trippinBatchHandler.ODataRouteName = routeName;
services.AddSingleton(trippinBatchHandler);
// Add custom TrippinBatchHandler
ODataBatchHandler trippinBatchHandler = new TrippinBatchHandler(GlobalConfiguration.DefaultServer);
trippinBatchHandler.ODataRouteName = routeName;
services.AddSingleton(trippinBatchHandler);
});
});

RegisterTrippin(config, GlobalConfiguration.DefaultServer);

}

public static void RegisterTrippin(
HttpConfiguration config, HttpServer server)
{
// enable query options for all properties
config.Filter().Expand().Select().OrderBy().MaxTop(null).Count();
config.SetTimeZoneInfo(TimeZoneInfo.Utc);
config.MapRestier<TrippinApi>(
routeName,
"",
false); // Custom TrippinBatchHandler registered in UseRestier
config.MapRestier((builder) =>
{
builder.MapApiRoute<TrippinApi>(
routeName,
"",
false); // Custom TrippinBatchHandler registered in UseRestier
});
}
}

public class myHttpRouteConstraint : IHttpRouteConstraint
{
bool IHttpRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
Expand Down
Loading